docker的基本使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
FROM golang:alpine

# Set necessary environmet variables needed for our image
ENV GO111MODULE=on \
CGO_ENABLED=0 \
GOOS=linux \
GOARCH=amd64

# Move to working directory /build
WORKDIR /build

# Copy and download dependency using go mod
COPY go.mod .
COPY go.sum .
RUN go mod download

# Copy the code into the container
COPY . .

# Build the application
RUN go build -o main .

# Move to /dist directory as the place for resulting binary folder
WORKDIR /dist

# Copy binary from build to main folder
RUN cp /build/main .

# Export necessary port
EXPOSE 3000

# Command to run when starting the container
CMD ["/dist/main"]

docker build . -t go-dock

docker run -p 3000:3000 go-dock

左边的 3000 是主机(你的计算机)上的端口号。
右边的 3000 是Docker容器内部的端口号。

go mod tidy

go mod init [mod-name]


docker的基本使用
http://example.com/2023/09/06/docker的基本使用/
作者
Forrest
发布于
2023年9月6日
许可协议