WORKDIR
[root@docker01 /opt/dockerfile]# mkdir xiaoniao [root@docker01 /opt/dockerfile]# cd xiaoniao/ [root@docker01 /opt/dockerfile/xiaoniao]# vim dockerfile FROM centos:6.9 RUN curl -o /etc/yum.repos.d/epel.repo RUN curl -o /etc/yum.repos.d/CentOS-Base.repo RUN yum install nginx -y WORKDIR /usr/share/nginx/html RUN curl -o xiaoniaofeifei.zip RUN unzip xiaoniaofeifei.zip CMD ["nginx","-g","daemon off;"] [root@docker01 /opt/dockerfile/xiaoniao]# docker image build -t xiaoniao:v2 . 7.dockerfile3实践ADD
[root@docker01 /opt/dockerfile/xiaoniao]# cp -a /opt/xiaoniao . [root@docker01 /opt/dockerfile/xiaoniao]# ll total 4 -rw-r--r-- 1 root root 364 Dec 30 18:29 dockerfile drwxr-xr-x 3 root root 124 Dec 30 14:25 xiaoniao [root@docker01 /opt/dockerfile/xiaoniao]# vim dockerfile FROM centos:6.9 RUN curl -o /etc/yum.repos.d/epel.repo RUN curl -o /etc/yum.repos.d/CentOS-Base.repo RUN yum install nginx -y WORKDIR /usr/share/nginx/html ADD xiaoniao . CMD ["nginx","-g","daemon off;"] [root@docker01 /opt/dockerfile/xiaoniao]# docker image build -t xiaoniao:v3 . [root@docker01 /opt/dockerfile/xiaoniao]# docker run -d -p 82:80 xiaoniao:v3 f91022430a17a2568e07efd3ca490f843fdc43f273801080383f9ba593259945 [root@docker01 /opt/dockerfile/xiaoniao]# docker ps -a -l 8.dockerfile4实践VOLUME
# 查看挂载卷 啥也没有 [root@docker01 /opt/dockerfile/xiaoniao]# docker volume ls DRIVER VOLUME NAME # 编辑dockerfile [root@docker01 /opt/dockerfile/xiaoniao]# vim dockerfile FROM centos:6.9 RUN curl -o /etc/yum.repos.d/epel.repo RUN curl -o /etc/yum.repos.d/CentOS-Base.repo RUN yum install nginx -y ADD xiaoniao /usr/share/nginx/html VOLUME /usr/share/nginx/html CMD ["nginx","-g","daemon off;"] # 一键提交 [root@docker01 /opt/dockerfile/xiaoniao]# docker build -t xiaoniao:v4 . # 启动容器 用--name取个名字 后面好使用 好区分 [root@docker01 /opt/dockerfile/xiaoniao]# docker run --name xiaoniao -d -p 91:80 xiaoniao:v4 301f158a84b4757e0134443140da8a019f7e1fb3b0f4de9316082c73b0541f65 # 查看挂载卷 [root@docker01 /opt/dockerfile/xiaoniao]# docker volume ls DRIVER VOLUME NAME local efa95b60c21beb0cf8f942bf42f59a7f623c793380ad9e250d3057f3d0a028ae # 查看详情并修改页面 [root@docker01 /opt/dockerfile/xiaoniao]# docker container inspect xiaoniao "Source": "/var/lib/docker/volumes/efa95b60c21beb0cf8f942bf42f59a7f623c793380ad9e250d3057f3d0a028ae/_data", [root@docker01 /opt/dockerfile/xiaoniao]# cd /var/lib/docker/volumes/efa95b60c21beb0cf8f942bf42f59a7f623c793380ad9e250d3057f3d0a028ae/_data [root@docker01 _data]# vim index.html <title>大鸟飞飞</title> # 再次启动个容器 [root@docker01 _data]# docker stop xiaoniao [root@docker01 _data]# docker run -d -p 92:80 --volumes-from xiaoniao xiaoniao:v4 就算xiaoniao死了了 还是可以用别的端口跟xiaoniao挂一样的卷 9.dockerfile5实践EXPOSE
[root@docker01 /opt/dockerfile/xiaoniao]# vim dockerfile FROM centos:6.9 RUN curl -o /etc/yum.repos.d/epel.repo RUN curl -o /etc/yum.repos.d/CentOS-Base.repo RUN yum install nginx -y ADD xiaoniao /usr/share/nginx/html VOLUME /usr/share/nginx/html EXPOSE 80 22 CMD ["nginx","-g","daemon off;"] [root@docker01 /opt/dockerfile/xiaoniao]# docker build -t xiaoniao:v5 . # 给了随机端口映射 一个22 一个80 [root@docker01 /opt/dockerfile/xiaoniao]# docker run -d -P xiaoniao:v5 e7b882318f3f350499df9c96d65d09ace3a22a19b661e645f4bc875dbebb85a7 [root@docker01 /opt/dockerfile/xiaoniao]# docker ps -a -l CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES e7b882318f3f xiaoniao:v5 "nginx -g \'daemon of…" 28 seconds ago Up 26 seconds 0.0.0.0:4001->22/tcp, 0.0.0.0:4000->80/tcp eager_pasteur 10.dockerfile6实践ENTRYPOINT
初始化命令可以被覆盖 如果不小心输错 就会一直处于错误状态
[root@docker01 /opt/dockerfile/xiaoniao]# vim dockerfile FROM centos:6.9 RUN curl -o /etc/yum.repos.d/epel.repo RUN curl -o /etc/yum.repos.d/CentOS-Base.repo RUN yum install nginx -y ADD xiaoniao /usr/share/nginx/html VOLUME /usr/share/nginx/html EXPOSE 80 22 ENTRYPOINT ["nginx","-g","daemon off;"] [root@docker01 /opt/dockerfile/xiaoniao]# docker build -t xiaoniao:v6 . # 后面填错了还是能起来 但是会秒死 [root@docker01 /opt/dockerfile/xiaoniao]# docker run -d -P xiaoniao:v6 dslakljdkj d6f482adf9d26da7df76ed7af7ce84734326a7917b8c8c00864c4bb26fcb90b3 [root@docker01 /opt/dockerfile/xiaoniao]# docker ps -a -l Exited # 可以用脚本传参方式解决 [root@docker01 /opt/dockerfile/xiaoniao]# vim dockerfile FROM centos:6.9 RUN curl -o /etc/yum.repos.d/epel.repo RUN curl -o /etc/yum.repos.d/CentOS-Base.repo RUN yum install nginx -y ADD xiaoniao /usr/share/nginx/html VOLUME /usr/share/nginx/html EXPOSE 80 22 ADD init.sh /init.sh ENTRYPOINT ["/bin/bash","/init.sh"] [root@docker01 /opt/dockerfile/xiaoniao]# vim init.sh #!/bin/bash nginx -g \'daemon off;\' # 查看状态 存活 [root@docker01 /opt/dockerfile/xiaoniao]# docker build -t xiaoniao:v7 . [root@docker01 /opt/dockerfile/xiaoniao]# docker ps -a -l CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES aef1ff663faf xiaoniao:v8 "/bin/bash /init.sh …" 6 seconds ago Up 5 seconds 0.0.0.0:4013->22/tcp, 0.0.0.0:4012->80/tcp romantic_newton 11.dockerfile7实践