感受docker build过程

这篇日志只是让你感受下制作镜像的过程,并不是详细介绍,后面有专门的文档来介绍制作镜像的各种办法。

docker build 命令用于使用 Dockerfile 创建镜像。

语法

docker build [OPTIONS] PATH | URL | -

OPTIONS说明:

  • –build-arg=[] :设置镜像创建时的变量;
  • –cpu-shares :设置 cpu 使用权重;
  • –cpu-period :限制 CPU CFS周期;
  • –cpu-quota :限制 CPU CFS配额;
  • –cpuset-cpus :指定使用的CPU id;
  • –cpuset-mems :指定使用的内存 id;
  • –disable-content-trust :忽略校验,默认开启;
  • -f :指定要使用的Dockerfile路径;
  • –force-rm :设置镜像过程中删除中间容器;
  • –isolation :使用容器隔离技术;
  • –label=[] :设置镜像使用的元数据;
  • -m :设置内存最大值;
  • –memory-swap :设置Swap的最大值为内存+swap,”-1″表示不限swap;
  • –no-cache :创建镜像的过程不使用缓存;
  • –pull :尝试去更新镜像的新版本;
  • –quiet, -q :安静模式,成功后只输出镜像 ID;
  • –rm :设置镜像成功后删除中间容器;
  • –shm-size :设置/dev/shm的大小,默认值是64M;
  • –ulimit :Ulimit配置。
  • –tag, -t: 镜像的名字及标签,通常 name:tag 或者 name 格式;可以在一次构建中为一个镜像设置多个标签。
  • –network: 默认 default。在构建期间设置RUN指令的网络模式

示例:做个简单的mysql-client,你可以理解成把mysql-client封装到docker里面的方法

先写个Dockerfile

[[email protected] dockerfile]# vim Dockerfile 
[[email protected] dockerfile]# cat Dockerfile 
FROM alpine:3.1
RUN apk add --update mysql-client && rm -rf /var/cache/apk/*
ENTRYPOINT ["mysql"]

然后执行创建命令

[[email protected] dockerfile]# docker build -t mysql-client .
Sending build context to Docker daemon 2.048kB
Step 1/3 : FROM alpine:3.1
---> a1038a41fe2b
Step 2/3 : RUN apk add --update mysql-client && rm -rf /var/cache/apk/*
---> Running in 858db1c410a7
fetch http://dl-cdn.alpinelinux.org/alpine/v3.1/main/x86_64/APKINDEX.tar.gz
(1/4) Installing mysql-common (5.5.44-r0)
(2/4) Installing ncurses-terminfo-base (5.9-r3)
(3/4) Installing ncurses-libs (5.9-r3)
(4/4) Installing mysql-client (5.5.44-r0)
Executing busybox-1.22.1-r15.trigger
OK: 30 MiB in 19 packages
Removing intermediate container 858db1c410a7
---> 9db8c24f2c24
Step 3/3 : ENTRYPOINT ["mysql"]
---> Running in 870c91336bf1
Removing intermediate container 870c91336bf1
---> 93d76ebc1d92
Successfully built 93d76ebc1d92
Successfully tagged mysql-client:latest
[[email protected] dockerfile]# 
Message from [email protected] at Jul 5 09:19:29 ...
kernel:unregister_netdevice: waiting for lo to become free. Usage count = 1

查看一下刚才创建的镜像,可以看到,封装出来才30M左右

[[email protected] dockerfile]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql-client latest 93d76ebc1d92 7 minutes ago 30.3MB
alpine latest 4d90542f0623 2 weeks ago 5.58MB
ubuntu 14.04 2c5e00d77a67 7 weeks ago 188MB
registry 2 f32a97de94e1 3 months ago 25.8MB
alpine 3.1 a1038a41fe2b 5 months ago 5.05MB
hello-world latest fce289e99eb9 6 months ago 1.84kB

注意观察日志输出:

2019-07-05T09:15:35.581089641+08:00 container create 858db1c410a73253059bdef7faaa0bf5827ab98e5b3a68030de5ab3086e9c679 (image=sha256:a1038a41fe2b75d8c53d0a4d22207e4e7f72e95a11da4d20424f0062b239b67f, name=condescending_feistel)
2019-07-05T09:15:35.581221519+08:00 container attach 858db1c410a73253059bdef7faaa0bf5827ab98e5b3a68030de5ab3086e9c679 (image=sha256:a1038a41fe2b75d8c53d0a4d22207e4e7f72e95a11da4d20424f0062b239b67f, name=condescending_feistel)
2019-07-05T09:15:35.623012360+08:00 network connect 05475ea368337254dea611bfff0a86005befb5ea3da6e3bb6b7b3d40bbe4e33e (container=858db1c410a73253059bdef7faaa0bf5827ab98e5b3a68030de5ab3086e9c679, name=bridge, type=bridge)
2019-07-05T09:15:36.453502785+08:00 container start 858db1c410a73253059bdef7faaa0bf5827ab98e5b3a68030de5ab3086e9c679 (image=sha256:a1038a41fe2b75d8c53d0a4d22207e4e7f72e95a11da4d20424f0062b239b67f, name=condescending_feistel)
2019-07-05T09:16:45.084592028+08:00 container die 858db1c410a73253059bdef7faaa0bf5827ab98e5b3a68030de5ab3086e9c679 (exitCode=0, image=sha256:a1038a41fe2b75d8c53d0a4d22207e4e7f72e95a11da4d20424f0062b239b67f, name=condescending_feistel)
2019-07-05T09:16:45.148975295+08:00 network disconnect 05475ea368337254dea611bfff0a86005befb5ea3da6e3bb6b7b3d40bbe4e33e (container=858db1c410a73253059bdef7faaa0bf5827ab98e5b3a68030de5ab3086e9c679, name=bridge, type=bridge)
2019-07-05T09:16:45.480769746+08:00 container destroy 858db1c410a73253059bdef7faaa0bf5827ab98e5b3a68030de5ab3086e9c679 (image=sha256:a1038a41fe2b75d8c53d0a4d22207e4e7f72e95a11da4d20424f0062b239b67f, name=condescending_feistel)
2019-07-05T09:16:45.533218805+08:00 container create 870c91336bf1c010b9094015c73a48f13622736f534e0190cc1aaa30bde36fc9 (image=sha256:9db8c24f2c243ca6b454750f14842893cc76ef259ee121f50f4e6c2f1e72e250, name=priceless_keldysh)
2019-07-05T09:16:45.622090501+08:00 container destroy 870c91336bf1c010b9094015c73a48f13622736f534e0190cc1aaa30bde36fc9 (image=sha256:9db8c24f2c243ca6b454750f14842893cc76ef259ee121f50f4e6c2f1e72e250, name=priceless_keldysh)
2019-07-05T09:16:45.626378980+08:00 image tag sha256:93d76ebc1d92b961dad408a98df49bb20e70dffe311676763beb9f7dffc08eeb (name=mysql-client:latest)

Message from [email protected] at Jul 5 09:19:29 ...
kernel:unregister_netdevice: waiting for lo to become free. Usage count = 1
此条目发表在Docker分类目录,贴了标签。将固定链接加入收藏夹。

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注