构建本地的容器镜像,目的是:
- 对容器的修改和复用
- 自定义镜像
- 打包,分发服务以及运行环境
根据上节内容 Docker容器内部署静态网站 的容器来演示。
构建镜像基本操作
docker commit // 通过容器构建
docker build // 通过Dockerfile 文件构建
查看容器
hejing@learning:~$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e6bb5696e362 ubuntu "/bin/bash" 42 hours ago Exited (0) 41 hours ago web
61ab2ec20acd ubuntu "/bin/bash" 44 hours ago Exited (0) 55 minutes ago mydocker1
查看镜像
hejing@learning:~$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest fb14c85f88d3 8 days ago 109 MB
hejing@learning:~$
当前只有一个镜像。
commit 用法
Usage: docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
Create a new image from a container's changes
Options:
-a, --author string Author (e.g., "John Hannibal Smith <hannibal@a-team.com>")
-c, --change list Apply Dockerfile instruction to the created image (default [])
--help Print usage
-m, --message string Commit message
-p, --pause Pause container during commit (default true)
以 容器 web 为例 构建新的 image
hejing@learning:~$ docker commit -a "2010jing" -m "test image" web 2010jing/web:latest
sha256:d8ed8044f72c5c0790b7afdc1d090522196ed744e14052ef328eab4a7e5fafef
hejing@learning:~$
再查看 image
hejing@learning:~$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
2010jing/web latest d8ed8044f72c 21 seconds ago 241 MB
ubuntu latest fb14c85f88d3 8 days ago 109 MB
hejing@learning:~$
发现多了一个新的镜像,本地仓库为 2010jing/web
推送到 hub.docker.com 之前请自行先注册账号。

hub.docker.com
先登录 hub.docker.com
hejing@learning:~$ docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: 2010jing
Password:
Login Succeeded
hejing@learning:~$
推送到 hub.docker.com
hejing@learning:~$ docker push 2010jing/web:latest
The push refers to a repository [docker.io/2010jing/web]
508465605fca: Pushed
cb3f1f28f7cc: Pushed
4b4853bba079: Pushed
70636f56b400: Pushed
9cb0fb25c646: Pushed
820f0ba55685: Pushed
latest: digest: sha256:dd4f115093c38ffdd7bdf35c351339cdab716c9edadf6c659ee776a59305ed84 size: 1569
hejing@learning:~$
查看网页

image.png
已经成功推送。
从自己的仓库中获取镜像
hejing@learning:~$ docker pull 2010jing/web
Using default tag: latest
latest: Pulling from 2010jing/web
6ae1cb217992: Already exists
9997953254b2: Already exists
c96150385f96: Already exists
42eca8881e4c: Already exists
fcd269a640b4: Already exists
a02058542622: Pull complete
Digest: sha256:dd4f115093c38ffdd7bdf35c351339cdab716c9edadf6c659ee776a59305ed84
Status: Downloaded newer image for 2010jing/web:latest
Docker 重命名镜像 TAG
Usage: docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]
Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
hejing@learning:~$ docker tag --help
Usage: docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]
Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
Options:
--help Print usage
hejing@learning:~$ docker tag d8ed8044f72c 2010jing/web:local_web
hejing@learning:~$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
2010jing/web local_web d8ed8044f72c 37 minutes ago 241 MB
ubuntu latest fb14c85f88d3 8 days ago 109 MB
hejing@learning:~$
docker build 将在后面章节学习。
docker build
建立目录 /dockerfile/cct/
创建文件 /dockerfile/cct/Dockerfile
Dockerfile 内容如下
hejing@learning:/dockerfile/cct$ cat Dockerfile
#Container connection test
FROM 2010jing/web:20171224
RUN apt install -y iputils-ping
RUN apt install -y net-tools
RUN apt update
RUN apt install -y nginx
RUN apt install -y curl
EXPOSE 80
cmd /bin/bash
hejing@learning:/dockerfile/cct$
docker build 用法
Usage: docker build [OPTIONS] PATH | URL | -
Build an image from a Dockerfile
Options:
--build-arg list Set build-time variables (default [])
--cache-from stringSlice Images to consider as cache sources
--cgroup-parent string Optional parent cgroup for the container
--compress Compress the build context using gzip
--cpu-period int Limit the CPU CFS (Completely Fair Scheduler) period
--cpu-quota int Limit the CPU CFS (Completely Fair Scheduler) quota
-c, --cpu-shares int CPU shares (relative weight)
--cpuset-cpus string CPUs in which to allow execution (0-3, 0,1)
--cpuset-mems string MEMs in which to allow execution (0-3, 0,1)
--disable-content-trust Skip image verification (default true)
-f, --file string Name of the Dockerfile (Default is 'PATH/Dockerfile')
--force-rm Always remove intermediate containers
--help Print usage
--isolation string Container isolation technology
--label list Set metadata for an image (default [])
-m, --memory string Memory limit
--memory-swap string Swap limit equal to memory plus swap: '-1' to enable unlimited swap
--network string Set the networking mode for the RUN instructions during build (default "default")
--no-cache Do not use cache when building the image
--pull Always attempt to pull a newer version of the image
-q, --quiet Suppress the build output and print image ID on success
--rm Remove intermediate containers after a successful build (default true)
--security-opt stringSlice Security options
--shm-size string Size of /dev/shm, default value is 64MB
-t, --tag list Name and optionally a tag in the 'name:tag' format (default [])
--ulimit ulimit Ulimit options (default [])
hejing@learning:/dockerfile/cct$
构建 iamge示例
hejing@learning:/dockerfile/cct$ sudo docker build -t 2010jing/cct .
Sending build context to Docker daemon 2.048 kB
Step 1/8 : FROM 2010jing/web:20171224
---> 9b03d979e5b1
Step 2/8 : RUN apt install -y iputils-ping
---> Running in f086624b49e5
WARNING: apt does not have a stable CLI interface. Use with caution in scripts.
Reading package lists...
Building dependency tree...
Reading state information...
iputils-ping is already the newest version (3:20121221-5ubuntu2).
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
---> f78f62335143
Removing intermediate container f086624b49e5
Step 3/8 : RUN apt install -y net-tools
---> Running in a9dbda8b3aaf
// 省略中间一大部分信息
...
...
Step 8/8 : CMD /bin/bash
---> Running in c094899b42f0
---> 83031701e398
Removing intermediate container c094899b42f0
Successfully built 83031701e398
查看 iamges
hejing@learning:/dockerfile/cct$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
2010jing/cct latest 83031701e398 2 minutes ago 255 MB
2010jing/web 20171224 9b03d979e5b1 15 hours ago 245 MB
2010jing/web latest d6f15d5de749 20 hours ago 241 MB
2010jing/web local_web d8ed8044f72c 21 hours ago 241 MB
ubuntu latest fb14c85f88d3 9 days ago 109 MB
hejing@learning:/dockerfile/cct$
看到 新的 image 在第一行。
在Dockerfile中用到的命令有(摘自网络)
FROM
FROM指定一个基础镜像, 一般情况下一个可用的 Dockerfile一定是 FROM 为第一个指令。至于image则可以是任何合理存在的image镜像。
FROM 一定是首个非注释指令 Dockerfile.
FROM 可以在一个 Dockerfile 中出现多次,以便于创建混合的images。
如果没有指定 tag ,latest 将会被指定为要使用的基础镜像版本。
MAINTAINER
这里是用于指定镜像制作者的信息
RUN
RUN命令将在当前image中执行任意合法命令并提交执行结果。命令执行提交后,就会自动执行Dockerfile中的下一个指令。
层级 RUN 指令和生成提交是符合Docker核心理念的做法。它允许像版本控制那样,在任意一个点,对image 镜像进行定制化构建。
RUN 指令缓存不会在下个命令执行时自动失效。比如 RUN apt-get dist-upgrade -y 的缓存就可能被用于下一个指令. --no-cache 标志可以被用于强制取消缓存使用。
ENV
ENV指令可以用于为docker容器设置环境变量
ENV设置的环境变量,可以使用 docker inspect命令来查看。同时还可以使用docker run --env <key>=<value>来修改环境变量。
USER
USER 用来切换运行属主身份的。Docker 默认是使用 root,但若不需要,建议切换使用者身分,毕竟 root 权限太大了,使用上有安全的风险。
WORKDIR
WORKDIR 用来切换工作目录的。Docker 默认的工作目录是/,只有 RUN 能执行 cd 命令切换目录,而且还只作用在当下下的 RUN,也就是说每一个 RUN 都是独立进行的。如果想让其他指令在指定的目录下执行,就得靠 WORKDIR。WORKDIR 动作的目录改变是持久的,不用每个指令前都使用一次 WORKDIR。
COPY
COPY 将文件从路径 <src> 复制添加到容器内部路径 <dest>。
<src> 必须是想对于源文件夹的一个文件或目录,也可以是一个远程的url,<dest> 是目标容器中的绝对路径。
所有的新文件和文件夹都会创建UID 和 GID 。事实上如果 <src> 是一个远程文件URL,那么目标文件的权限将会是600。
ADD
ADD 将文件从路径 <src> 复制添加到容器内部路径 <dest>。
<src> 必须是想对于源文件夹的一个文件或目录,也可以是一个远程的url。<dest> 是目标容器中的绝对路径。
所有的新文件和文件夹都会创建UID 和 GID。事实上如果 <src> 是一个远程文件URL,那么目标文件的权限将会是600。
VOLUME
创建一个可以从本地主机或其他容器挂载的挂载点,一般用来存放数据库和需要保持的数据等。
EXPOSE
EXPOSE 指令指定在docker允许时指定的端口进行转发。
CMD
Dockerfile.中只能有一个CMD指令。 如果你指定了多个,那么最后个CMD指令是生效的。
CMD指令的主要作用是提供默认的执行容器。这些默认值可以包括可执行文件,也可以省略可执行文件。
当你使用shell或exec格式时, CMD 会自动执行这个命令。
ONBUILD
ONBUILD 的作用就是让指令延迟執行,延迟到下一个使用 FROM 的 Dockerfile 在建立 image 时执行,只限延迟一次。
ONBUILD 的使用情景是在建立镜像时取得最新的源码 (搭配 RUN) 与限定系统框架。
ARG
ARG是Docker1.9 版本才新加入的指令。
ARG 定义的变量只在建立 image 时有效,建立完成后变量就失效消失
LABEL
定义一个 image 标签 Owner,并赋值,其值为变量 Name 的值。(LABEL Owner=$Name )
ENTRYPOINT
是指定 Docker image 运行成 instance (也就是 Docker container) 时,要执行的命令或者文件。