Skip to main content
Version: 2.8.x(Latest)

Container deployment refers to deploying Golang applications using Dockerization, the most popular and recommended deployment method in the cloud service era.

In the examples below, we use main as the project name consistently.

1. Compile Program

Cross-platform cross-compilation is one of Golang's features, allowing us to easily compile the version needed for the target server platform, and it's statically compiled, making it very easy to solve runtime dependencies.

Use the following command to statically compile an executable for the Linux platform amd64 architecture:

CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o main main.go

The generated main is our statically compiled executable file deployable on Linux amd64.

2. Compile Image

We need to compile this executable file main into a Docker image for distribution and deployment. The runtime environment for Golang is recommended to use the Alpine base system image, resulting in a container image of about 20MB.

A reference Dockerfile is as follows:

FROM loads/alpine:3.8

LABEL maintainer="john@goframe.org"

###############################################################################
# INSTALLATION
###############################################################################

# Set a fixed project path
ENV WORKDIR /app/main

# Add application executable and set permissions
ADD ./main $WORKDIR/main
RUN chmod +x $WORKDIR/main

# Add static resource files
ADD resource $WORKDIR/resource

###############################################################################
# START
###############################################################################
WORKDIR $WORKDIR
CMD ./main

In this example, our base image is loads/alpine:3.8. For users in China, it is recommended to use this base image. The Dockerfile address for the base image is: https://github.com/gqcn/dockerfiles, and the repository address is: https://hub.docker.com/u/loads.

Then use the docker build -t main . command to compile and generate a Docker image named main.

Notes

It should be noted that in some project architectural designs, static files and configuration files may not be compiled and published with the image but managed and published separately.

For example, in projects using the MVVM pattern (such as those using the Vue framework), the front end and back end are often very independent, so the public directory is often not included in the image. In projects that use a configuration management center (such as using consul/etcd/zookeeper), the config directory is often not needed.

Therefore, the use of the Dockerfile provided above is only for reference, and necessary adjustments should be made based on actual conditions.

3. Run Image

Use the following command to run the image just compiled:

docker run main

4. Image Distribution

For container distribution, you can use the official Docker platform: https://hub.docker.com/, or consider using Alibaba Cloud in China: https://www.aliyun.com/product/acr.

5. Container Orchestration

In enterprise-level production environments, Docker containers often need to be used in conjunction with container orchestration tools like Kubernetes or Docker Swarm. Container orchestration involves a lot of content, and interested individuals can refer to the following materials: