Docker 学习7 Dockerfile详解
一、镜像的生成途径
1、使用当前进程替换上一个进程 exec
2、生成方式
3、dockerfile制作镜像要求
a、要有专有的工作目录。
b、要有专门的制作文件,文件名首字母大写
c、如果要打包很多很多文件到镜像中,那么这些文件只能放在当前工作目录下,不能放在工作目录上层,只能是子目录,不能是父目录。
d、.(这个有个小点)dockeringore 文件,如果一个目录中有一百个文件,然后在dockerfile 声明此目录需要包含这个目录,于是打包成镜像时就将这个目录包含进来了,然后在这个目录下有三个文件不想包含进来,于是将这三个文件路径写在dockeringore文件中,那么包括dockeringore本身和其中指定的文件都不会被打包进去。
e、shell 中 echo ${NAME:-tom} 意思是echo 变量NAME,若变量存在则打印变量值,若变量不存在则打印tom值,但是这个值不会赋值给NAME,若想要赋值给NAME,则命令为 echo ${NAME:=tom}
二、Dockerfile关键字
1、FROM
2、maintanier(可选项,已经废弃了,现在由label代替了)
3、copy
4、docker build
a、COPY文件
[root@localhost images1]# docker rmi b9faa659b2f0
Untagged: wohaoshuaihttpd:v0.1-1
Deleted: sha256:b9faa659b2f075f7972c0d240f337cd3e352230c71ec0a8fe3f71f4dd56f10e6
Deleted: sha256:b115441d58b0cfd85748a7c6e65774b38d611c2ba89e8566d12feb7fcd9be1d3
Deleted: sha256:fa8322f8adbf2c0608f682fd68d78dab800e86dcf6a7d7f872f636e190ce35a6
[root@localhost images1]# cat Dockerfile
#Description: test image
FROM busybox:latest
MAINTAINER "wohaoshuai <wohaoshuai@qq.com>"
#LABEL maintainer="wohaoshuai <wohaoshuai@qq.com>"
COPY index.html /data/web/html/
[root@localhost images1]# docker build -t wohaoshuaihttpd:v0.1-1 ./
Sending build context to Docker daemon 3.072kB
Step 1/3 : FROM busybox:latest
---> af2f74c517aa
Step 2/3 : MAINTAINER "wohaoshuai <wohaoshuai@qq.com>"
---> Running in d30be42cbf5b
Removing intermediate container d30be42cbf5b
---> f2af27ce81e5
Step 3/3 : COPY index.html /data/web/html/
---> 087c55a4372d
Successfully built 087c55a4372d
Successfully tagged wohaoshuaihttpd:v0.1-1
[root@localhost images1]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
wohaoshuaihttpd v0.1-1 087c55a4372d About a minute ago 1.2MB
busybox latest af2f74c517aa 2 weeks ago 1.2MB
[root@localhost images1]# docker run -it --name wohaoshuai1 --rm wohaoshuaihttpd:v0.1-1 /bin/cat /data/web/html/index.html
<h1>busybox httpd server</h1>
b、COPY目录
[root@localhost images1]# cat Dockerfile
#Description: test image
FROM busybox:latest
MAINTAINER "wohaoshuai <wohaoshuai@qq.com>"
#LABEL maintainer="wohaoshuai <wohaoshuai@qq.com>"
COPY index.html /data/web/html/
COPY yum.repos.d /etc/yum.repos.d/
[root@localhost images1]# ls yum.repos.d/
CentOS-Base.repo CentOS-CR.repo CentOS-Debuginfo.repo CentOS-fasttrack.repo CentOS-Media.repo CentOS-Sources.repo CentOS-Vault.repo docker-ce.repo
[root@localhost images1]# docker build -t wohaoshuaihttpd:v0.1-2 ./
Sending build context to Docker daemon 22.53kB
Step 1/4 : FROM busybox:latest
---> af2f74c517aa
Step 2/4 : MAINTAINER "wohaoshuai <wohaoshuai@qq.com>"
---> Using cache
---> f2af27ce81e5
Step 3/4 : COPY index.html /data/web/html/
---> Using cache
---> 087c55a4372d
Step 4/4 : COPY yum.repos.d /etc/yum.repos.d/
---> 8e63e3a6d63c
Successfully built 8e63e3a6d63c
Successfully tagged wohaoshuaihttpd:v0.1-2
[root@localhost images1]# docker run -it --name wohaoshuai1 --rm wohaoshuaihttpd:v0.1-2 /bin/ls /etc/yum.repos.d
CentOS-Base.repo CentOS-Media.repo CentOS-fasttrack.repo
CentOS-CR.repo CentOS-Sources.repo docker-ce.repo
CentOS-Debuginfo.repo CentOS-Vault.repo
5、ADD
a、通过连接下载压缩包至镜像中的目录时只会将压缩包放在目标路径下
[root@localhost images1]# cat Dockerfile
#Description: test image
FROM busybox:latest
MAINTAINER "wohaoshuai <wohaoshuai@qq.com>"
#LABEL maintainer="wohaoshuai <wohaoshuai@qq.com>"
COPY index.html /data/web/html/
COPY yum.repos.d /etc/yum.repos.d/
ADD http://nginx.org/download/nginx-1.15.12.tar.gz /usr/local/src/
[root@localhost images1]# docker build -t wohaoshuaihttpd:v0.1-3 ./
Sending build context to Docker daemon 24.06kB
Step 1/5 : FROM busybox:latest
---> af2f74c517aa
Step 2/5 : MAINTAINER "wohaoshuai <wohaoshuai@qq.com>"
---> Using cache
---> f2af27ce81e5
Step 3/5 : COPY index.html /data/web/html/
---> Using cache
---> 087c55a4372d
Step 4/5 : COPY yum.repos.d /etc/yum.repos.d/
---> Using cache
---> 5ed5523bedf5
Step 5/5 : ADD http://nginx.org/download/nginx-1.15.12.tar.gz /usr/local/src/
Downloading [==================================================>] 1.032MB/1.032MB
---> 9321e473e9fd
Successfully built 9321e473e9fd
Successfully tagged wohaoshuaihttpd:v0.1-3
[root@localhost images1]# docker run -it --name wohaoshuai1 --rm wohaoshuaihttpd:v0.1-3 /bin/ls /usr/local/src/
nginx-1.15.12.tar.gz
b、将本地压缩包添加至镜像中的路径时压缩包会自动解压
[root@localhost images1]# cat Dockerfile
#Description: test image
FROM busybox:latest
MAINTAINER "wohaoshuai <wohaoshuai@qq.com>"
#LABEL maintainer="wohaoshuai <wohaoshuai@qq.com>"
COPY index.html /data/web/html/
COPY yum.repos.d /etc/yum.repos.d/
#ADD http://nginx.org/download/nginx-1.15.12.tar.gz /usr/local/src/
ADD nginx-1.15.12.tar.gz /usr/local/src/
[root@localhost images1]# docker build -t wohaoshuaihttpd:v0.1-4 ./
Sending build context to Docker daemon 1.057MB
Step 1/5 : FROM busybox:latest
---> af2f74c517aa
Step 2/5 : MAINTAINER "wohaoshuai <wohaoshuai@qq.com>"
---> Using cache
---> f2af27ce81e5
Step 3/5 : COPY index.html /data/web/html/
---> Using cache
---> 087c55a4372d
Step 4/5 : COPY yum.repos.d /etc/yum.repos.d/
---> Using cache
---> 5ed5523bedf5
Step 5/5 : ADD nginx-1.15.12.tar.gz /usr/local/src/
---> d2c9f10561d8
Successfully built d2c9f10561d8
Successfully tagged wohaoshuaihttpd:v0.1-4
[root@localhost images1]# docker run -it --name wohaoshuai1 --rm wohaoshuaihttpd:v0.1-4 /bin/ls /usr/local/src/
nginx-1.15.12
[root@localhost images1]# docker run -it --name wohaoshuai1 --rm wohaoshuaihttpd:v0.1-4 /bin/ls /usr/local/src/nginx-1.15.12
CHANGES LICENSE auto configure html src
CHANGES.ru README conf contrib man
6、WORKDIR
[root@localhost images1]# cat Dockerfile
#Description: test image
FROM busybox:latest
MAINTAINER "wohaoshuai <wohaoshuai@qq.com>"
#LABEL maintainer="wohaoshuai <wohaoshuai@qq.com>"
COPY index.html /data/web/html/
COPY yum.repos.d /etc/yum.repos.d/
#ADD http://nginx.org/download/nginx-1.15.12.tar.gz /usr/local/src/
WORKDIR /usr/local/
ADD nginx-1.15.12.tar.gz ./src
7、VOLUME(以此镜像启动的容器会自动挂载卷至声明的路径中)
[root@localhost images1]# cat Dockerfile
#Description: test image
FROM busybox:latest
MAINTAINER "wohaoshuai <wohaoshuai@qq.com>"
#LABEL maintainer="wohaoshuai <wohaoshuai@qq.com>"
COPY index.html /data/web/html/
COPY yum.repos.d /etc/yum.repos.d/
#ADD http://nginx.org/download/nginx-1.15.12.tar.gz /usr/local/src/
WORKDIR /usr/local/
ADD nginx-1.15.12.tar.gz ./src
VOLUME /data/mysql/
[root@localhost images1]# docker build -t wohaoshuaihttpd:v0.1-5 ./
Sending build context to Docker daemon 1.057MB
Step 1/7 : FROM busybox:latest
---> af2f74c517aa
Step 2/7 : MAINTAINER "wohaoshuai <wohaoshuai@qq.com>"
---> Using cache
---> f2af27ce81e5
Step 3/7 : COPY index.html /data/web/html/
---> Using cache
---> 087c55a4372d
Step 4/7 : COPY yum.repos.d /etc/yum.repos.d/
---> Using cache
---> 5ed5523bedf5
Step 5/7 : WORKDIR /usr/local/
---> Running in 5a4e26a6f3d2
Removing intermediate container 5a4e26a6f3d2
---> d947722654f3
Step 6/7 : ADD nginx-1.15.12.tar.gz ./src
---> 4c2b8cd7c923
Step 7/7 : VOLUME /data/mysql/
---> Running in aa6bd3d6c6ef
Removing intermediate container aa6bd3d6c6ef
---> 10f65aedcb7f
Successfully built 10f65aedcb7f
Successfully tagged wohaoshuaihttpd:v0.1-5
[root@localhost images1]# docker run -it --name wohaoshuai1 --rm wohaoshuaihttpd:v0.1-5 /bin/ls /usr/local/src
nginx-1.15.12
[root@localhost images1]# docker run -it --name wohaoshuai1 --rm wohaoshuaihttpd:v0.1-5 sleep 60
[root@localhost application]# docker inspect -f {{.Mounts}} wohaoshuai1
[{volume 41bb5a4cb251996e85259490f34c13039a4a60c31a019e1da6b5a059ada6477c /var/lib/docker/volumes/41bb5a4cb251996e85259490f34c13039a4a60c31a019e1da6b5a059ada6477c/_data /data/mysql local t
rue }]
8、EXPOSE
[root@localhost images1]# cat Dockerfile
#Description: test image
FROM busybox:latest
MAINTAINER "wohaoshuai <wohaoshuai@qq.com>"
#LABEL maintainer="wohaoshuai <wohaoshuai@qq.com>"
COPY index.html /data/web/html/
COPY yum.repos.d /etc/yum.repos.d/
#ADD http://nginx.org/download/nginx-1.15.12.tar.gz /usr/local/src/
WORKDIR /usr/local/
ADD nginx-1.15.12.tar.gz ./src
VOLUME /data/mysql/
EXPOSE 80
[root@localhost images1]# docker build -t wohaoshuaihttpd:v0.1-6 ./
Sending build context to Docker daemon 1.057MB
Step 1/8 : FROM busybox:latest
---> af2f74c517aa
Step 2/8 : MAINTAINER "wohaoshuai <wohaoshuai@qq.com>"
---> Using cache
---> f2af27ce81e5
Step 3/8 : COPY index.html /data/web/html/
---> Using cache
---> 087c55a4372d
Step 4/8 : COPY yum.repos.d /etc/yum.repos.d/
---> Using cache
---> 5ed5523bedf5
Step 5/8 : WORKDIR /usr/local/
---> Using cache
---> d947722654f3
Step 6/8 : ADD nginx-1.15.12.tar.gz ./src
---> Using cache
---> 4c2b8cd7c923
Step 7/8 : VOLUME /data/mysql/
---> Using cache
---> 10f65aedcb7f
Step 8/8 : EXPOSE 80
---> Running in a644ef9ea077
Removing intermediate container a644ef9ea077
---> 069cdf4d8de8
Successfully built 069cdf4d8de8
Successfully tagged wohaoshuaihttpd:v0.1-6
[root@localhost images1]# docker run -it --name wohaoshuai1 --rm wohaoshuaihttpd:v0.1-6 /bin/httpd -f(前台运行) -h /data/web/html
[root@localhost application]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
eaab4307ee93 wohaoshuaihttpd:v0.1-6 "/bin/httpd -f -h /d…" 3 minutes ago Up 3 minutes 80/tcp wohaoshuai1
[root@localhost application]# curl 172.17.0.2
<h1>busybox httpd server</h1>
[root@localhost application]# docker port wohaoshuai1
[root@localhost application]#
(发现未将端口映射至宿主机)
在启动时加上-P选项,将镜像中所有申明的端口都映射至宿主机
[root@localhost images1]# docker run -it --name wohaoshuai1 -P --rm wohaoshuaihttpd:v0.1-6 /bin/httpd -f -h /data/web/html
[root@localhost application]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6531f428273d wohaoshuaihttpd:v0.1-6 "/bin/httpd -f -h /d…" 4 seconds ago Up 3 seconds 0.0.0.0:32768->80/tcp wohaoshuai1
[root@localhost application]# docker port wohaoshuai1
80/tcp -> 0.0.0.0:32768
镜像中声明的暴露端口是指默认暴露端口,也可以启动镜像的时候手动加上-p选项指定暴露的端口
9、ENV
a、在Dockerfile中定义环境变量
[root@localhost images1]# cat Dockerfile #Description: test image FROM busybox:latest MAINTAINER "wohaoshuai <wohaoshuai@qq.com>" #LABEL maintainer="wohaoshuai <wohaoshuai@qq.com>" ENV DOC_ROOT=/data/web/html/ \ WEB_SERVER_PACKAGE="nginx-1.15.12" #若变量为空则将返回/data/web/html/,但不会讲该值赋值给此变量 COPY index.html ${DOC_ROOT:-/data/web/html/} COPY yum.repos.d /etc/yum.repos.d/ #ADD http://nginx.org/download/nginx-1.15.12.tar.gz /usr/local/src/ WORKDIR /usr/local/ ADD ${WEB_SERVER_PACKAGE}.tar.gz ./src VOLUME /data/mysql/ EXPOSE 80 [root@localhost images1]# docker build -t wohaoshuaihttpd:v0.1-7 ./ Sending build context to Docker daemon 1.058MB Step 1/9 : FROM busybox:latest ---> af2f74c517aa Step 2/9 : MAINTAINER "wohaoshuai <wohaoshuai@qq.com>" ---> Using cache ---> f2af27ce81e5 Step 3/9 : ENV DOC_ROOT=/data/web/html/ WEB_SERVER_PACKAGE="nginx-1.15.12" ---> Running in 2f63c2a97feb Removing intermediate container 2f63c2a97feb ---> 5e878ad397d3 Step 4/9 : COPY index.html ${DOC_ROOT:-/data/web/html/} ---> c8ccb0db2c85 Step 5/9 : COPY yum.repos.d /etc/yum.repos.d/ ---> b4e3ba7698c0 Step 6/9 : WORKDIR /usr/local/ ---> Running in f37c3a5f7b6b Removing intermediate container f37c3a5f7b6b ---> 4185f0e9b005 Step 7/9 : ADD ${WEB_SERVER_PACKAGE}.tar.gz ./src ---> b9e5dc8aefd4 Step 8/9 : VOLUME /data/mysql/ ---> Running in 7e74c121a4ae Removing intermediate container 7e74c121a4ae ---> 91f4d73fe90f Step 9/9 : EXPOSE 80 ---> Running in 322864c64e00 Removing intermediate container 322864c64e00 ---> 3729cc7464e2 Successfully built 3729cc7464e2 Successfully tagged wohaoshuaihttpd:v0.1-7 [root@localhost images1]# docker run -it --name wohaoshuai1 -P --rm wohaoshuaihttpd:v0.1-7 /bin/ls /usr/local/src nginx-1.15.12 [root@localhost images1]# docker exec -it wohaoshuai1 /bin/sh /usr/local # echo ${WEB_SERVER_PACKAGE} && echo ${DOC_ROOT} nginx-1.15.12 /data/web/html/
[root@localhost images1]# docker run -it --name wohaoshuai1 -P --rm wohaoshuaihttpd:v0.1-7 printenv
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=474614eded73
TERM=xterm
DOC_ROOT=/data/web/html/
WEB_SERVER_PACKAGE=nginx-1.15.12
HOME=/root
b、启动容器的时候赋予环境变量
[root@localhost images1]# docker run -it --name wohaoshuai1 -P -e WEB_SERVER_PACKAGE="nginx-1.15.13" -e NAME="wohaoshuai" --rm wohaoshuaihttpd:v0.1-7 printenv
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=2be6a82f0adb
TERM=xterm
WEB_SERVER_PACKAGE=nginx-1.15.13
NAME=wohaoshuai
DOC_ROOT=/data/web/html/
HOME=/root
10、RUN :相当于在镜像中执行相应命令
[root@localhost images1]# cat Dockerfile
#Description: test image
FROM busybox:latest
MAINTAINER "wohaoshuai <wohaoshuai@qq.com>"
#LABEL maintainer="wohaoshuai <wohaoshuai@qq.com>"
ENV DOC_ROOT=/data/web/html/ \
WEB_SERVER_PACKAGE="nginx-1.15.12.tar.gz"
#若变量为空则将返回/data/web/html/,但不会讲该值赋值给此变量
COPY index.html ${DOC_ROOT:-/data/web/html/}
COPY yum.repos.d /etc/yum.repos.d/
ADD http://nginx.org/download/${WEB_SERVER_PACKAGE} /usr/local/src/
WORKDIR /usr/local/
#ADD ${WEB_SERVER_PACKAGE}.tar.gz ./src
VOLUME /data/mysql/
EXPOSE 80
RUN cd /usr/local/src && \
tar -xf ${WEB_SERVER_PACKAGE}
[root@localhost images1]# docker build -t wohaoshuaihttpd:v0.1-9 ./
Sending build context to Docker daemon 1.058MB
Step 1/10 : FROM busybox:latest
---> af2f74c517aa
Step 2/10 : MAINTAINER "wohaoshuai <wohaoshuai@qq.com>"
---> Using cache
---> f2af27ce81e5
Step 3/10 : ENV DOC_ROOT=/data/web/html/ WEB_SERVER_PACKAGE="nginx-1.15.12.tar.gz"
---> Running in 130868ae462c
Removing intermediate container 130868ae462c
---> 7ee3bd028dcb
Step 4/10 : COPY index.html ${DOC_ROOT:-/data/web/html/}
---> 1b50d9f7b618
Step 5/10 : COPY yum.repos.d /etc/yum.repos.d/
---> 812d86e6606a
Step 6/10 : ADD http://nginx.org/download/${WEB_SERVER_PACKAGE} /usr/local/src/
Downloading [==================================================>] 1.032MB/1.032MB
---> 89c9d4323fbe
Step 7/10 : WORKDIR /usr/local/
---> Running in a92d220baaac
Removing intermediate container a92d220baaac
---> dc11e4e9003c
Step 8/10 : VOLUME /data/mysql/
---> Running in d998a6698c6f
Removing intermediate container d998a6698c6f
---> 4d41ad3eae50
Step 9/10 : EXPOSE 80
---> Running in bfe9d0fe42b0
Removing intermediate container bfe9d0fe42b0
---> 8177bf9c1201
Step 10/10 : RUN cd /usr/local/src && tar -xf ${WEB_SERVER_PACKAGE}
---> Running in 6635339f9b2c
Removing intermediate container 6635339f9b2c
---> c7f2dc787705
Successfully built c7f2dc787705
Successfully tagged wohaoshuaihttpd:v0.1-9
[root@localhost images1]# docker run -it --name wohaoshuai1 -P --rm wohaoshuaihttpd:v0.1-9 ls /usr/local/src
nginx-1.15.12 nginx-1.15.12.tar.gz
11、CMD 定义一个镜像文件要启动为容器时默认要运行的程序或命令,可以给多个,但是只有最后一个会生效
a、第一种方式启动后容器中的PID不为1,无法接收unix信号,是由内核直接创建,因此无法使用docker stop 命令停止
[root@localhost images2]# cat Dockerfile
FROM busybox
LABEL maintainer="wohaoshuai <wohaoshuai@qq.com>" app="httpd"
ENV WEB_DOC_ROOT="/data/web/html"
RUN mkdir -p ${WEB_DOC_ROOT} && \
echo "<h1>Busybox httpd server.</h1>" > ${WEB_DOC_ROOT}/index.html
CMD /bin/httpd -f -h ${WEB_DOC_ROOT}
[root@localhost images2]# docker build -t wohaoshuaihttpd:v0.2-1 ./
Sending build context to Docker daemon 2.048kB
Step 1/5 : FROM busybox
---> af2f74c517aa
Step 2/5 : LABEL maintainer="wohaoshuai <wohaoshuai@qq.com>" app="httpd"
---> Using cache
---> 7ea5abaaf52f
Step 3/5 : ENV WEB_DOC_ROOT="/data/web/html"
---> Using cache
---> 2ab8b976c922
Step 4/5 : RUN mkdir -p ${WEB_DOC_ROOT} && echo "<h1>Busybox httpd server.</h1>" > ${WEB_DOC_ROOT}/index.html
---> Running in 3abfe017a227
Removing intermediate container 3abfe017a227
---> ad6a65debbf4
Step 5/5 : CMD /bin/httpd -f -h ${WEB_DOC_ROOT}
---> Running in 9813c1737aa2
Removing intermediate container 9813c1737aa2
---> bf9029cac03e
Successfully built bf9029cac03e
Successfully tagged wohaoshuaihttpd:v0.2-1
[root@localhost images2]# docker inspect -f {{.Config.Cmd}} wohaoshuaihttpd:v0.2-1
[/bin/sh -c /bin/httpd -f -h ${WEB_DOC_ROOT}]
[root@localhost images2]# docker run --name wohaoshuai2 -it --rm -P wohaoshuaihttpd:v0.2-1 #此时会卡在这儿,因为httpd并没有交互式接口,但是此时可以通过docker exec 命令登陆
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e4846ba4e7c7 wohaoshuaihttpd:v0.2-1 "/bin/sh -c '/bin/ht…" 12 seconds ago Up 11 seconds wohaoshuai2
[root@localhost ~]# docker exec -it wohaoshuai2 /bin/sh
/ # ps #此时可以看到httpd默认的pid为1,默认用/bin/httpd -f -h /data/web/html启动时为了避免它不是1做替换操作,默认就执行了exec的替换,所以看上去我们启动的httpd 进程依然是为1是为了确保容器能自动接收unix信号以便于执行docker stop命令时能停掉,但在上面的
截图中确实是验证了是启动为/bin/sh 的子进程的,并且也验证了镜像启动以后默认的运行命令被我们改掉了,而且shell解析环境变量也成功了。
PID USER TIME COMMAND
1 root 0:00 /bin/httpd -f -h /data/web/html
6 root 0:00 /bin/sh
11 root 0:00 ps
/ #
b、如果命令中有空格则使用第二种语法格式
[root@localhost images2]# cat Dockerfile
FROM busybox
LABEL maintainer="wohaoshuai <wohaoshuai@qq.com>" app="httpd"
ENV WEB_DOC_ROOT="/data/web/html"
RUN mkdir -p ${WEB_DOC_ROOT} && \
echo "<h1>Busybox httpd server.</h1>" > ${WEB_DOC_ROOT}/index.html
#CMD /bin/httpd -f -h ${WEB_DOC_ROOT}
CMD ["/bin/httpd","-f","-h ${WEB_DOC_ROOT}"]
[root@localhost images2]# docker build -t wohaoshuaihttpd:v0.2-2 ./
Sending build context to Docker daemon 2.048kB
Step 1/5 : FROM busybox
---> af2f74c517aa
Step 2/5 : LABEL maintainer="wohaoshuai <wohaoshuai@qq.com>" app="httpd"
---> Using cache
---> 7ea5abaaf52f
Step 3/5 : ENV WEB_DOC_ROOT="/data/web/html"
---> Using cache
---> 2ab8b976c922
Step 4/5 : RUN mkdir -p ${WEB_DOC_ROOT} && echo "<h1>Busybox httpd server.</h1>" > ${WEB_DOC_ROOT}/index.html
---> Using cache
---> ad6a65debbf4
Step 5/5 : CMD ["/bin/httpd","-f","-h ${WEB_DOC_ROOT}"]
---> Running in 02b4bdd57f3d
Removing intermediate container 02b4bdd57f3d
---> d7bec572fdee
Successfully built d7bec572fdee
Successfully tagged wohaoshuaihttpd:v0.2-2
[root@localhost images2]# docker inspect -f {{.Config.Cmd}} wohaoshuaihttpd:v0.2-2
[/bin/httpd -f -h ${WEB_DOC_ROOT}]
基于制作的镜像启动容器
[root@localhost images2]# docker run --name wohaoshuai2 -it --rm -P wohaoshuaihttpd:v0.2-2 #会报错,因为默认不是通过shell子进程启动,因此无法识别相应的变量,所以会找不到相应的文件或文件夹
httpd: can't change directory to ' ${WEB_DOC_ROOT}': No such file or directory
将CMD 中内容改为 CMD ["/bin/sh","-c","/bin/httpd -f -h ${WEB_DOC_ROOT}"] #注意不能将 -c 后的其它参数也写成字符串,否则会无法启动
[root@localhost images2]# more Dockerfile
FROM busybox
LABEL maintainer="wohaoshuai <wohaoshuai@qq.com>" app="httpd"
ENV WEB_DOC_ROOT="/data/web/html"
RUN mkdir -p ${WEB_DOC_ROOT} && \
echo "<h1>Busybox httpd server.</h1>" > ${WEB_DOC_ROOT}/index.html
#CMD /bin/httpd -f -h ${WEB_DOC_ROOT}
CMD ["/bin/sh","-c","/bin/httpd -f -h ${WEB_DOC_ROOT}"]
[root@localhost images2]# docker build -t wohaoshuaihttpd:v0.2-3 ./
Sending build context to Docker daemon 2.048kB
Step 1/5 : FROM busybox
---> af2f74c517aa
Step 2/5 : LABEL maintainer="wohaoshuai <wohaoshuai@qq.com>" app="httpd"
---> Using cache
---> b94158ebd25b
Step 3/5 : ENV WEB_DOC_ROOT="/data/web/html"
---> Using cache
---> 128dc125c148
Step 4/5 : RUN mkdir -p ${WEB_DOC_ROOT} && echo "<h1>Busybox httpd server.</h1>" > ${WEB_DOC_ROOT}/index.html
---> Using cache
---> 79e6d697305f
Step 5/5 : CMD ["/bin/sh","-c","/bin/httpd -f -h ${WEB_DOC_ROOT}"]
---> Running in ad80d6ab8dd9
Removing intermediate container ad80d6ab8dd9
---> 745d9aa5c776
Successfully built 745d9aa5c776
Successfully tagged wohaoshuaihttpd:v0.2-3
[root@localhost images2]# docker run -it --rm -P --name wohaoshuai1 wohaoshuaihttpd:v0.2-3
[root@localhost ~]# docker exec -it wohaoshuai1 /bin/sh
/ # ls
bin data dev etc home proc root sys tmp usr var
/ # ps
PID USER TIME COMMAND
1 root 0:00 /bin/httpd -f -h /data/web/html
13 root 0:00 /bin/sh
20 root 0:00 ps
/ # netstat -a
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 :::80 :::* LISTEN