前言:
推荐免费Docker基础讲解视频:【狂神说Java】Docker最新超详细版教程通俗易懂_哔哩哔哩_bilibili
构建自己的centOS
Docker Hub中99%的镜像都是从这个基础镜像过来的FROM scratch,然后配置需要的软件和配置来进行构建的;
创建一个自己的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 # 编写dockerfile配置文件 [root@jokerdig dockerfile]# vim mydockerfile-centos FROM centos:7 MAINTAINER jokerdig<jokerdig@163.com> ENV MYPATH /usr/local WORKDIR $MYPATH RUN yum -y install vim # 安装vim RUN yum -y install net-tools # 安装net-tools EXPOSE 80 CMD echo $MYPATH CMD echo "---------end-----" CMD /bin/bash # 查看 [root@jokerdig dockerfile]# cat mydockerfile-centos FROM centos:7 MAINTAINER jokerdig<jokerdig@163.com> ENV MYPATH /usr/local WORKDIR $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说明生成成功 Successfully built 2f2745999e39 Successfully tagged mycentos:0.1 # 查看镜像 [root@jokerdig dockerfile]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE mycentos 0.1 2f2745999e39 56 seconds ago 467MB
测试运行
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 [root@jokerdig dockerfile]# docker run -it 2f2745999e39 [root@465fadcbf43e local]# pwd /usr/local [root@465fadcbf43e local]# ls bin games lib libexec share etc include lib64 sbin src [root@465fadcbf43e local]# ifconfig eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 172.18.0.5 netmask 255.255.0.0 broadcast 172.18.255.255 ether 02:42:ac:12:00:05 txqueuelen 0 (Ethernet) RX packets 13 bytes 1120 (1.0 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 .......................
查看镜像构建历史
1 2 3 4 5 6 7 8 9 10 11 12 13 14 [root@jokerdig dockerfile]# docker history 2f2745999e39 IMAGE CREATED CREATED BY SIZE COMMENT 2f2745999e39 5 minutes ago /bin/sh -c #(nop) CMD ["/bin/sh" "-c" "/bin… 0B 521f7c8be542 5 minutes ago /bin/sh -c #(nop) CMD ["/bin/sh" "-c" "echo… 0B c8ffab1d789e 5 minutes ago /bin/sh -c #(nop) CMD ["/bin/sh" "-c" "echo… 0B 3c671c292043 5 minutes ago /bin/sh -c #(nop) EXPOSE 80 0B 03c54a82312a 5 minutes ago /bin/sh -c yum -y install net-tools 25MB 7d69a6745e2d 6 minutes ago /bin/sh -c yum -y install vim 238MB a5ada6508f68 6 minutes ago /bin/sh -c #(nop) WORKDIR /usr/local 0B 5c31bb070341 6 minutes ago /bin/sh -c #(nop) ENV MYPATH=/usr/local 0B 0975e3513911 6 minutes ago /bin/sh -c #(nop) MAINTAINER jokerdig<joker… 0B eeb6ee3f44bd 12 months ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B <missing> 12 months ago /bin/sh -c #(nop) LABEL org.label-schema.sc… 0B <missing> 12 months ago /bin/sh -c #(nop) ADD file:b3ebbe8bd304723d4… 204MB
CMD和ENTRYPOINT的区别
测试CMD和ENTRYPOINT的区别
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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 # 编写配置文件 [root@jokerdig dockerfile]# vim mydockerfile-test [root@jokerdig dockerfile]# cat mydockerfile-test FROM centos CMD ["ls","-a"] # 构建 [root@jokerdig dockerfile]# docker build -f mydockerfile-test -t cmdtest . Sending build context to Docker daemon 3.072kB Step 1/2 : FROM centos ---> 5d0da3dc9764 Step 2/2 : CMD ["ls","-a"] ---> Running in 51bc7af240e5 Removing intermediate container 51bc7af240e5 ---> 22524f27f2c5 Successfully built 22524f27f2c5 Successfully tagged cmdtest:latest # 运行,打印所有目录 [root@jokerdig dockerfile]# docker run 22524f27f2c5 . .. .dockerenv bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var # 我们想追加一个命令 ls 追加 -l [root@jokerdig dockerfile]# docker run 22524f27f2c5 -l # 但是它直接报错了,因为使用CMD的时候,-l 直接替换了 ls -a 的命令 docker: Error response from daemon: failed to create shim task: OCI runtime create failed: runc create failed: unable to start container process: exec: "-l": executable file not found in $PATH: unknown. ERRO[0000] error waiting for container: context canceled # 正确的命令 [root@jokerdig dockerfile]# docker run 22524f27f2c5 ls -al total 56 drwxr-xr-x 1 root root 4096 Sep 26 05:02 . drwxr-xr-x 1 root root 4096 Sep 26 05:02 .. -rwxr-xr-x 1 root root 0 Sep 26 05:02 .dockerenv lrwxrwxrwx 1 root root 7 Nov 3 2020 bin -> usr/bin drwxr-xr-x 5 root root 340 Sep 26 05:02 dev drwxr-xr-x 1 root root 4096 Sep 26 05:02 etc drwxr-xr-x 2 root root 4096 Nov 3 2020 home lrwxrwxrwx 1 root root 7 Nov 3 2020 lib -> usr/lib lrwxrwxrwx 1 root root 9 Nov 3 2020 lib64 -> usr/lib64 drwx------ 2 root root 4096 Sep 15 2021 lost+found drwxr-xr-x 2 root root 4096 Nov 3 2020 media drwxr-xr-x 2 root root 4096 Nov 3 2020 mnt drwxr-xr-x 2 root root 4096 Nov 3 2020 opt dr-xr-xr-x 198 root root 0 Sep 26 05:02 proc dr-xr-x--- 2 root root 4096 Sep 15 2021 root drwxr-xr-x 11 root root 4096 Sep 15 2021 run lrwxrwxrwx 1 root root 8 Nov 3 2020 sbin -> usr/sbin drwxr-xr-x 2 root root 4096 Nov 3 2020 srv dr-xr-xr-x 13 root root 0 Sep 26 05:02 sys drwxrwxrwt 7 root root 4096 Sep 15 2021 tmp drwxr-xr-x 12 root root 4096 Sep 15 2021 usr drwxr-xr-x 20 root root 4096 Sep 15 2021 var
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 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 # 通过ENTRYPOINT构建 [root@jokerdig dockerfile]# vim mydockerfile-test2 [root@jokerdig dockerfile]# cat mydockerfile-test2 FROM centos ENTRYPOINT ["ls","-a"] # 构建 [root@jokerdig dockerfile]# docker build -f mydockerfile-test2 -t cmdtest . Sending build context to Docker daemon 4.096kB Step 1/2 : FROM centos ---> 5d0da3dc9764 Step 2/2 : ENTRYPOINT ["ls","-a"] ---> Running in 68a368c2694a Removing intermediate container 68a368c2694a ---> 263bc176c4b9 Successfully built 263bc176c4b9 Successfully tagged cmdtest:latest # 追加-l命令,直接-l也可以追加命令,不会替换原本的命令; [root@jokerdig dockerfile]# docker run 263bc176c4b9 -l total 56 drwxr-xr-x 1 root root 4096 Sep 26 05:05 . drwxr-xr-x 1 root root 4096 Sep 26 05:05 .. -rwxr-xr-x 1 root root 0 Sep 26 05:05 .dockerenv lrwxrwxrwx 1 root root 7 Nov 3 2020 bin -> usr/bin drwxr-xr-x 5 root root 340 Sep 26 05:05 dev drwxr-xr-x 1 root root 4096 Sep 26 05:05 etc drwxr-xr-x 2 root root 4096 Nov 3 2020 home lrwxrwxrwx 1 root root 7 Nov 3 2020 lib -> usr/lib lrwxrwxrwx 1 root root 9 Nov 3 2020 lib64 -> usr/lib64 drwx------ 2 root root 4096 Sep 15 2021 lost+found drwxr-xr-x 2 root root 4096 Nov 3 2020 media drwxr-xr-x 2 root root 4096 Nov 3 2020 mnt drwxr-xr-x 2 root root 4096 Nov 3 2020 opt dr-xr-xr-x 198 root root 0 Sep 26 05:05 proc dr-xr-x--- 2 root root 4096 Sep 15 2021 root drwxr-xr-x 11 root root 4096 Sep 15 2021 run lrwxrwxrwx 1 root root 8 Nov 3 2020 sbin -> usr/sbin drwxr-xr-x 2 root root 4096 Nov 3 2020 srv dr-xr-xr-x 13 root root 0 Sep 26 05:05 sys drwxrwxrwt 7 root root 4096 Sep 15 2021 tmp drwxr-xr-x 12 root root 4096 Sep 15 2021 usr drwxr-xr-x 20 root root 4096 Sep 15 2021 var
通过测试可以看出:
CMD和ENTRYPOINT都可以执行其中的命令;
如果在运行的时候追加命令,CMD会报错,而ENTRYPOINT会将追加的命令拼接在其文件中的命令之后,并且会正常运行;
Dockerfile制作tomcat镜像
准备tomcat的压缩包,jdk压缩包
1 2 [root@jokerdig tomcat]# ls apache-tomcat-8.5.82.tar.gz jdk-8u341-linux-x64.tar.gz
编写Dockerfile文件
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 [root@jokerdig tomcat]# touch readme.txt # Dockerfile为官方命名 [root@jokerdig tomcat]# vim Dockerfile FROM centos:7 MAINTAINER jokerdig<jokerdig@163.com> COPY readme.txt /usr/local/readme.txt ADD jdk-8u341-linux-x64.tar.gz /usr/local/ ADD apache-tomcat-8.5.82.tar.gz /usr/local/ CMD yum -y install vim ENV MYPATH /usr/local WORKDIR $MYPATH ENV JAVA_HOME /usr/local/jdk1.8.0_341 ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar ENV CATALINA_HOME /usr/local/apache-tomcat-8.5.82 ENV CATALINA_BASE /usr/local/apache-tomcat-8.5.82 ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin EXPOSE 8080 CMD /usr/local/apache-tomcat-8.5.82/bin/startup.sh && ta il -F /usr/local/apache-tomcat-8.5.82/bin/logs/catalina. out # 构建tomcat [root@jokerdig tomcat]# docker build -t diytomcat . ....... Successfully built 7d8317896cad Successfully tagged diytomcat:latest # 构建成功 # 查看镜像 [root@jokerdig tomcat]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE diytomcat latest 7d8317896cad 4 minutes ago 589MB # 启动tomcat [root@jokerdig tomcat]# docker run -d -p 8080:8080 --name diytomcat -v /home/tomcat/test:/usr/local/apache-tomcat-8.5.82/webapps/test -v /home/tomcat/logs:/usr/local/apache-tomcat-8.5.82/logs diytomcat 676756a52196d2f7d53707057c7d7397a46b73c78d7481ed924410ad8be4dff3 [root@jokerdig tomcat]# # 查看我们挂载的logs和test 两个目录 [root@jokerdig tomcat]# ls apache-tomcat-8.5.82.tar.gz Dockerfile jdk-8u341-linux-x64.tar.gz logs readme.txt test
访问测试
发布镜像到DockerHub
注册Dockerhub账号;
Docker Hub Container Image Library | App Containerization
登录;
1 2 3 4 5 6 7 [root@jokerdig tomcat]# docker login -u jokerdig #(你的用户名) 然后回车 Password: # (输入时密码不会显示) WARNING! Your password will be stored unencrypted in /root/.docker/config.json. Configure a credential helper to remove this warning. See https://docs.docker.com/engine/reference/commandline/login/#credentials-store Login Succeeded
在服务器上提交镜像;
1 2 3 4 5 6 7 8 9 10 11 12 # 修改镜像版本号 [root@jokerdig tomcat]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE diytomcat latest 7d8317896cad 28 minutes ago 589MB [root@jokerdig tomcat]# docker tag diytomcat:latest jokerdig/diytomcat:1.0 [root@jokerdig tomcat]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE jokerdig/diytomcat 1.0 7d8317896cad 28 minutes ago 589MB # 提交镜像(速度很慢) [root@jokerdig tomcat]# docker push jokerdig/diytomcat:1.0 The push refers to repository [docker.io/jokerdig/diytomcat] ........
发布镜像到阿里云容器服务
登录阿里云,找到镜像容器服务
选择个人实例,设置密码
创建命名空间
创建镜像仓库
推送步骤(根据你自己的页面步骤)
退出Dockerhub登录
1 2 [root@jokerdig tomcat]# docker logout Removing login credentials for https://index.docker.io/v1/
登录阿里云
1 2 3 4 5 6 7 8 # 注意,这里复制你自己的 [root@jokerdig tomcat]# docker login --username=joker大 雄 registry.cn-wulanchabu.aliyuncs.com Password: WARNING! Your password will be stored unencrypted in /root/.docker/config.json. Configure a credential helper to remove this warning. See https://docs.docker.com/engine/reference/commandline/login/#credentials-store Login Succeeded
设置镜像tag
1 2 3 4 5 6 # 设置镜像tag,这里复制你自己的 [root@jokerdig tomcat]# docker tag 7d8317896cad registry.cn-wulanchabu.aliyuncs.com/jokerdig/docker-test:1.0 # 查看镜像 [root@jokerdig tomcat]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE registry.cn-wulanchabu.aliyuncs.com/jokerdig/docker-test 1.0 7d8317896cad 47 minutes ago 589MB
提交镜像
1 2 # 提交镜像,这里复制你自己的 [root@jokerdig tomcat]# docker push registry.cn-wulanchabu.aliyuncs.com/jokerdig/docker-test:1.0
小结