Docker基础Part5——DockerFlie [TOC]
DockerFile介绍 Dockerfile 是一个用来构建镜像的文本文件,文本内容包含了一条条构建镜像所需的指令和说明。
Dockerfile 是面向开发的,Docker镜像逐渐成为企业交付、发布项目的标准。
构建步骤:
编写 dockerfile 文件
docker build 构建镜像
docker run 运行镜像
docker push 发布镜像(DockerHub 、阿里云仓库)
每一个指令都会创建一个新的镜像层并提交。
DockerFile指令 1 2 3 4 5 6 7 8 9 10 11 12 FROM MAINTAINER RUN ADD WORKDIR VOLUME EXPOSE CMD ENTRYPOINT ONBUILD COPY ENV
实战测试
DockerHub中大部分镜像都是从FROM scratch
开始。
关键字不一定要大写,但是一般都是大写来和变量区分。
官方的centos镜像没有vim、ifconfig
命令,这里制作自己的centos镜像。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 [root@VM-1-14-centos dockerfile] [root@VM-1-14-centos dockerfile] FROM centos MAINTAINER NephrenCake<834957063@qq.com> ENV MYPATH /usr/local WOKDIR $MYPATH run yum -y install vim RUN yum -y install net-tools EXPOSE 80 CMD echo $MYPATH CMD echo "-----end-----" CMD /bin/bash docker build -f mydockerfile_centos -t mycentos:0.1 . ...... Successfully built 0fb03cb15b56 Successfully tagged mycentos:0.1 [root@VM-1-14-centos dockerfile] [root@6db4033677cd local ] /usr/local [root@6db4033677cd local ] ...... [root@6db4033677cd local ] ...... [root@VM-1-14-centos dockerfile] IMAGE CREATED CREATED BY SIZE COMMENT 0fb03cb15b56 25 minutes ago /bin/sh -c 857a02979c6b 25 minutes ago /bin/sh -c 9b1d90e765f1 25 minutes ago /bin/sh -c 383a1fdc4420 25 minutes ago /bin/sh -c 315a1c8eafe2 25 minutes ago /bin/sh -c yum -y install net-tools 23.3MB 1bd3533bf951 25 minutes ago /bin/sh -c yum -y install vim 58MB e7468e6156a1 26 minutes ago /bin/sh -c 02c89bb6f0d5 26 minutes ago /bin/sh -c 1e2c46d0633d 26 minutes ago /bin/sh -c 300e315adb2f 4 months ago /bin/sh -c <missing> 4 months ago /bin/sh -c <missing> 4 months ago /bin/sh -c
建议在dockerfile所在目录构建镜像。
对所有拿到的镜像都可以docker history
来分析其构建过程。
CMD 和 ENTRYPOINT区别
test CMD:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 [root@VM-1-14-centos dockerfile] FROM centos CMD ["ls" ,"-a" ] [root@VM-1-14-centos dockerfile] $ docker run cmd-test:0.1 . .. .dockerenv bin dev ...... [root@VM-1-14-centos dockerfile] docker: Error response from daemon: OCI runtime create failed: container_linux.go:349: starting container process caused "exec: \"-l\": executable file not found in $PATH " : unknown.ERRO[0000] error waiting for container: context canceled [root@VM-1-14-centos dockerfile] total 48 lrwxrwxrwx 1 root root 7 Nov 3 15:22 bin -> usr/bin drwxr-xr-x 5 root root 340 Apr 25 03:10 dev drwxr-xr-x 1 root root 4096 Apr 25 03:09 etc drwxr-xr-x 2 root root 4096 Nov 3 15:22 home ...... [root@VM-1-14-centos dockerfile] CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 9ce9877f6610 cmd-test:0.1 "ls -l" 11 seconds ago Exited (0) 10 seconds ago interesting_knuth
test ENTRYPOINT:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 [root@VM-1-14-centos dockerfile] FROM centos ENTRYPOINT ["ls" ,"-a" ] [root@VM-1-14-centos dockerfile] [root@VM-1-14-centos dockerfile] . .. .dockerenv bin dev ...... [root@VM-1-14-centos dockerfile] total 56 drwxr-xr-x 1 root root 4096 May 16 06:32 . drwxr-xr-x 1 root root 4096 May 16 06:32 .. -rwxr-xr-x 1 root root 0 May 16 06:32 .dockerenv ......
Tomcat实战 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 Tomcat jdk dockerfile readme FROM centos MAINTAINER cheng<1204598429@qq.com> COPY README /usr/local /README ADD jdk-8u231-linux-x64.tar.gz /usr/local / ADD apache-tomcat-9.0.35.tar.gz /usr/local / RUN yum -y install vim ENV MYPATH /usr/local WORKDIR $MYPATH ENV JAVA_HOME /usr/local /jdk1.8.0_231 ENV CATALINA_HOME /usr/local /apache-tomcat-9.0.35 ENV PATH $PATH :$JAVA_HOME /bin:$CATALINA_HOME /lib EXPOSE 8080 CMD /usr/local /apache-tomcat-9.0.35/bin/startup.sh && tail -F /usr/local /apache-tomcat-9.0.35/logs/catalina.out $ docker build -t mytomcat:0.1 . $ docker run -d -p 8080:8080 --name tomcat01 -v /home/kuangshen/build/tomcat/test :/usr/local /apache-tomcat-9.0.35/webapps/test -v /home/kuangshen/build/tomcat/tomcatlogs/:/usr/local /apache-tomcat-9.0.35/logs mytomcat:0.1
发布镜像 登录 注册DockerHub
1 2 3 4 5 6 docker login [OPTIONS] [SERVER] Options: -p, --password string Password --password-stdin Take the password from stdin -u, --username string Username
提交 1 2 3 4 5 6 7 docker push xxx/image:[镜像版本号] $ docker build -t nephrencake/mytomcat:1.0 . $ docker tag 容器id nephrencake/mytomcat:1.0
修改版本 1 2 3 4 5 $ docker tag [ImageId] registry.cn-xxx.aliyuncs.com/xxxx/image:[镜像版本号] $ sudo docker tag a5ef1f32aaae registry.cn-xxx.aliyuncs.com/xxxx/image:1.0 $ docker push registry.cn-xxx.aliyuncs.com/xxxx/image:[镜像版本号]
打包镜像 1 2 3 4 docker save [OPTIONS] IMAGE [IMAGE...] Options: -o, --output string Write to a file, instead of STDOUT
小结