Implementation of two basic images for Docker deployment of Go

Implementation of two basic images for Docker deployment of Go

1. golang:latest base image

mkdir gotest
touch main.go
touch Dockerfile

1. Example code

package main

import (
    "fmt"
    "log"
    "net/http"
)

func main() {
    http.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
        fmt.Fprint(writer, "Hello World")
    })
    fmt.Println("3000!!")
    log.Fatal(http.ListenAndServe(":3000", nil))
}

2. Dockerfile configuration

#Source image FROM golang:latest
#Set the working directory WORKDIR $GOPATH/src/github.com/gotest
#Add the server's go project code to the docker container ADD . $GOPATH/src/github.com/gotest
#go build executable file RUN go build .
#Expose port EXPOSE 3000
#Finally run the docker command ENTRYPOINT ["./gotest"]

3. Packaging the image

docker build -t gotest .
  • The golang:latest compilation process actually builds a go development environment in the container.
  • This source image package is about 800M, which is relatively large.

2. alpine:latest base image

  1. The general process of using this image is to package the go program into a binary file on the Linux machine, then drop it into the apine environment and execute the compiled file.
  2. By default, Go's runtime environment variable CGO_ENABLED=1, which means cgo is started by default, allowing you to call C code from Go code. CGO can be disabled by setting CGO_ENABLED=0. So you need to execute: CGO_ENABLED=0 go build .
  3. This base image is only 13M in size, which is very small.
#Source image FROM alpine:latest
#Set the working directory WORKDIR $GOPATH/src/github.com/common
#Add the server's go project code to the docker container ADD . $GOPATH/src/github.com/common
#Expose port EXPOSE 3002
#Finally run the docker command ENTRYPOINT ["./common"]

Packaging image

docker build -t common .

This is the end of this article about the implementation of two basic images for Docker deployment of Go. For more relevant content about Docker deployment of Go images, 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:
  • Methods and steps for deploying go projects based on Docker images

<<:  Vue implements the magnifying glass effect of tab switching

>>:  Vue Basics Introduction: Vuex Installation and Use

Recommend

Two ways to export csv in win10 mysql

There are two ways to export csv in win10. The fi...

Two ways to install Python3 on Linux servers

First method Alibaba Cloud and Baidu Cloud server...

MySQL SQL statement performance tuning simple example

MySQL SQL statement performance tuning simple exa...

JS implements a simple todoList (notepad) effect

The notepad program is implemented using the thre...

How to find out uncommitted transaction information in MySQL

A while ago, I wrote a blog post titled "Can...

Detailed configuration of wireless network card under Ubuntu Server

1. Insert the wireless network card and use the c...

MySQL's conceptual understanding of various locks

Optimistic Locking Optimistic locking is mostly i...

WeChat applet implements fixed header and list table components

Table of contents need: Function Points Rendering...

MySQL-8.0.26 Configuration Graphics Tutorial

Preface: Recently, the company project changed th...

HTML insert image example (html add image)

Inserting images into HTML requires HTML tags to ...

Linux installation MySQL tutorial (binary distribution)

This tutorial shares the detailed steps of instal...

How to develop uniapp using vscode

Because I have always used vscode to develop fron...