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

Build a Scala environment under Linux and write a simple Scala program

It is very simple to install Scala environment in...

Use HTML to write a simple email template

Today, I want to write about a "low-tech&quo...

Detailed discussion of several methods for deduplicating JavaScript arrays

Table of contents 1. Set Deduplication 2. Double ...

Analysis and solution of MySQL connection throwing Authentication Failed error

[Problem description] On the application side, th...

Example code for hiding element scrollbars using CSS

How can I hide the scrollbars while still being a...

Detailed explanation of transaction isolation levels in MySql study notes

background When we talk about transactions, every...

Write a formal blog using XHTML CSS

The full name of Blog should be Web log, which mea...

HTML tag overflow processing application

Use CSS to modify scroll bars 1. Overflow setting...

A brief summary of basic web page performance optimization rules

Some optimization rules for browser web pages Pag...

Browser compatibility summary of common CSS properties (recommended)

Why do we need to summarize the browser compatibi...

Perfect solution to the problem of webpack packaging css background image path

Inside the style tag of the vue component, there ...

Table paging function implemented by Vue2.0+ElementUI+PageHelper

Preface I have been working on some front-end pro...

Summary of common Linux distribution mirror source configuration

I have been researching Linux recently and tried ...

How to embed flash video format (flv, swf) files in html files

Flash file formats: .FLV and .SWF There are two ex...