Deploy go projects based on Docker images (detailed examples)

WBOY
Release: 2022-06-08 20:52:32
forward
4836 people have browsed it

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:sql;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:sql;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!

Related labels:
source:csdn.net
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template