Methods and steps for deploying go projects based on Docker images

Methods and steps for deploying go projects based on Docker images

Dependence on knowledge

  • Go cross-compilation basics
  • Docker Basics
  • Dockerfile custom image basics
  • Docker-compose orchestration file writing basics

Of course, you can also follow these steps to complete the deployment even if you don't know anything about it. However, if there are some small problems in the middle, you may not know how to solve them. Of course, you can also leave a message.

I developed and tested it on a Mac. If you are on Windows, there may be some differences, but it shouldn't be a big problem.

1. Dependence on the environment

  • Docker

2. Write a GoLang web program

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

package main
import (
  "fmt"
  "log"
  "net/http"
)
func sayHello(w http.ResponseWriter, r *http.Request) {
  fmt.Fprintf(w, "hello world")
}
func main() {
  http.HandleFunc("/", sayHello)//Register URI path and corresponding processing function log.Println("[Default project] Service started successfully listening to port 80")
  er := http.ListenAndServe("0.0.0.0:80", nil)
  if er != nil {
    log.Fatal("ListenAndServe: ", er)
  }
}

3. Compile into a package under Linux

I developed it on a Mac and needed to use cross-compilation of go. If you are not familiar with cross-compilation, you can check the documentation or just copy the command below to compile.
We want to run it in Docker , based on the golang image, so we need to compile it into a program compatible with the i386 processor.

sudo env GOOS=linux GOARCH=386 go build main.go

After the compilation is completed, there will be an additional main program locally. For now, just ignore it and keep it as a backup.

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 two new files, app and script , in it. Put main program in the previous step into the app folder, and create a build.sh script file in script . Ignore the content of the file for now, I will explain it later.
The specific file structure is as follows.

.
├── Dockerfile
├── app
│ └── main
└── script
  └── build.sh

The following is to write the contents of the Dockerfile file. I will first post the code:

FROM golang
MAINTAINER Qianyi WORKDIR /go/src/
COPY . .
EXPOSE 80
CMD ["/bin/bash", "/go/src/script/build.sh"]

Here is the explanation:

FROM is the image from which it is integrated. Our go program officially provides a golang image, which we can use directly.

MAINTAINER is the name of maintenance.

WORKDIR Working directory.

COPY is a copy command that copies all local files to the working directory.

EXPOSE This is the port developed by the other party. I open port 80 by default. You can modify it according to the actual situation.

CMD executes a command with parameters. I wrote it this way so that script/build.sh script will be executed when the image is started. This script contains the command to start the go program.

I paste the content here:

#!/usr/bin/env bash
cd /go/src/app/ && ./main

Just these two lines.

5. Compile our own image

This belongs to Docker knowledge, I paste the command out.

docker build -t go-web .
  • When this command is executed, if there is no golang image locally, it will first go to the official image library to pull the image and then compile it. We just need to wait for it quietly.
  • go-web parameter is the name of the image you compiled. You can modify it at will, and you can also add a version number, such as go-web:v1 .

Seeing the above output, it means that the compilation is successful, and there is an image named go-web in your local image. You can use docker images to query:

6. Write the docker-compose.yml file

This is our last step. If we use go-web we just compiled to run our go program:

version: '2'
networks:
 basic:
services:
 world:
  container_name: world
  image: go-web
  ports:
   - "8099:80"
  volumes:
   - ./app/go/world:/go/src/app:rw
  networks:
   - basic

At this point, our orchestration file has been written. Now we just need to use docker-compose to start our orchestration file. The startup command is as follows:

docker-compose -f docker-compose.yml up -d world

If the following prompt is output, it means the startup is successful.

Creating world ... done

After successful startup, you can use

docker ps

Let's check whether the startup is successful.

Now we can access our go program by visiting http://127.0.0.1:8099 .

This is the end of this article about the steps to deploy a go project based on a Docker image. For more information about deploying a go project with a Docker image, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Implementation of two basic images for Docker deployment of Go

<<:  Steps to modify the MySQL database data file path under Linux

>>:  Native js imitates mobile phone pull-down refresh

Recommend

Steps to package and release the Vue project

Table of contents 1. Transition from development ...

Briefly understand the two common methods of creating files in Linux terminal

We all know that we can use the mkdir command to ...

How to build YUM in Centos7 environment

1. Enter the configuration file of the yum source...

js implements custom drop-down box

This article example shares the specific code of ...

CSS pseudo-class: empty makes me shine (example code)

Anyone who has read my articles recently knows th...

How to use Linux locate command

01. Command Overview The locate command is actual...

How to configure eureka in docker

eureka: 1. Build a JDK image Start the eureka con...

How React Hooks Work

Table of contents 1. React Hooks vs. Pure Functio...

What you need to know about responsive design

Responsive design is to perform corresponding ope...

Summary of Css methods for clearing floats

Float is often used in web page layout, but the f...

Detailed explanation of the Sidecar mode in Docker Compose

Table of contents What is Docker Compose Requirem...

Installation and deployment of MySQL Router

Table of contents 01 Introduction to MySQL Router...