Table of Contents
Dependency knowledge
1. Dependent environment
2. Writing a GoLang web program
3. Compile it into a program package under linux
4. Use Dockerfile to customize the image of our go program
file to This is our last step. If we use the
Home Operation and Maintenance Docker Deploy go projects based on Docker images (detailed examples)

Deploy go projects based on Docker images (detailed examples)

Feb 10, 2022 pm 05:54 PM
docker

This article brings you relevant knowledge about deploying go projects based on docker images, including issues related to writing GoLang web programs and compiling packages under Linux. I hope it will be helpful to everyone.

Deploy go projects based on Docker images (detailed examples)

Dependency knowledge

  • Go cross-compilation basics
  • Docker basics
  • Dockerfile custom image basics
  • Basics of writing docker-compose orchestration files

Of course, you can follow this step to complete the deployment even if you don’t know it at all. However, if there is a small problem in the middle, you will not know how to solve it. Of course you can also leave a message.

I developed and tested on a mac environment. If you are on windows, there may be a little difference, but there shouldn't be any big problems.

1. Dependent environment

  • Docker

2. Writing a GoLang web program

I will write one here The simplest hello world program, the listening port is port 80.
Create a new main.go file with the following content:

package mainimport (
    "fmt"
    "log"
    "net/http")func sayHello(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "hello world")}func main() {
    http.HandleFunc("/", sayHello)//注册URI路径与相应的处理函数
    log.Println("【默认项目】服务启动成功 监听端口 80")
    er := http.ListenAndServe("0.0.0.0:80", nil)
    if er != nil {
        log.Fatal("ListenAndServe: ", er)
    }}
Copy after login

3. Compile it into a program package under linux

I developed it on a mac and need Using go cross-compilation, if you are not familiar with cross-compilation, you can check the documentation, or directly copy my command below to compile.
We want to run it in Docker. The basic golang image is running, so it needs to be compiled into a program compatible with the i386 processor.

sudo env GOOS=linux GOARCH=386 go build main.go
Copy after login

After this compilation is completed, there will be one more main program locally. Just leave it alone for the time being.

4. Use Dockerfile to customize the image of our go program

Create a new folder, create a new Dockerfile file in it, and then create a new# in it ##app, script two files. Put the main program in the previous step into the app folder, and create a new build.sh script file in script. The content of the file Don’t worry about it for now, I’ll talk about it later. The specific file structure is as follows.

.
├── Dockerfile
├── app
│   └── main
└── script
    └── build.sh
Copy after login

The following is the content of the

Dockerfile file. I will code the content first:

FROM golang
MAINTAINER  青羽
WORKDIR /go/src/COPY . .EXPOSE 80CMD ["/bin/bash", "/go/src/script/build.sh"]
Copy after login

Here is the explanation:

  • FROM is the image from which it is integrated. Our go program officially provides an image like golang, which we can use directly.
  • MAINTAINER is to maintain this name.
  • WORKDIR Working directory.
  • COPY This is a copy command that copies all local files to the working directory.
  • EXPOSE This is the port developed by the other party. By default, I open port 80. This can be modified according to the actual situation.
  • CMD Execute a parameter with The command I wrote like this is to execute the script script/build.sh when the image starts. This script contains the command to start the go program. Here I paste the content:
  • #!/usr/bin/env bash
    cd /go/src/app/ && ./main
    Copy after login
Just these two lines.

5. Compile our own image

This belongs to the knowledge of

Docker, I will paste the command.

docker build -t go-web .
Copy after login

    This command is executed. If the
  • golang image is not available locally, it will first go to the official image library to pull the image and then compile it. We can just wait for him quietly. .
  • go-webThis parameter is the name of your last compiled image. You can modify it at will, or add a version number, such as: go-web:v1.

Deploy go projects based on Docker images (detailed examples)

When you see the above output, it means that the compilation is successful, and there is a file named

go-web## in your local image. # mirror. You can use docker images to query:

Deploy go projects based on Docker images (detailed examples) 6. Write the

docker-compose.yml

file to This is our last step. If we use the

go-web

we just compiled to run our go program: <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>version: &amp;#39;2&amp;#39;networks: basic:services: world: container_name: world image: go-web ports: - &quot;8099:80&quot; volumes: - ./app/go/world:/go/src/app:rw networks: - basic</pre><div class="contentsignin">Copy after login</div></div>At this point our orchestration file has been written, and now we only need We need to use

docker-compose

to start our orchestration file. The startup command is as follows: <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>docker-compose -f docker-compose.yml up -d world</pre><div class="contentsignin">Copy after login</div></div> If the following prompt is output, it means the startup is successful.

Creating world ... done
Copy after login

After the startup is successful, you can use

docker ps
Copy after login

to check whether the startup is successful.

Now visit

http://127.0.0.1:8099

to access our go program. Recommended learning: "

docker video tutorial

"

The above is the detailed content of Deploy go projects based on Docker images (detailed examples). For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to update the image of docker How to update the image of docker Apr 15, 2025 pm 12:03 PM

The steps to update a Docker image are as follows: Pull the latest image tag New image Delete the old image for a specific tag (optional) Restart the container (if needed)

How to use docker desktop How to use docker desktop Apr 15, 2025 am 11:45 AM

How to use Docker Desktop? Docker Desktop is a tool for running Docker containers on local machines. The steps to use include: 1. Install Docker Desktop; 2. Start Docker Desktop; 3. Create Docker image (using Dockerfile); 4. Build Docker image (using docker build); 5. Run Docker container (using docker run).

How to create a mirror in docker How to create a mirror in docker Apr 15, 2025 am 11:27 AM

Steps to create a Docker image: Write a Dockerfile that contains the build instructions. Build the image in the terminal, using the docker build command. Tag the image and assign names and tags using the docker tag command.

What to do if the docker image fails What to do if the docker image fails Apr 15, 2025 am 11:21 AM

Troubleshooting steps for failed Docker image build: Check Dockerfile syntax and dependency version. Check if the build context contains the required source code and dependencies. View the build log for error details. Use the --target option to build a hierarchical phase to identify failure points. Make sure to use the latest version of Docker engine. Build the image with --t [image-name]:debug mode to debug the problem. Check disk space and make sure it is sufficient. Disable SELinux to prevent interference with the build process. Ask community platforms for help, provide Dockerfiles and build log descriptions for more specific suggestions.

How to save docker image How to save docker image Apr 15, 2025 am 11:54 AM

To save the image in Docker, you can use the docker commit command to create a new image, containing the current state of the specified container, syntax: docker commit [Options] Container ID Image name. To save the image to the repository, you can use the docker push command, syntax: docker push image name [: tag]. To import saved images, you can use the docker pull command, syntax: docker pull image name [: tag].

How to exit the container by docker How to exit the container by docker Apr 15, 2025 pm 12:15 PM

Four ways to exit Docker container: Use Ctrl D in the container terminal Enter exit command in the container terminal Use docker stop &lt;container_name&gt; Command Use docker kill &lt;container_name&gt; command in the host terminal (force exit)

How to copy files in docker to outside How to copy files in docker to outside Apr 15, 2025 pm 12:12 PM

Methods for copying files to external hosts in Docker: Use the docker cp command: Execute docker cp [Options] &lt;Container Path&gt; &lt;Host Path&gt;. Using data volumes: Create a directory on the host, and use the -v parameter to mount the directory into the container when creating the container to achieve bidirectional file synchronization.

How to check the name of the docker container How to check the name of the docker container Apr 15, 2025 pm 12:21 PM

You can query the Docker container name by following the steps: List all containers (docker ps). Filter the container list (using the grep command). Gets the container name (located in the "NAMES" column).

See all articles