docker 构建最小镜像 - 2MB 不到
文章目录
- 1.编译 code
- 2.编写 Dockerfile
- 3.构建镜像
- 4.运行起来
- 5.总结
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#