1. Prepare documents

touch Dockerfile
docker search centos
docker pull cent
docker images
Copy the code

2. Write Dockerfile

The packaging dependency phase uses Golang as the base image
FROM golang:1.16 as builder

# Enable go Module
ENV GO111MODULE=on \
    GOPROXY=https://goproxy.cn,direct

WORKDIR /app

COPY.

# specify OS, etc., and go build
RUN GOOS=linux GOARCH=amd64 go build .

Because I rely not only on binaries, but also on HTML files in the Views folder and some static files in the Assets folder
# so I put these files in publish folder
#RUN mkdir publish && cp toc-generator publish && \
# cp -r views publish && cp -r assets publish

The run phase specifies Scratch as the base image
FROM alpine

LABEL maintainer="[email protected]"
LABEL version="1.0"
LABEL description="TTTT EEEE SSS T"

WORKDIR /app

# Copy all files from the publish folder from the previous phase
COPY --from=builder /app .

# Specifies the runtime environment variable
#ENV GIN_MODE=release \
# PORT=80

EXPOSE 12345

ENTRYPOINT ["./main"]
Copy the code

3. Make containers

sudo docker build -t dockgo . 

# test
docker run -d p:12345:12345 dockgo
sudo docker run -d -p 12345:12345 dockgo
sudo docker run -it -p 12345:12345 dockgo
Copy the code

4. Print the image into a tar packet

Sudo docker save -o dockergo.tar dockgo

5. Use the tar package

sudo docker image rm 9e37e0dbdd5c

Sudo docker load < path of the tar package

6. Delete the exited Container

docker rm $(docker ps -aq) 
Copy the code