專案下載連結
編寫dockerfile
因為php比較麻煩,本次以go專案為例
FROM golang:alpine AS builder
LABEL stage=gobuilder
ENV CGO_ENABLED 0
ENV GOOS linux
ENV GOPROXY https://goproxy.cn,direct
WORKDIR /build/zero
ADD go.mod .
ADD go.sum .
RUN go mod download
COPY . .
COPY service/hello/etc /app/etc
RUN go build -ldflags="-s -w" -o /app/hello service/hello/hello.go
FROM alpine
RUN apk update --no-cache && apk add --no-cache ca-certificates tzdata
ENV TZ Asia/Shanghai
WORKDIR /app
COPY --from=builder /app/hello /app/hello
COPY --from=builder /app/etc /app/etc
CMD ["./hello", "-f", "etc/hello-api.yaml"]
打包映象
docker build -t hello:v1 -f service/hello/Dockerfile .
luwei@luweideMacBook-Pro-2 greet % docker build -t hello:v1 -f service/hello/Dockerfile .
[+] Building 89.0s (13/13) FINISHED
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 629B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load metadata for docker.io/library/golang:alpine 79.4s
=> [1/8] FROM docker.io/library/golang:alpine@sha256:55da409cc0fe11df63a7d6962fbefd1321fedc305d9969da636876893e289e2d 0.0s
=> [internal] load build context 0.0s
=> => transferring context: 4.44kB 0.0s
=> CACHED [2/8] WORKDIR /build/zero 0.0s
=> CACHED [3/8] ADD go.mod . 0.0s
=> CACHED [4/8] ADD go.sum . 0.0s
=> CACHED [5/8] RUN go mod download 0.0s
=> [6/8] COPY . . 0.4s
=> [7/8] COPY service/hello/etc /app/etc 0.0s
=> [8/8] RUN go build -ldflags="-s -w" -o /app/hello service/hello/hello.go 8.5s
=> exporting to image 0.6s
=> => exporting layers 0.6s
=> => writing image sha256:a4bbb622bd909fe136fb5c0f714f1661b07d30cff24982a373c6c51f7670f789 0.0s
=> => naming to docker.io/library/hello:v1
打包結果
docker images
luwei@luweideMacBook-Pro-2 /tmp % docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello v2 6f23d965fb2f 14 hours ago 17.7MB
上傳到dockerhub
.進入連結 registry.hub.docker.com/
修改容器資訊
docker tag 6f23d965fb2f 2781897595/hello_test:v2
luwei@luweideMacBook-Pro-2 ~ % docker tag 6f23d965fb2f 2781897595/hello_test:v2
luwei@luweideMacBook-Pro-2 ~ %
luwei@luweideMacBook-Pro-2 ~ % docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
2781897595/hello_test v2 6f23d965fb2f 14 hours ago 17.7MB
hello v2 6f23d965fb2f 14 hours ago 17.7MB
<none> <none> fa9cda232ad4 16 hours ago 1.3GB
推上去
git push docker push 2781897595/hello_test:v2
luwei@luweideMacBook-Pro-2 ~ % git push docker push 2781897595/hello_test:v2
fatal: not a git repository (or any of the parent directories): .git
luwei@luweideMacBook-Pro-2 ~ %
luwei@luweideMacBook-Pro-2 ~ % docker push 2781897595/hello_test:v2
The push refers to repository [docker.io/2781897595/hello_test]
723e1838e8f9: Pushed
0b308dbc5609: Pushed
6050dcf697b5: Pushed
467d9565d60a: Pushed
1a058d5342cc: Mounted from library/alpine
v2: digest: sha256:9d07cfc1f7266404e457bb39289b6dc5b88a5a7a83a4ef0a673f2053db07980b size: 1363
luwei@luweideMacBook-Pro-2 ~ %
檢視
luwei@luweideMacBook-Pro-2 ~ % docker search 2781897595
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
2781897595/hello_test 測試應用 0
2781897595/donglei 自己測試使用 0
luwei@luweideMacBook-Pro-2 ~ %
編寫docer-compose
hello_test:
image: 2781897595/hello_test:v2 #根據需要選擇自己的映象
container_name: hello_test111
restart: always
ports:
- 8889:8889
執行結果
docker-compose up -d hello_test
luwei@luweideMacBook-Pro-2 lnmp % docker-compose up -d hello_test
Creating hello_test111 ... done
檢視容器
luwei@luweideMacBook-Pro-2 /tmp % docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6dd6c58c2965 2781897595/hello_test:v2 "./hello -f etc/hell…" 49 seconds ago Up 16 seconds 0.0.0.0:8889->8889/tcp hello_test111
6b0f04545e93 kibana:6.8.13 "/usr/local/bin/kiba…" 41 hours ago Up 38 hours 0.0.0.0:5601->5601/tcp kibana
請求容器
luwei@luweideMacBook-Pro-2 /tmp % curl -i http://localhost:8889/from/you
HTTP/1.1 200 OK
Content-Type: application/json
X-Trace-Id: e91e56360cb01195c2614bd21c2a9016
Date: Wed, 24 Nov 2021 02:36:54 GMT
Content-Length: 14
{"message":""}%
luwei@luweideMacBook-Pro-2 /tmp %
結束
本作品採用《CC 協議》,轉載必須註明作者和本文連結