Dockerfile 就是用来构建 docker 镜像的构建文件!
# 创建 Dockerfile 文件,文件中的内容指令都是大写的
[root@qiaoyanjie docker-test-volume]# cat Dockerfile01
FROM centos
VOLUME ["volume01","volume02"]
CMD echo "---------end--------"
CMD /bin/bash
# 这里的每个命令,就是镜像的一层
通过 --help 查看 docker build 的参数
[root@qiaoyanjie docker-test-volume]# docker build --help
Usage: docker build [OPTIONS] PATH | URL | -
Build an image from a Dockerfile
Options:
--add-host list Add a custom host-to-IP mapping (host:ip)
--build-arg list Set build-time variables
--cache-from strings 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
--iidfile string Write the image ID to the file
--isolation string Container isolation technology
--label list Set metadata for an image
-m, --memory bytes Memory limit
--memory-swap bytes 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 strings Security options
--shm-size bytes Size of /dev/shm
-t, --tag list Name and optionally a tag in the 'name:tag' format
--target string Set the target build stage to build.
--ulimit ulimit Ulimit options (default [])
-f Dockerfile01 -- /home/docker-test-volume/Dockerfile01 绝对路径/相对路径都可以
-t centos_joe:1.0 -- 镜像名不可以有大写字母
. -- 不要忘记最后有个点
[root@qiaoyanjie docker-test-volume]# docker build -f Dockerfile01 -t centos_joe:1.0 .
Sending build context to Docker daemon 2.048kB
Step 1/4 : FROM centos
---> 5d0da3dc9764
Step 2/4 : VOLUME ["volume01","volume02"]
---> Running in d9e880291818
Removing intermediate container d9e880291818
---> 5fe0bc8787a6
Step 3/4 : CMD echo "---------end--------"
---> Running in b220ce1acf8c
Removing intermediate container b220ce1acf8c
---> 0dab02bff4f9
Step 4/4 : CMD /bin/bash
---> Running in b9e8f8bef174
Removing intermediate container b9e8f8bef174
---> fd9c73362619
Successfully built fd9c73362619
Successfully tagged centos_joe:1.0
当删除镜像时遇到 image has dependent child images 问题
[root@qiaoyanjie docker-test-volume]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos_joe 1.1 f2f02c9b2a25 49 minutes ago 231MB
centos_joe 1.0 312d40de4873 49 minutes ago 231MB
[root@qiaoyanjie docker-test-volume]# docker rmi -f f2f02c9b2a25
Error response from daemon: conflict: unable to delete f2f02c9b2a25 (cannot be forced) - image has dependent child images
这样的错误,原因是有另外的 image FROM 了这个 image,可以使用下面的命令列出所有在指定 image 之后创建的 image 的父 image
[root@qiaoyanjie docker-test-volume]# docker image inspect --format='{{.RepoTags}} {{.Id}} {{.Parent}}' $(docker image ls -q --filter since=f2f02c9b2a25)
[centos_joe:1.0] sha256:312d40de487313d43c742f0bcbac6ef5f2fb11643727cd3edd0e1286b168a088 sha256:f2f02c9b2a25f6b08c5bb4bbdf1d78e87b09ba7aec1ba4b48ee24e3dbeca0208
可根据 REPOSITORY:TAG 删除镜像
[root@qiaoyanjie docker-test-volume]# docker rmi centos_joe:1.1
Untagged: centos_joe:1.1
[root@qiaoyanjie docker-test-volume]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos_joe 1.0 312d40de4873 56 minutes ago 231MB
根据我们自己构建的镜像,启动并进入容器
volume01 volume02 这两个目录是我们生成镜像的时候自动挂载的数据卷目录
[root@qiaoyanjie docker-test-volume]# docker run -it fd9c73362619 /bin/bash
[root@f03fbf85f49f /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var volume01 volume02
这个卷和外部一定有一个同步的目录!
只写了容器内部路径,属于匿名挂载
在容器内 volume01 目录内创建一个文件,测试挂载在宿主机的目录是否同步
[root@f03fbf85f49f /]# cd volume01/
[root@f03fbf85f49f volume01]# touch Joe.txt
[root@f03fbf85f49f volume01]# ls
Joe.txt
通过 docker inspect 容器ID 查看挂载路径
进入宿主机目录,查看已经同步成功!
[root@qiaoyanjie docker-test-volume]# cd /var/lib/docker/volumes/ae8d873b75a78c9bb9a397a1d4a218afb895542cb1c2e740f693bdf37b8c3f5d/_data
[root@qiaoyanjie _data]# ls
Joe.txt
假设构建镜像时没有挂载卷,要手动镜像挂载 -v 卷名:容器内路径