SoFunction
Updated on 2025-03-06

Detailed explanation of the steps to deploy golang project in docker

Understand Docker

Docker can help you create a single deployable "unit" for your application. Such units are also called containers, which contain everything the application needs. For example, code (or binary files), runtime, system tools, and system library files. Packaging all of this required content into a single unit ensures that the exact same environment is provided no matter where the application is deployed. This technology can also help you maintain a completely consistent development and production environment that is often difficult to track.

Once built, the creation and deployment of containers will be automatically performed. This itself can avoid a series of problems. Most of these problems are caused by files that are out of sync, or differences between development and production environments. Docker can solve these problems.

Profits from using Docker during development

Some of the benefits you can get from using Docker in development work include:

  • All team members share a standard development environment.
  • Centrally update dependent components, using the same container anywhere,
  • From development to production, the exact same environment can be used, and
  • Easier to fix potential problems that may only be encountered in production environments.

Golang supports cross-compilation to generate executable programs for another platform on one platform

1. Cross-compilation

Compile Linux 64-bit executable programs under Windows

Execute in the root directory where the project is located:

GOOS: operating system of the target platform (darwin, freebsd, linux, windows)

set GOOS=linux

GOARCH: The architecture of the target platform (386, amd64, arm)

set GOARCH=amd64

Compilation

go build .

2. Packing mirror

Dockerfile (placed in the root directory)

FROM /common/alpine:latest
MAINTAINER FAE Config Server "fae@"
ADD mars /usr/local/bin/
ENTRYPOINT ["/usr/local/bin/mars"]
  • FROM: Set the basic mirror for the following instructions. A valid Dockerfile must use FROM as the first non-commented instruction
  • MAINTAINER: Set the Author field of the generated image
  • ADD: Copy new files, directories, or remote file URLs and add them to the file system path to the container
  • ENTRYPOINT: ENTRYPOINT allows configuration containers to run as executable files

Enter the docker environment under the following directory

set DOCKER_HOST=tcp://10.110.200.29:5555

Build a mirror

docker build -t /fcae/mars .

// Check whether the image is successfully builtdocker images

Start the service

docker run -i -d --name=mars -p 8011:8011 /fcae/mars
  • --name: Specify the service name
  • -p: Set the service to expose the port to the outside

Check whether the service starts successfully

docker ps -a

Summarize

The above is the entire content of this article. I hope that the content of this article has certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support.