Docker基础知识 (28) - 在 Dockerfile 中以 scratch 为基础镜像 (FROM scratch)
1. 创建基于静态编译的 C 程序镜像
本文在 Ubuntu 20.04 下创建基于静态编译的 C 程序镜像。
Docker 版本: 20.10.7
Docker Compose 版本: 2.6.1
#include <stdio.h>
int main() {
puts("Hello World!- C");
return 0;
}
Step 1/3 : FROM scratch
--->
Step 2/3 : COPY hello /
---> bb893abeef08
Step 3/3 : CMD ["/hello"]
---> Running in c31e62693472
Removing intermediate container c31e62693472
---> cebea71dcbe0
Successfully built cebea71dcbe0
Successfully tagged hello:1.0
$ docker images
2. 创建基于编译的 Go 程序镜像
本文在 Ubuntu 20.04 下创建基于编译的 Go 程序镜像。
Docker 版本: 20.10.7
Docker Compose 版本: 2.6.1
1) Go 程序
package main import "fmt" func main() { fmt.Println("Hello world - Go") }
# 编译
Step 1/3 : FROM scratch
--->
Step 2/3 : COPY test /
---> cd67f4bfb544
Step 3/3 : CMD ["/test"]
---> Running in c3cf81ea01e4
Removing intermediate container c3cf81ea01e4
---> 535665c081c8
Successfully built 535665c081c8
Successfully tagged test:1.0
$ docker images
3. 创建基于 Debian rootfs 的 Linux 镜像
Debian:https://www.debian.org/
Docker Debain: https://docker.debian.net/
Docker Debain GitHub: https://github.com/debuerreotype/docker-debian-artifacts
本文在 Ubuntu 20.04 下创建基于 Debian rootfs 的 Linux 镜像。
Docker 版本: 20.10.7
Docker Compose 版本: 2.6.1
FROM scratch
Add rootfs.tar.xz /
WORKDIR /home/docker
CMD /bin/bash
Step 1/4 : FROM scratch
--->
Step 2/4 : Add rootfs.tar.xz /
---> 806b049c2199
Step 3/4 : WORKDIR /home/docker
---> Running in e4c0defe9fd3
Removing intermediate container e4c0defe9fd3
---> a2bea1387c68
Step 4/4 : CMD /bin/bash
---> Running in ea20508cb334
Removing intermediate container ea20508cb334
---> fd4fa7caba5d
Successfully built fd4fa7caba5d
Successfully tagged debian_local:1.0
root@0e6e3704225c:/home/docker# cd /
root@0e6e3704225c:/# ls
bin boot dev etc home lib lib32 lib64 libx32 media mnt opt proc root run sbin srv sys tmp usr var
root@0e6e3704225c:/# cat /etc/issue
Debian GNU/Linux bookworm/sid \n \l
4. 创建基于 CentOS rootfs 的 Linux 镜像
CentOS:https://www.centos.org/
CentOS Vault Mirror: https://vault.centos.org/
本文在 CentOS 7.9 下创建基于 CentOS rootfs 的 Linux 镜像。
Docker 版本: 20.10.7
Docker Compose 版本: 2.6.1
1) 制作 CentOS 7.5 rootfs
$ mkdir -p ~/centos75/rootfs
$ cd ~/centos75
$ rpm --root /home/xxx/centos75/rootfs --initdb # 设置 rpm 操作的根目录
# 下载
$ wget https://vault.centos.org/7.5.1804/os/x86_64/Packages/centos-release-7-5.1804.el7.centos.x86_64.rpm
$ sudo rpm -ivh --nodeps --root /home/xxx/centos75/rootfs --package ./centos-release-7-5.1804.el7.centos.x86_64.rpm
warning: ./centos-release-7-5.1804.el7.centos.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY
Preparing... ################################# [100%]
Updating / installing...
1:centos-release-7-5.1804.el7.cento################################# [100%]
warning: %post(centos-release-7-5.1804.el7.centos.x86_64) scriptlet failed, exit status 127
$ sudo yum --installroot=/home/xxx/centos75/rootfs install yum --nogpgcheck
...
$ ls ./rootfs
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
2) 创建 Dockerfile
$ cd ~/centos75
$ vim Dockerfile
FROM scratch
Add ./rootfs /
WORKDIR /home/docker
CMD /bin/bash
3) 创建 Linux 镜像,并运行容器
$ cd ~/centos75
# 创建镜像
$ sudo docker build -t centos_local:7.5 .
[+] Building 10.1s (6/6) FINISHED
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 101B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load build context 4.4s
=> => transferring context: 453.26MB 4.4s
=> [1/2] ADD ./rootfs / 2.2s
=> [2/2] WORKDIR /home/docker 0.3s
=> exporting to image 3.1s
=> => exporting layers 3.1s
=> => writing image sha256:72826446cccfd5363073ca1923c59cabf843278aa82d20ad720ac2d1cf458333 0.0s
=> => naming to docker.io/library/centos_local:7.5 0.0s
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos_local 7.5 72826446cccf 4 minutes ago 452MB
$ docker run -itd --name centos-local-1.0 centos_local:7.5
3223f140df4d26e951dadf2d938fcc561f78ac7de86ca469e6c60853a03162fd
$ docker exec -it centos-local-1.0 /bin/bash
bash-4.2# cd /
bash-4.2# ls
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
bash-4.2# cat /etc/centos-release
CentOS Linux release 7.5.1804 (Core)
浙公网安备 33010602011771号