生成容器镜像的几种方式

commit

commit可能是最简单直白的方法。

  1. 运行一个容器
1
2
3
4
5
6
7
8
$ docker run \
-it \
-u 1000 \
-h foo-host \
-v foo-tmp-volume:/workspaces/tmp \
-w /home/ubuntu \
--name foo-ubuntu \
myubuntu:latest
  1. commit从容器创建新镜像
1
2
3
4
5
6
$ docker commit \
-a "Bill Dong" \
-c "CMD [ \"/bin/bash\" ]" \
-c "LABEL foo=bar" \
-m "foo-ubuntu-image init" \
foo-ubuntu foo-ubuntu-image:latest

save / load

save将镜像保存至tarball,包括元数据;load将tarball装载成镜像。

export / import

export将容器的文件系统导出至tarball,不包括元数据;import将tarball导入成文件系统镜像。

注意:该操作可以将多层文件系统变成一层;导入时可能会丢失CMD之类的信息,使用-c选项设置。

build

build是推荐的方法。

  1. 写一个Dockerfile
1
2
# FROM scratch
FROM myubuntu:latest
  1. 构建镜像
1
$ docker build .