1.编译 code

root@jn:/home/jn/Desktop/Docker# cat hello.go
package main

import (
        "fmt"
        "time"
)

func main() {
        for {
                fmt.Println("hello~")
                time.Sleep(time.Second)
        }
}
root@jn:/home/jn/Desktop/Docker# go build hello.go
root@jn:/home/jn/Desktop/Docker# ll -h hello
-rwxrwxr-x 1 jn jn 1.7M Sep   5 02:32 hello*
root@jn:/home/jn/Desktop/Docker#

2.编写 Dockerfile

Dockerfile:

FROM scratch
COPY hello /
CMD ["/hello"]

3.构建镜像

docker build -t hello .

root@jn:/home/jn/Desktop/Docker# docker build -t hello .
[+] Building 0.0s (5/5) FINISHED                                                                                                                                                                           docker:default
 => [internal] load build definition from Dockerfile                                                                                                                                                                 0.0s
 => => transferring dockerfile: 79B                                                                                                                                                                                  0.0s
 => [internal] load .dockerignore                                                                                                                                                                                    0.0s
 => => transferring context: 2B                                                                                                                                                                                      0.0s
 => [internal] load build context                                                                                                                                                                                    0.0s
 => => transferring context: 1.76MB                                                                                                                                                                                  0.0s
 => CACHED [1/1] COPY hello /                                                                                                                                                                                        0.0s
 => exporting to image                                                                                                                                                                                               0.0s
 => => exporting layers                                                                                                                                                                                              0.0s
 => => writing image sha256:4182d38fc8c5abca0a807ff9b39864a960c97b4a9e03363d79f8461e81e190d4                                                                                                                         0.0s
 => => naming to docker.io/library/hello                                                                                                                                                                             0.0s
root@jn:/home/jn/Desktop/Docker#

结果:

root@jn:/home/jn/Desktop/Docker# docker images
REPOSITORY              TAG       IMAGE ID       CREATED         SIZE
hello                   latest    4182d38fc8c5   5 minutes ago   1.76MB
root@jn:/home/jn/Desktop/Docker#

4.运行起来

root@jn:/home/jn/Desktop/Docker# docker run hello
hello~
hello~
hello~
hello~
hello~

5.总结

(镜像总共1.76MB,程序占1.7MB)

root@jn:/home/jn/Desktop/Docker# ll -h hello
-rwxrwxr-x 1 jn jn 1.7M Sep   5 02:32 hello*
root@jn:/home/jn/Desktop/Docker#
root@jn:/home/jn/Desktop/Docker# docker images
REPOSITORY              TAG       IMAGE ID       CREATED         SIZE
hello                   latest    4182d38fc8c5   9 minutes ago   1.76MB
root@jn:/home/jn/Desktop/Docker#

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。