Docker multi-stage build image

Overview

Using docker's multi-stage mirroring mechanism can greatly reduce the volume of the final image built, thereby facilitating transmission

For example, we use the go language to build programs, and some dependencies such as go need to be installed in the compilation environment, and the built binary files only need to be run in a small Linux container.

Environmental information

CentOS Linux release 7.9.2009 (Core)
go version go1.15.5 linux/amd64
Docker version 18.09.9, build 039a7df9ba

Prepare the environment

#1 安装docker
https://blog.csdn.net/xys2015/article/details/109370082

#2 安装go
https://blog.csdn.net/xys2015/article/details/113770562

Build a simple Go HTTP Server on the host

1 goal

  • Use Go to compile a program
  • Run the program to monitor port 8180
  • Request localhost:8180/xxx, return Hello, you requested: /xxx
  • Log the requested log to standard output

2 write code

package main

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

// HelloServer responds to requests with the given URL path.
func HelloServer(w http.ResponseWriter, r *http.Request) {
    
    
    fmt.Fprintf(w, "Hello, you requested: %s\n", r.URL.Path)
    log.Printf("Received request for path: %s", r.URL.Path)
}

func main() {
    
    
    var addr string = ":8180"
    handler := http.HandlerFunc(HelloServer)
    if err := http.ListenAndServe(addr, handler); err != nil {
    
    
        log.Fatalf("Could not listen on port %s %v", addr, err)
    }
}

3 compile

go build hello.go

3 run

./hello

4 visits

[root@192_168_31_100 ~]# curl localhost:8180/xxx
Hello, you requested: /xxx
[root@192_168_31_100 ~]# curl localhost:8180/hello
Hello, you requested: /hello
[root@192_168_31_100 ~]# curl localhost:8180/world
Hello, you requested: /world

5 View the output log

[root@192_168_31_100 /data/gitee-dld/hello-go/http-server]# ./hello 
2021/02/09 15:26:32 Received request for path: /xxx
2021/02/09 15:26:35 Received request for path: /hello
2021/02/09 15:26:39 Received request for path: /world

6 End the program

CTRL + C

Reference address https://gitee.com/as4k/gitee-dld/tree/master/hello-go/http-server

Put the above program into the container to run

1 write Dockerfile

cat Dockerfile
FROM golang:1-alpine as build
# golang:1-alpine 这个标签的含义是 最新1.x版本,基础镜像是 alpine linux

WORKDIR /app
COPY http-server http-server
RUN go build /app/http-server/hello.go
#这里生成的二进制文件会放在 /app/hello

FROM alpine:latest

WORKDIR /app
COPY --from=build /app/hello /app/hello
#--from=build 这里即用到第一阶段的镜像

EXPOSE 8180
ENTRYPOINT ["./hello"]

2 build image

#处在Dockerfile所在目录下
docker build -t hello-go:v1 .

3 run the container

docker run -d --name hello-go --rm -p 8180:8180 hello-go:v1

4 access to the container

curl localhost:8180/hello-world

5 Related output information

5.1 镜像大小对比
====================
# docker images | egrep "hello|golang"
hello-go                 v1                  abe1c3a706e0        About a minute ago   12.1MB
golang                   1-alpine            1463476d8605        7 weeks ago          299MB
可以看到,节省了不少磁盘空间,如果go程序有大量依赖,效果更明显

5.2 容器运行信息和访问信息
====================
[root@192_168_31_100 /data/gitee-dld/hello-go]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                    NAMES
e964f390ac91        hello-go:v1         "./hello"           2 seconds ago       Up 1 second         0.0.0.0:8180->8180/tcp   hello-go
[root@192_168_31_100 /data/gitee-dld/hello-go]# curl localhost:8180/hello-world
Hello, you requested: /hello-world

Reference address https://gitee.com/as4k/gitee-dld/tree/master/hello-go

Guess you like

Origin blog.csdn.net/xys2015/article/details/113771678