Docker 学习7 Dockerfile详解

一、镜像的生成途径

  1、使用当前进程替换上一个进程 exec

  2、生成方式

    Docker 学习7 Dockerfile详解

  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

    Docker 学习7 Dockerfile详解

  2、maintanier(可选项,已经废弃了,现在由label代替了)

    Docker 学习7 Dockerfile详解

  3、copy

    Docker 学习7 Dockerfile详解

  4、docker build

    a、COPY文件

[root@localhost images1]# docker rmi b9faa659b2f0
Untagged: wohaoshuaihttpd:v0.-
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.- ./
Sending build context to Docker daemon .072kB
Step / : FROM busybox:latest
---> af2f74c517aa
Step / : MAINTAINER "wohaoshuai <wohaoshuai@qq.com>"
---> Running in d30be42cbf5b
Removing intermediate container d30be42cbf5b
---> f2af27ce81e5
Step / : COPY index.html /data/web/html/
---> 087c55a4372d
Successfully built 087c55a4372d
Successfully tagged wohaoshuaihttpd:v0.-
[root@localhost images1]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
wohaoshuaihttpd v0.- 087c55a4372d About a minute ago .2MB
busybox latest af2f74c517aa weeks ago .2MB
[root@localhost images1]# docker run -it --name wohaoshuai1 --rm wohaoshuaihttpd:v0.- /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.- ./
Sending build context to Docker daemon .53kB
Step / : FROM busybox:latest
---> af2f74c517aa
Step / : MAINTAINER "wohaoshuai <wohaoshuai@qq.com>"
---> Using cache
---> f2af27ce81e5
Step / : COPY index.html /data/web/html/
---> Using cache
---> 087c55a4372d
Step / : COPY yum.repos.d /etc/yum.repos.d/
---> 8e63e3a6d63c
Successfully built 8e63e3a6d63c
Successfully tagged wohaoshuaihttpd:v0.-
[root@localhost images1]# docker run -it --name wohaoshuai1 --rm wohaoshuaihttpd:v0.- /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

    Docker 学习7 Dockerfile详解

    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.- ./
Sending build context to Docker daemon .06kB
Step / : FROM busybox:latest
---> af2f74c517aa
Step / : MAINTAINER "wohaoshuai <wohaoshuai@qq.com>"
---> Using cache
---> f2af27ce81e5
Step / : COPY index.html /data/web/html/
---> Using cache
---> 087c55a4372d
Step / : COPY yum.repos.d /etc/yum.repos.d/
---> Using cache
---> 5ed5523bedf5
Step / : ADD http://nginx.org/download/nginx-1.15.12.tar.gz /usr/local/src/
Downloading [==================================================>] .032MB/.032MB
---> 9321e473e9fd
Successfully built 9321e473e9fd
Successfully tagged wohaoshuaihttpd:v0.-
[root@localhost images1]# docker run -it --name wohaoshuai1 --rm wohaoshuaihttpd:v0.- /bin/ls /usr/local/src/
nginx-1.15..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..tar.gz /usr/local/src/
[root@localhost images1]# docker build -t wohaoshuaihttpd:v0.- ./
Sending build context to Docker daemon .057MB
Step / : FROM busybox:latest
---> af2f74c517aa
Step / : MAINTAINER "wohaoshuai <wohaoshuai@qq.com>"
---> Using cache
---> f2af27ce81e5
Step / : COPY index.html /data/web/html/
---> Using cache
---> 087c55a4372d
Step / : COPY yum.repos.d /etc/yum.repos.d/
---> Using cache
---> 5ed5523bedf5
Step / : ADD nginx-1.15..tar.gz /usr/local/src/
---> d2c9f10561d8
Successfully built d2c9f10561d8
Successfully tagged wohaoshuaihttpd:v0.-
[root@localhost images1]# docker run -it --name wohaoshuai1 --rm wohaoshuaihttpd:v0.- /bin/ls /usr/local/src/
nginx-1.15.
[root@localhost images1]# docker run -it --name wohaoshuai1 --rm wohaoshuaihttpd:v0.- /bin/ls /usr/local/src/nginx-1.15.
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..tar.gz ./src

  7、VOLUME(以此镜像启动的容器会自动挂载卷至声明的路径中)

    Docker 学习7 Dockerfile详解

[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..tar.gz ./src
VOLUME /data/mysql/
[root@localhost images1]# docker build -t wohaoshuaihttpd:v0.- ./
Sending build context to Docker daemon .057MB
Step / : FROM busybox:latest
---> af2f74c517aa
Step / : MAINTAINER "wohaoshuai <wohaoshuai@qq.com>"
---> Using cache
---> f2af27ce81e5
Step / : COPY index.html /data/web/html/
---> Using cache
---> 087c55a4372d
Step / : COPY yum.repos.d /etc/yum.repos.d/
---> Using cache
---> 5ed5523bedf5
Step / : WORKDIR /usr/local/
---> Running in 5a4e26a6f3d2
Removing intermediate container 5a4e26a6f3d2
---> d947722654f3
Step / : ADD nginx-1.15..tar.gz ./src
---> 4c2b8cd7c923
Step / : VOLUME /data/mysql/
---> Running in aa6bd3d6c6ef
Removing intermediate container aa6bd3d6c6ef
---> 10f65aedcb7f
Successfully built 10f65aedcb7f
Successfully tagged wohaoshuaihttpd:v0.-
[root@localhost images1]# docker run -it --name wohaoshuai1 --rm wohaoshuaihttpd:v0.- /bin/ls /usr/local/src
nginx-1.15.
[root@localhost images1]# docker run -it --name wohaoshuai1 --rm wohaoshuaihttpd:v0.- sleep [root@localhost application]# docker inspect -f {{.Mounts}} wohaoshuai1
[{volume 41bb5a4cb251996e85259490f34c13039a4a60c31a019e1da6b5a059ada6477c /var/lib/docker/volumes/41bb5a4cb251996e85259490f34c13039a4a60c31a019e1da6b5a059ada6477c/_data /data/mysql local t
rue }]

  8、EXPOSE

    Docker 学习7 Dockerfile详解

[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..tar.gz ./src
VOLUME /data/mysql/
EXPOSE
[root@localhost images1]# docker build -t wohaoshuaihttpd:v0.- ./
Sending build context to Docker daemon .057MB
Step / : FROM busybox:latest
---> af2f74c517aa
Step / : MAINTAINER "wohaoshuai <wohaoshuai@qq.com>"
---> Using cache
---> f2af27ce81e5
Step / : COPY index.html /data/web/html/
---> Using cache
---> 087c55a4372d
Step / : COPY yum.repos.d /etc/yum.repos.d/
---> Using cache
---> 5ed5523bedf5
Step / : WORKDIR /usr/local/
---> Using cache
---> d947722654f3
Step / : ADD nginx-1.15..tar.gz ./src
---> Using cache
---> 4c2b8cd7c923
Step / : VOLUME /data/mysql/
---> Using cache
---> 10f65aedcb7f
Step / : EXPOSE
---> Running in a644ef9ea077
Removing intermediate container a644ef9ea077
---> 069cdf4d8de8
Successfully built 069cdf4d8de8
Successfully tagged wohaoshuaihttpd:v0.-
[root@localhost images1]# docker run -it --name wohaoshuai1 --rm wohaoshuaihttpd:v0.- /bin/httpd -f(前台运行) -h /data/web/html [root@localhost application]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
eaab4307ee93 wohaoshuaihttpd:v0.- "/bin/httpd -f -h /d…" minutes ago Up minutes /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.- /bin/httpd -f -h /data/web/html

[root@localhost application]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6531f428273d wohaoshuaihttpd:v0.- "/bin/httpd -f -h /d…" seconds ago Up seconds 0.0.0.0:->/tcp wohaoshuai1
[root@localhost application]# docker port wohaoshuai1
/tcp -> 0.0.0.0:

    镜像中声明的暴露端口是指默认暴露端口,也可以启动镜像的时候手动加上-p选项指定暴露的端口

  9、ENV

    Docker 学习7 Dockerfile详解

    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 [root@localhost images1]# docker build -t wohaoshuaihttpd:v0.- ./
Sending build context to Docker daemon .058MB
Step / : FROM busybox:latest
---> af2f74c517aa
Step / : MAINTAINER "wohaoshuai <wohaoshuai@qq.com>"
---> Using cache
---> f2af27ce81e5
Step / : ENV DOC_ROOT=/data/web/html/ WEB_SERVER_PACKAGE="nginx-1.15.12"
---> Running in 2f63c2a97feb
Removing intermediate container 2f63c2a97feb
---> 5e878ad397d3
Step / : COPY index.html ${DOC_ROOT:-/data/web/html/}
---> c8ccb0db2c85
Step / : COPY yum.repos.d /etc/yum.repos.d/
---> b4e3ba7698c0
Step / : WORKDIR /usr/local/
---> Running in f37c3a5f7b6b
Removing intermediate container f37c3a5f7b6b
---> 4185f0e9b005
Step / : ADD ${WEB_SERVER_PACKAGE}.tar.gz ./src
---> b9e5dc8aefd4
Step / : VOLUME /data/mysql/
---> Running in 7e74c121a4ae
Removing intermediate container 7e74c121a4ae
---> 91f4d73fe90f
Step / : EXPOSE
---> Running in 322864c64e00
Removing intermediate container 322864c64e00
---> 3729cc7464e2
Successfully built 3729cc7464e2
Successfully tagged wohaoshuaihttpd:v0.-
[root@localhost images1]# docker run -it --name wohaoshuai1 -P --rm wohaoshuaihttpd:v0.- /bin/ls /usr/local/src
nginx-1.15. [root@localhost images1]# docker exec -it wohaoshuai1 /bin/sh
/usr/local # echo ${WEB_SERVER_PACKAGE} && echo ${DOC_ROOT}
nginx-1.15.
/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.- printenv
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=2be6a82f0adb
TERM=xterm
WEB_SERVER_PACKAGE=nginx-1.15.
NAME=wohaoshuai
DOC_ROOT=/data/web/html/
HOME=/root

  10、RUN :相当于在镜像中执行相应命令

    Docker 学习7 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.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 RUN cd /usr/local/src && \
tar -xf ${WEB_SERVER_PACKAGE} [root@localhost images1]# docker build -t wohaoshuaihttpd:v0.- ./
Sending build context to Docker daemon .058MB
Step / : FROM busybox:latest
---> af2f74c517aa
Step / : MAINTAINER "wohaoshuai <wohaoshuai@qq.com>"
---> Using cache
---> f2af27ce81e5
Step / : ENV DOC_ROOT=/data/web/html/ WEB_SERVER_PACKAGE="nginx-1.15.12.tar.gz"
---> Running in 130868ae462c
Removing intermediate container 130868ae462c
---> 7ee3bd028dcb
Step / : COPY index.html ${DOC_ROOT:-/data/web/html/}
---> 1b50d9f7b618
Step / : COPY yum.repos.d /etc/yum.repos.d/
---> 812d86e6606a
Step / : ADD http://nginx.org/download/${WEB_SERVER_PACKAGE} /usr/local/src/
Downloading [==================================================>] .032MB/.032MB
---> 89c9d4323fbe
Step / : WORKDIR /usr/local/
---> Running in a92d220baaac
Removing intermediate container a92d220baaac
---> dc11e4e9003c
Step / : VOLUME /data/mysql/
---> Running in d998a6698c6f
Removing intermediate container d998a6698c6f
---> 4d41ad3eae50
Step / : EXPOSE
---> Running in bfe9d0fe42b0
Removing intermediate container bfe9d0fe42b0
---> 8177bf9c1201
Step / : 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.-
[root@localhost images1]# docker run -it --name wohaoshuai1 -P --rm wohaoshuaihttpd:v0.- ls /usr/local/src
nginx-1.15. nginx-1.15..tar.gz

  11、CMD 定义一个镜像文件要启动为容器时默认要运行的程序或命令,可以给多个,但是只有最后一个会生效

    Docker 学习7 Dockerfile详解

    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.- ./
Sending build context to Docker daemon .048kB
Step / : FROM busybox
---> af2f74c517aa
Step / : LABEL maintainer="wohaoshuai <wohaoshuai@qq.com>" app="httpd"
---> Using cache
---> 7ea5abaaf52f
Step / : ENV WEB_DOC_ROOT="/data/web/html"
---> Using cache
---> 2ab8b976c922
Step / : 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 / : CMD /bin/httpd -f -h ${WEB_DOC_ROOT}
---> Running in 9813c1737aa2
Removing intermediate container 9813c1737aa2
---> bf9029cac03e
Successfully built bf9029cac03e
Successfully tagged wohaoshuaihttpd:v0.- [root@localhost images2]# docker inspect -f {{.Config.Cmd}} wohaoshuaihttpd:v0.-
[/bin/sh -c /bin/httpd -f -h ${WEB_DOC_ROOT}]
[root@localhost images2]# docker run --name wohaoshuai2 -it --rm -P wohaoshuaihttpd:v0.- #此时会卡在这儿,因为httpd并没有交互式接口,但是此时可以通过docker exec 命令登陆
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e4846ba4e7c7 wohaoshuaihttpd:v0.- "/bin/sh -c '/bin/ht…" seconds ago Up 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
root : /bin/httpd -f -h /data/web/html
root : /bin/sh
root : 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.- ./
Sending build context to Docker daemon .048kB
Step / : FROM busybox
---> af2f74c517aa
Step / : LABEL maintainer="wohaoshuai <wohaoshuai@qq.com>" app="httpd"
---> Using cache
---> 7ea5abaaf52f
Step / : ENV WEB_DOC_ROOT="/data/web/html"
---> Using cache
---> 2ab8b976c922
Step / : RUN mkdir -p ${WEB_DOC_ROOT} && echo "<h1>Busybox httpd server.</h1>" > ${WEB_DOC_ROOT}/index.html
---> Using cache
---> ad6a65debbf4
Step / : CMD ["/bin/httpd","-f","-h ${WEB_DOC_ROOT}"] 
---> Running in 02b4bdd57f3d
Removing intermediate container 02b4bdd57f3d
---> d7bec572fdee
Successfully built d7bec572fdee
Successfully tagged wohaoshuaihttpd:v0.-

[root@localhost images2]# docker inspect -f {{.Config.Cmd}} wohaoshuaihttpd:v0.-
[/bin/httpd -f -h ${WEB_DOC_ROOT}]

      基于制作的镜像启动容器

[root@localhost images2]# docker run --name wohaoshuai2 -it --rm -P wohaoshuaihttpd:v0.-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.- ./
Sending build context to Docker daemon .048kB
Step / : FROM busybox
---> af2f74c517aa
Step / : LABEL maintainer="wohaoshuai <wohaoshuai@qq.com>" app="httpd"
---> Using cache
---> b94158ebd25b
Step / : ENV WEB_DOC_ROOT="/data/web/html"
---> Using cache
---> 128dc125c148
Step / : RUN mkdir -p ${WEB_DOC_ROOT} && echo "<h1>Busybox httpd server.</h1>" > ${WEB_DOC_ROOT}/index.html
---> Using cache
---> 79e6d697305f
Step / : 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.-
[root@localhost images2]# docker run -it --rm -P --name wohaoshuai1 wohaoshuaihttpd:v0.- [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
root : /bin/httpd -f -h /data/web/html
root : /bin/sh
root : ps
/ # netstat -a
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp ::: :::* LISTEN
上一篇:java多线程并发编程


下一篇:Nginx服务安装配置