方式二
dockerfile就是用来构建docker镜像的构建文件,是一些命令和脚本。通过这个脚本可以生成镜像,镜像是一层一层的,脚本有一个个命令,每个命令就是一层。编写dockerfile命令,指令都是大写
FROM centosVOLUME ["volume01","volume02"]CMD echo "---end---"CMD /bin/bash
1.1 生成镜像build Build an image from a Dockerfile
[root@zhouhao docker-test-volume]# docker build -f /home/docker-test-volume/dockerfile01 -t kuangshen/centos:1.0 .Sending build context to Docker daemon 2.048kBStep 1/4 : FROM centos ---> 5d0da3dc9764Step 2/4 : VOLUME ["volume01","volume02"] ---> Running in 7540670d6792Removing intermediate container 7540670d6792 ---> fb0b219ac855Step 3/4 : CMD echo "---end---" ---> Running in 0195dea5b72bRemoving intermediate container 0195dea5b72b ---> 926350f7666dStep 4/4 : CMD /bin/bash ---> Running in 4513ae7da577Removing intermediate container 4513ae7da577 ---> b5f896be1be9Successfully built b5f896be1be9Successfully tagged kuangshen/centos:1.0
-f, --file string Name of the Dockerfile (Default is ‘PATH/Dockerfile’)-t, --tag list Name and optionally a tag in the ‘name:tag’ format 1.2 查看生成的镜像
[root@zhouhao docker-test-volume]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEkuangshen/centos 1.0 b5f896be1be9 about a minute ago 231MBtomcat-zhouhao 1.0 4a6332ef2158 22 hours ago 684MBtomcat latest 4ebba13e9156 2 days ago 680MBmysql 5.7 0712d5dc1b14 10 days ago 448MBnginx latest c316d5a335a5 10 days ago 142MBcentos latest 5d0da3dc9764 4 months ago 231MB
1.3 进入生产的镜像docker run -it b5f896be1be9 /bin/bash
发现容器内挂载生成的目录
[root@ecd870e627d2 /]# ls -ltotal 56lrwxrwxrwx 1 root root 7 Nov 3 2020 bin -> usr/bindrwxr-xr-x 5 root root 360 Feb 6 02:01 devdrwxr-xr-x 1 root root 4096 Feb 6 02:01 etcdrwxr-xr-x 2 root root 4096 Nov 3 2020 homelrwxrwxrwx 1 root root 7 Nov 3 2020 lib -> usr/liblrwxrwxrwx 1 root root 9 Nov 3 2020 lib64 -> usr/lib64drwx------ 2 root root 4096 Sep 15 14:17 lost+founddrwxr-xr-x 2 root root 4096 Nov 3 2020 mediadrwxr-xr-x 2 root root 4096 Nov 3 2020 mntdrwxr-xr-x 2 root root 4096 Nov 3 2020 optdr-xr-xr-x 86 root root 0 Feb 6 02:01 procdr-xr-x--- 2 root root 4096 Sep 15 14:17 rootdrwxr-xr-x 11 root root 4096 Sep 15 14:17 runlrwxrwxrwx 1 root root 8 Nov 3 2020 sbin -> usr/sbindrwxr-xr-x 2 root root 4096 Nov 3 2020 srvdr-xr-xr-x 13 root root 0 Feb 6 02:01 sysdrwxrwxrwt 7 root root 4096 Sep 15 14:17 tmpdrwxr-xr-x 12 root root 4096 Sep 15 14:17 usrdrwxr-xr-x 20 root root 4096 Sep 15 14:17 vardrwxr-xr-x 2 root root 4096 Feb 6 02:01 volume01drwxr-xr-x 2 root root 4096 Feb 6 02:01 volume02
发现宿主机的挂载路径
docker inspect ecd870e627d2
1.4 dockerfile小结我们通常会用dockerfile的形式构建自己的镜像构建镜像时没有挂载卷,就需要手动挂载,命令是:-v 卷名:容器内路径 二、数据卷容器
不同容器之间实现数据同步
2.1 启动centos镜像
启动centos01镜像
[root@zhouhao _data]# docker run -it --name centos01 kuangshen/centos:1.0[root@b9262e3651d6 /]#
Ctrl+P+Q不停止容器,并且退出容器
启动centos02镜像
–volumes-from list Mount volumes from the specified container(s)
docker run -it --volumes-from centos01 --name centos02 kuangshen/centos:1.0
在centos01容器中创建的文件,可以同步到centos02容器中centos01就是数据卷容器启动centos03镜像
docker run -it --name centos03 --volumes-from centos01 kuangshen/centos:1.0
该技术可用于多个MySQL或者redis之间同步数据库 三、Docker File 3.1 Docker File构建镜像的步骤
dockerfile是用来构建docker镜像的文件
构建步骤:
1.编写一个dockerfile文件
2.docker build 构建成为一个镜像
3.docker run 运行镜像
4.docker push 发布镜像,可以发布到DockerHub或者阿里云镜像仓库很多官方镜像都是基础包,基础功能都没有,我们通常会自己制作镜像 3.2 Docker File指令
关键字都必须是大写字母,按照从上到下的顺序执行
#是注释
每条指令会创建提交一个新的镜像层
做镜像就需要编写Docker File文件
FROM,基础镜像
MAINTAINER #镜像是谁写的,姓名+邮箱
RUN #镜像构建的时候,需要运行的命令
ADD #添加tomcat镜像,tomcat压缩包
WORKDIR #镜像的工作目录
VOLUME #挂载主机目录
EXPOSE #对外指定端口
CMD #指定容器运行时,要执行的命令,替换命令
ENTRYPOINT #指定容器运行时,要执行的命令,可以追加命令
onBUILE #当构建一个被继承的dockerfile,这个时候就会运行ONBUILD指令,是一个触发指令
COPY #类似ADD命令,将文件拷贝到镜像中
ENV #构建容器的时候设置环境变量
四、构建自己的centos docker hub中99%的镜像都是从FROM scratch镜像过来的
FROM centos:7MAINTAINER zhouhao<123@qq.com>ENV MYPATH /usr/localWORKDIR $MYPATHRUN yum -y install vimRUN yum -y install net-toolsEXPOSE 80CMD echo $MYPATHCMD echo "---end---"CMD /bin/bash
4.2 通过编写的Docker File文件构建镜像,通过docker build命令docker build -f mydockerfile-centos -t mycentos:0.1 .
-f, --file string Name of the Dockerfile (Default is ‘PATH/Dockerfile’)-t, --tag list Name and optionally a tag in the ‘name:tag’ format 4.3 查看并运行生成的镜像
[root@zhouhao dockerfile]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEmycentos 0.1 8bb2e3836d93 about an hour ago 515MB
docker run -it mycentos:0.1
直接到达环境设置中的工作目录
[root@25c7e4fecd8d local]# pwd/usr/local
具有ifconfig命令
[root@25c7e4fecd8d local]# ifconfigeth0: flags=4163
[root@zhouhao dockerfile]# docker history 8bb2e3836d93IMAGE CREATED CREATED BY SIZE COMMENT8bb2e3836d93 about an hour ago /bin/sh -c #(nop) CMD ["/bin/sh" "-c" "/bin… 0B 4c4b13db9c62 about an hour ago /bin/sh -c #(nop) CMD ["/bin/sh" "-c" "echo… 0B eabd0686f6bf about an hour ago /bin/sh -c #(nop) CMD ["/bin/sh" "-c" "echo… 0B 6eba2612b7e0 about an hour ago /bin/sh -c #(nop) EXPOSE 80 0B 855b33fbf440 about an hour ago /bin/sh -c yum -y install net-tools 155MB 48edb1b8f841 about an hour ago /bin/sh -c yum -y install vi 156MB 58621fa669cd about an hour ago /bin/sh -c #(nop) WORKDIR /usr/local 0B 8a9fd6406711 about an hour ago /bin/sh -c #(nop) ENV MYPATH=/usr/local 0B c190425f9770 about an hour ago /bin/sh -c #(nop) MAINTAINER zhouhao<123@qq… 0B eeb6ee3f44bd 4 months ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B
FROM centos:7CMD ["ls","-a"]
5.2 构建镜像注意:命令最后的点号容易忘记,表示当前目录
[root@zhouhao dockerfile]# docker build -f docker-cmd-test -t cmdtest .Sending build context to Docker daemon 3.072kBStep 1/2 : FROM centos:7 ---> eeb6ee3f44bdStep 2/2 : CMD ["ls","-a"] ---> Running in b448178a3749Removing intermediate container b448178a3749 ---> 6fa04f9a0ea8Successfully built 6fa04f9a0ea8Successfully tagged cmdtest:latest
5.3 查看构建好的镜像[root@zhouhao dockerfile]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEcmdtest latest 6fa04f9a0ea8 35 seconds ago 204MBmycentos 0.1 8bb2e3836d93 2 days ago 515MB
[root@zhouhao dockerfile]# docker run 6fa04f9a0ea8....dockerenvanaconda-post.logbindevetchomeliblib64mediamntoptprocrootrunsbinsrvsystmpusrvar
5.5 追加-l命令,报错[root@zhouhao ~]# docker run 6fa04f9a0ea8 -ldocker: Error response from daemon: OCI runtime create failed: container_linux.go:380: starting container process caused: exec: "-l": executable file not found in $PATH: unknown.
-l命令替换了CMD [“ls”,"-a"]命令,实质上执行的是-l命令,而-l不是一个命令 六、构建entrypoint命令的镜像 6.1 编写Docker File文件
FROM centosENTRYPOINT ["ls","-a"]
6.2 生成镜像文件[root@zhouhao dockerfile]# docker build -f docker-entrypoint-test -t entrypoint-test .Sending build context to Docker daemon 4.096kBStep 1/2 : FROM centos ---> 5d0da3dc9764Step 2/2 : ENTRYPOINT ["ls","-a"] ---> Running in a878562cb6faRemoving intermediate container a878562cb6fa ---> 2ce3b22fd611Successfully built 2ce3b22fd611Successfully tagged entrypoint-test:latest
6.3 运行镜像文件[root@zhouhao dockerfile]# docker run 2ce3b22fd611....dockerenvbindevetchomeliblib64lost+foundmediamntoptprocrootrunsbinsrvsystmpusrvar
6.4 追加-l命令,运行镜像文件[root@zhouhao ~]# docker run 2ce3b22fd611 -ltotal 56drwxr-xr-x 1 root root 4096 Feb 11 10:10 .drwxr-xr-x 1 root root 4096 Feb 11 10:10 ..-rwxr-xr-x 1 root root 0 Feb 11 10:10 .dockerenvlrwxrwxrwx 1 root root 7 Nov 3 2020 bin -> usr/bindrwxr-xr-x 5 root root 340 Feb 11 10:10 devdrwxr-xr-x 1 root root 4096 Feb 11 10:10 etcdrwxr-xr-x 2 root root 4096 Nov 3 2020 homelrwxrwxrwx 1 root root 7 Nov 3 2020 lib -> usr/liblrwxrwxrwx 1 root root 9 Nov 3 2020 lib64 -> usr/lib64drwx------ 2 root root 4096 Sep 15 14:17 lost+founddrwxr-xr-x 2 root root 4096 Nov 3 2020 mediadrwxr-xr-x 2 root root 4096 Nov 3 2020 mntdrwxr-xr-x 2 root root 4096 Nov 3 2020 optdr-xr-xr-x 93 root root 0 Feb 11 10:10 procdr-xr-x--- 2 root root 4096 Sep 15 14:17 rootdrwxr-xr-x 11 root root 4096 Sep 15 14:17 runlrwxrwxrwx 1 root root 8 Nov 3 2020 sbin -> usr/sbindrwxr-xr-x 2 root root 4096 Nov 3 2020 srvdr-xr-xr-x 13 root root 0 Feb 6 02:03 sysdrwxrwxrwt 7 root root 4096 Sep 15 14:17 tmpdrwxr-xr-x 12 root root 4096 Sep 15 14:17 usrdrwxr-xr-x 20 root root 4096 Sep 15 14:17 var
6.5 entrypoint命令与cmd命令的区别entrypoint可以直接在后面追加命令参数,而不是替换整个命令cmd命令会整个替换掉命令,不支持追加命令 七、实战:构建tomcat镜像 7.1 准备工作
准备镜像文件,tomcat压缩包,jdk压缩包tomcat下载地址:https://mirrors.huaweicloud.com/apache/tomcat/tomcat-9/v9.0.22/bin/jdk下载地址:https://mirrors.huaweicloud.com/java/jdk/8u151-b12/ 7.2 编写dockerfile文件
把文件命明为Dockerfile,这是官方命名,build会自动寻找这个文件,就不需要-f指定文件了
FROM centos:7MAINTAINER zhouhao<123@qq.com>COPY readme.txt /usr/local/readme.txtADD jdk-8u151-linux-x64.tar.gz /usr/local/ADD apache-tomcat-9.0.22.tar.gz /usr/local/ENV MYPATH /usr/local/WORKDIR $MYPATHENV JAVA_HOME /usr/local/jdk1.8.0_151ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jarENV CATALINA_HOME /usr/local/apache-tomcat-9.0.22ENV CATALINA_base /usr/local/apache-tomcat-9.0.22ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/binEXPOSE 8080CMD /usr/local/apache-tomcat-9.0.22/bin/startup.sh && tail -f /usr/local/apache-tomcat-9.0.22/logs/catalina.out
7.3 构建tomcat镜像[root@zhouhao tomcat]# docker build -t diytomcat .Sending build context to Docker daemon 200.7MBStep 1/14 : FROM centos:7 ---> eeb6ee3f44bdStep 2/14 : MAINTAINER zhouhao<123@qq.com> ---> Using cache ---> c190425f9770Step 3/14 : COPY readme.txt /usr/local/readme.txt ---> 7c03471412a4Step 4/14 : ADD jdk-8u151-linux-x64.tar.gz /usr/local/ ---> d0c79719de08Step 5/14 : ADD apache-tomcat-9.0.22.tar.gz /usr/local/ ---> 06fd3f81c15eStep 6/14 : ENV MYPATH /usr/local/ ---> Running in d82f18a94d54Removing intermediate container d82f18a94d54 ---> 8109021ee2c6Step 7/14 : WORKDIR $MYPATH ---> Running in da35f83b1705Removing intermediate container da35f83b1705 ---> e1dcfaa2ff68Step 8/14 : ENV JAVA_HOME /usr/local/jdk1.8.0_11 ---> Running in 0fcb2b9cc317Removing intermediate container 0fcb2b9cc317 ---> 486b3d24132cStep 9/14 : ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar ---> Running in 8ec07a30b084Removing intermediate container 8ec07a30b084 ---> 2c81c404773cStep 10/14 : ENV CATALINA_HOME /usr/local/apache-tomcat-9.0.22 ---> Running in f795daf83c54Removing intermediate container f795daf83c54 ---> ef9236f85235Step 11/14 : ENV CATALINA_base /usr/local/apache-tomcat-9.0.22 ---> Running in 87e6f1a4e8d1Removing intermediate container 87e6f1a4e8d1 ---> f9da85083198Step 12/14 : ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin ---> Running in 4345ae834319Removing intermediate container 4345ae834319 ---> 83ad9c8a482fStep 13/14 : EXPOSE 8080 ---> Running in b3c5d4fa1512Removing intermediate container b3c5d4fa1512 ---> f1c381220e87Step 14/14 : CMD /usr/local/apache-tomcat-9.0.22/bin/startup.sh && tail -f /usr/local/apache-tomcat-9.0.22/bin/logs/catalina.out ---> Running in e952c27f0b54Removing intermediate container e952c27f0b54 ---> 7fe3bef56dbbSuccessfully built 7fe3bef56dbbSuccessfully tagged diytomcat:latest
7.4 启动tomcat镜像[root@zhouhao tomcat]# docker run -d -p 9090:8080 --name tomcat-zhouhao -v /home/zhouhao/build/tomcat/test:/usr/local/apache-tomcat-9.0.22/webapps/test -v /home/zhouhao/build/tomcat/tomcatlogs:/usr/local/apache-tomcat-9.0.22/logs fb1b20aee317d245f6e24cfb5549a8ca8d0d470a34fdc3b59f4629acbbbb74e7021396cdfd49
查看启动成功的tomcat容器
[root@zhouhao tomcat]# docker psConTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESd245f6e24cfb fb1b20aee317 "/bin/sh -c '/usr/lo…" 7 seconds ago Up 6 seconds 0.0.0.0:9090->8080/tcp, :::9090->8080/tcp tomcat-zhouhao
7.5 进入到tomcat容器[root@zhouhao tomcat]# docker exec -it d245f6e24cfb /bin/bash
-it必须加上,否则无法进入,-it的意思是以交互模式进入容器 7.6 访问tomcat测试
[root@zhouhao tomcat]# curl localhost:9090 Tomcat Setup First Web Application Realms & AAA JDBC DataSources Examples Servlet Specifications Tomcat Versions For security, access to the manager webapp is restricted. Users are defined in:
Apache Tomcat/9.0.22
If you're seeing this, you've successfully installed Tomcat、Congratulations!
Recommended Reading:
Security Considerations How-To
Manager Application How-To
Clustering/Session Replication How-To
Server Status Manager App Host Manager
Developer Quick Start
Managing Tomcat
$CATALINA_HOME/conf/tomcat-users.xml
In Tomcat 9.0 access to the manager application is split between different users、 Read more...
Release Notes
Changelog
Migration Guide
Security Notices
八、发布镜像到dockerhub 8.1 登录docker login -u zhouhao123321Password: WARNING! Your password will be stored unencrypted in /root/.docker/config.json.Configure a credential helper to remove this warning、Seehttps://docs.docker.com/engine/reference/commandline/login/#credentials-storeLogin Succeeded
更改镜像的名字,docker tag 命令
docker tag diytomcat zhouhao123321/diytomcat[root@zhouhao ~]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEdiytomcat latest fb1b20aee317 about an hour ago 604MBzhouhao123321/diytomcat latest fb1b20aee317 about an hour ago 604MBentrypoint-test latest 2ce3b22fd611 28 hours ago 231MBcmdtest latest 6fa04f9a0ea8 35 hours ago 204MBmycentos 0.1 8bb2e3836d93 4 days ago 515MB
docker push zhouhao123321/diytomcatUsing default tag: latestThe push refers to repository [docker.io/zhouhao123321/diytomcat]
九、docker小结