一、镜像的生成途径

  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.-
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

    

    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(以此镜像启动的容器会自动挂载卷至声明的路径中)

    

[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

    

[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

    

    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 :相当于在镜像中执行相应命令

    

[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 定义一个镜像文件要启动为容器时默认要运行的程序或命令,可以给多个,但是只有最后一个会生效

    

    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

Docker 学习7 Dockerfile详解的更多相关文章

  1. Docker 学习8 Dockerfile详解2

    一.继续上章节Docker学习7 CMD命令后. 11.ENTRYPOINT a.容器启动后相当于会启动ENTRYPOINT + CMD 命令,CMD相当于参数传给entrypoint的 [root@ ...

  2. Docker系列07—Dockerfile 详解

    本文收录在容器技术学习系列文章总目录 1.认识Dockerfile 1.1 镜像的生成途径 基于容器制作  dockerfile,docker build 基于容器制作镜像,已经在上篇Docker系列 ...

  3. docker学习(3)--Dockfile详解

    转载请注明出处:http://www.cnblogs.com/lighten/p/6900556.html 1.基本说明 Dockfile是一个用于编写docker镜像生成过程的文件,其有特定的语法. ...

  4. Docker 学习之命令详解

    1. docker version docker version 显示 Docker 版本信息. 2. docker info docker info 显示 Docker 系统信息,包括镜像和容器数. ...

  5. Docker入门02——Dockerfile详解

    基本示例 FROM MAINTAINER LABEL RUN ADD COPY CMD ENTRYPOINT ENV EXPOSE VOLUME WORKDIR USER ARG 基本示例 # Thi ...

  6. [转帖]Docker学习之Dockerfile命令详解

    Docker学习之Dockerfile命令详解 https://it.baiked.com/system/docker/2436.html 图挺好的 前言 之前,制作镜像的伪姿势搭建已经见过了,今天介 ...

  7. Dockerfile详解

    Dockerfile详解 利用Dockerfile文件,可以构建docker的image镜像 命令使用 通过-f参数指定Dockerfile路径,进行构建image docker build -f / ...

  8. Dockerfile详解及优化

    Dockerfile详解 0. Dockerfile的作用 docker可以根据Dockerfile中的指令来构建docker镜像.Dockerfile是一个文本文件,其应当包含用户想要构建一个镜像的 ...

  9. 最佳实战Docker持续集成图文详解

    最佳实战Docker持续集成图文详解 这是一种真正的容器级的实现,这个带来的好处,不仅仅是效率的提升,更是一种变革:开发人员第一次真正为自己的代码负责——终于可以跳过运维和测试部门,自主维护运行环境( ...

随机推荐

  1. ORA-00923: FROM keyword not found where expected(单双引号)

    1.前提 在学习oracel的过程中遇到的一个关于单双引号的问题 备注一下 2.学习过程中创建表语句是这样的 create table DEPT_DML --部门表( DEPT_NO NUMBER(8 ...

  2. SaltStack 介绍和安装

    SaltStack 介绍和安装 SaltStack 介绍 SaltStack是一种利用Python语言开发的,用于批量管理主机的一套工具,主要实现三种功能: 远程执行:通过saltstack工具,可以 ...

  3. sklearn中的损失函数

    python风控评分卡建模和风控常识(博客主亲自录制视频教程) https://study.163.com/course/introduction.htm?courseId=1005214003&am ...

  4. 模拟赛20181016 dp

    给出1-n的序列插入一个bst: 给出T组询问,包含n,h分别代表点数为n,高度为h的树,求所有插入顺序的合法方案数,模1e9+7 样例输入 1 2  1 样例输出 2 #include<bit ...

  5. webpack的按需引入配置

    ant.design插件需要less配合,yarn add babel-plugin-import,webpack4.0的babel文件已经配置到webpackconfig.js中,需要eject暴露 ...

  6. CodeChef - COUNTREL Count Relations

    题目链接 给你一个长为NNN的1,2,3,....N1,2,3,....N1,2,3,....N的序列,让你求出两种关系各个有多少可能: R1R_1R1​,由于x,yx,yx,y互不是子集,且交集为空 ...

  7. 开源litemall学习

    1参数拼装 https://blog.yeskery.com/articles/345298282 WxWebMvcConfiguration HandlerMethodArgumentResolve ...

  8. 【easy】198. House Robber 123总结……

    题目一: 一个极其简单的动态规划. class Solution { public: int rob(vector<int>& nums) { ; // 表示没有选择当前house ...

  9. zabbix3.2自动发现批量监控redis端口状态

    使用nmap提示被防火墙阻挡,实际没有启用防火墙 [root@eus_chinasoft_haproxy:/usr/local/aegis]# nmap 172.20.103.202 -p 7000 ...

  10. WPF 10天修炼 第四天- WPF布局容器

    WPF布局 WPF的窗口也就是Window类,是一个内容控件,该控件派生自ContentControl.内容控件有一个Content属性,该属性有一个限制,只能放置一个用户界面元素,或一个字符串.为了 ...