Docker自学文档
学前零星使用操作
一、基础镜像篇
二、基础容器篇
三、容器数据卷
四、DockerFile构建镜像
五、网络篇
六、Docker Compose
docker 在已经启动的容器中开启端口映射
ubuntu22.04使用docker-compose部署最美博客系统
本文档使用 MrDoc 发布
-
+
home page
四、DockerFile构建镜像
## 1、什么是dockerfile DockerFile 是一种被Docker程序解释的脚本,DockerFile是由一条条的命令组成的,每条命令对应linux下面的一条命令,Docker程序将这些DockerFile指令再翻译成真正的linux命令,其有自己的书写方式和支持的命令,Docker程序读取DockerFile并根据指令生成Docker镜像,相比手动制作镜像的方式,DockerFile更能直观的展示镜像是怎么产生的,有了DockerFile,当后期有额外的需求时,只要在之前的DockerFile添加或者修改响应的命令即可重新生成新的Docke镜像,避免了重复手动制作镜像的麻烦,类似与shell脚本一样,可以方便高效的制作镜像 #### **Dockerfile 镜像制作和使用流程** ![](/media/202309/image-20200127141358742-1580191807589-780x384.png) #### **Dockerfile文件的制作镜像的分层结构** ![](/media/202309/镜像制作分层结构-780x326.jpg) ## 2、dockerfile指令解读 ![](/media/202309/image-20230810170711842.png) ``` #### 2.1.FROM 功能为指定基础镜像,并且必须是第一条指令。 如果不以任何镜像为基础,那么写法为:FROM scratch。 同 时意味着接下来所写的指令将作为镜像的第一层开始 #### 2.2.MAINTAINER 镜像创建维护者信息 #### 2.3.RUN 功能为运行指定的命令 注意:多行命令不要写多个RUN,原因是Dockerfile中每一个指令都会建立一层. 多少个RUN就构建了多 少层镜像,会造成镜像的臃肿、多层,不仅仅增加了构件部署的时间,还容易出错。 RUN书写时的换行符是\ #### 2.4.CMD 功能为容器启动时要运行的命令 注意:补充细节:这里边包括参数的一定要用双引号,就是",不能是单引号。千万不能写成单引号。 原因是参数传递后,docker解析的是一个JSON array #### 2.5.RUN和CMD的区别 不要把RUN和CMD搞混了。 RUN是构件容器时就运行的命令以及提交运行结果 CMD是容器启动时执行的命 令,在构件时并不运行,构件时紧紧指定了这个命令到底是个什么样子 #### 2.6.LABEL 功能是为镜像指定标签,为镜像写一些注释信息 但是并不建议这样写,最好就写成一行,如太长需要换行的话则使用\符号 如下: 注意:LABEL会继承基础镜像种的LABEL,如遇到key相同,则值覆盖 #### 2.7.EXPOSE 功能为暴漏容器运行时的监听端口给外部 但是EXPOSE并不会vim 使容器访问主机的端口 如果想使得容器与主 机的端口有映射关系,必须在容器启动的时候加上 -P参数 注意:如果在端口号后面加/tcp,默认为tcp协议,如果需要UDP端口需要添加/udp #### 2.8.ENV 功能为设置环境变量 #### 2.9.ADD 一个复制命令,把文件复制到镜象中。 如果把虚拟机与容器想象成两台linux服务器的话,那么这个命令就类似 于scp,只是scp需要加用户名和密码的权限验证,而ADD不用。 注意:尽量不要把写成一个文件夹,如果是一个文件夹了,复制整个目录的内容,包括文件系统元数据 #### 2.10.WORKDIR 设置工作目录,对RUN,CMD,ENTRYPOINT,COPY,ADD生效。如果不存在则会创建,也可以设置多次 #### 2.11.VOLUME 可实现挂载功能,可以将内部文件夹挂载到外部 #### 2.12.ENTRYPOINT 该命令与CMD类似,用于执行命令使用,还可以与CMD命令一起拼合使用 它与CMD的区别: 相同点:只能写一条,如果写多条,那么只有最后一条生效 不同点:CMD在创建容器时,在后面添加其他的CMD指令,CMD会被覆盖,但是ENTRYPOINT不会被覆盖,如果两个同时使用,CMD会变成ENTRYPOINT的参数 #### 2.13.ONBUILD 当构建一个被继承 Dcokerfile 这个时候就会触发运行ONBUILD命令。 #### 2.14.COPY 类似ADD命令,将文件拷贝到镜像中 ``` ## 3.使用DockerFile构建自己的镜像 Docker Hub中99%镜像都是从这个基础镜像过来的FROM scratch,然后配置需要的软件和配置来进行构建 ### 3.1.创建一个自己的centos镜像 官方镜像属于极限压缩版缺少一些命令,我们构建一个自己的镜像添加一些命令,比如vim命令和ifconfig命令 #### 3.1.1.创建工作目录 ``` [root@localhost ~]# mkdir dockerfile [root@localhost ~]# cd dockerfile/ [root@localhost dockerfile]# ``` #### 3.1.2.创建dockerfile文件 ``` [root@localhost dockerfile]# touch dockerfile-centos ``` #### 3.1.3.编辑dockerfile文件 ``` [root@localhost dockerfile]# vim dockerfile-centos FROM centos:7 MAINTAINER zxw<zxw@xxjsjl.cn> #环境变量 ENV MYPATH /usr/local WORKDIR $MYPATH #添加执行过程命令等 RUN yum install -y vim RUN yum install -y net-tools #暴露端口 EXPOSE 80 #启动时运行的命令 CMD echo $MYPATH CMD echo "-------end-------" CMD /bin/bash ``` #### 3.1.4.构建镜像 ``` [root@localhost dockerfile]# docker build -f dockerfile-centos -t mycentos:0.1 . [+] Building 40.5s (8/8) FINISHED docker:default => [internal] load .dockerignore 0.0s => => transferring context: 2B 0.0s => [internal] load build definition from dockerfile-centos 0.0s => => transferring dockerfile: 435B 0.0s => [internal] load metadata for docker.io/library/centos:7 16.0s => CACHED [1/4] FROM docker.io/library/centos:7@sha256:9d4bcbbb213dfd745b58be38b13b996ebb5ac315fe757 0.0s => [2/4] WORKDIR /usr/local 0.1s => [3/4] RUN yum install -y vim 21.1s => [4/4] RUN yum install -y net-tools 1.9s => exporting to image 1.2s => => exporting layers 1.2s => => writing image sha256:13f3ef13eced3e0de61356fea7ee7ec097d1af1caa85c980994c08762e3d4c9b 0.0s => => naming to docker.io/library/mycentos:0.1 0.0s [root@localhost dockerfile]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE mycentos 0.1 13f3ef13eced 23 seconds ago 666MB ``` #### 3.1.5.使用自己的镜像创建一个容器,并检查添加的命令是否存在 ``` [root@localhost dockerfile]# docker run -d -it --name mycentos mycentos:0.1 e88e58afbc0f58f9886876ba3f910bd2f35a842851d532243f711c2cef6166b4 [root@localhost dockerfile]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES e88e58afbc0f mycentos:0.1 "/bin/sh -c /bin/bash" 4 seconds ago Up 2 seconds 80/tcp mycentos [root@localhost dockerfile]# docker exec -it mycentos bash #vi并tab发现存在vim命令 [root@e88e58afbc0f local]# vi vi view vigr vim vimdiff vimtutor vipw #使用ifconfig查看网卡ip地址信息 [root@e88e58afbc0f local]# ifconfig eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 172.17.0.2 netmask 255.255.0.0 broadcast 172.17.255.255 ether 02:42:ac:11:00:02 txqueuelen 0 (Ethernet) RX packets 18 bytes 2306 (2.2 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 lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 loop txqueuelen 1000 (Local Loopback) RX packets 0 bytes 0 (0.0 B) 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 [root@e88e58afbc0f local]# ``` #### 3.1.6.查看镜像的构建历史 ``` [root@localhost dockerfile]# docker history mycentos:0.1 IMAGE CREATED CREATED BY SIZE COMMENT 13f3ef13eced 29 minutes ago CMD ["/bin/sh" "-c" "/bin/bash"] 0B buildkit.dockerfile.v0 <missing> 29 minutes ago CMD ["/bin/sh" "-c" "echo \"-------end------… 0B buildkit.dockerfile.v0 <missing> 29 minutes ago CMD ["/bin/sh" "-c" "echo $MYPATH"] 0B buildkit.dockerfile.v0 <missing> 29 minutes ago EXPOSE map[80/tcp:{}] 0B buildkit.dockerfile.v0 <missing> 29 minutes ago RUN /bin/sh -c yum install -y net-tools # bu… 189MB buildkit.dockerfile.v0 <missing> 29 minutes ago RUN /bin/sh -c yum install -y vim # buildkit 274MB buildkit.dockerfile.v0 <missing> 29 minutes ago WORKDIR /usr/local 0B buildkit.dockerfile.v0 <missing> 29 minutes ago ENV MYPATH=/usr/local 0B buildkit.dockerfile.v0 <missing> 29 minutes ago MAINTAINER zxw<zxw@xxjsjl.cn> 0B buildkit.dockerfile.v0 <missing> 23 months ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B <missing> 23 months ago /bin/sh -c #(nop) LABEL org.label-schema.sc… 0B <missing> 23 months ago /bin/sh -c #(nop) ADD file:b3ebbe8bd304723d4… 204MB ``` 我们平时拿到的镜像,可以研究它是怎么做的 ### 3.2.CMD和ENTRYPOINT区别 >#### CMD > >功能为容器启动时要运行的命令,只有最后一条会生效 > >注意:补充细节:这里边包括参数的一定要用双引号,就是",不能是单引号。千万不能写成单引号。 原因是参数传递后,docker解析的是一个JSON array > >#### ENTRYPOINT > >该命令与CMD类似,用于执行命令使用,还可以与CMD命令一起拼合使用 > >它与CMD的区别: 相同点:只能写一条,如果写多条,那么只有最后一条生效 > >不同点:CMD在创建容器时,在后面添加其他的CMD指令,CMD会被覆盖,但是ENTRYPOINT不会被覆盖,如果两个同时使用,CMD会变成ENTRYPOINT的参数 #### 3.2.1.测试cmd ``` [root@localhost dockerfile]# vim dockerfile-cmd-test FROM centos:7 CMD ["ls","-a"] [root@localhost dockerfile]# docker build -f dockerfile-cmd-test -t cmd-test:0.1 . [+] Building 0.8s (5/5) FINISHED docker:default => [internal] load build definition from dockerfile-cmd-test 0.0s => => transferring dockerfile: 136B 0.0s => [internal] load .dockerignore 0.0s => => transferring context: 2B 0.0s => [internal] load metadata for docker.io/library/centos:7 0.3s => CACHED [1/1] FROM docker.io/library/centos:7@sha256:9d4bcbbb213dfd745b58be38b13b996ebb5ac315fe75711bd618426a630e0987 0.0s => exporting to image 0.5s => => exporting layers 0.0s => => writing image sha256:06f2cc65ea4ade90802e89678c89aca197bf6ffc2c56c531355fd659627f6493 0.0s => => naming to docker.io/library/cmd-test:0.1 [root@localhost dockerfile]# docker run -it cmd-test:0.1 #命令ls -a生效 . .. .dockerenv anaconda-post.log bin dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var [root@localhost dockerfile]# docker history cmd-test:0.1 IMAGE CREATED CREATED BY SIZE COMMENT 06f2cc65ea4a 23 months ago CMD ["ls" "-a"] 0B buildkit.dockerfile.v0 <missing> 23 months ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B <missing> 23 months ago /bin/sh -c #(nop) LABEL org.label-schema.sc… 0B <missing> 23 months ago /bin/sh -c #(nop) ADD file:b3ebbe8bd304723d4… 204MB ``` 想追加一个命令 -l ls -al,cmd的清理 -l会被替换cmd["ls","-a"] 命令,-l不是命令所以报错。 ``` [root@localhost dockerfile]# docker run -it cmd-test:0.1 -l docker: Error response from daemon: failed to create task for container: 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. ``` 如果想要执行,需要写完整命令 ``` [root@localhost dockerfile]# docker run -it cmd-test:0.1 ls -al total 12 drwxr-xr-x. 1 root root 6 Aug 14 08:04 . drwxr-xr-x. 1 root root 6 Aug 14 08:04 .. -rwxr-xr-x. 1 root root 0 Aug 14 08:04 .dockerenv -rw-r--r--. 1 root root 12114 Nov 13 2020 anaconda-post.log lrwxrwxrwx. 1 root root 7 Nov 13 2020 bin -> usr/bin drwxr-xr-x. 5 root root 360 Aug 14 08:04 dev drwxr-xr-x. 1 root root 66 Aug 14 08:04 etc drwxr-xr-x. 2 root root 6 Apr 11 2018 home lrwxrwxrwx. 1 root root 7 Nov 13 2020 lib -> usr/lib lrwxrwxrwx. 1 root root 9 Nov 13 2020 lib64 -> usr/lib64 drwxr-xr-x. 2 root root 6 Apr 11 2018 media drwxr-xr-x. 2 root root 6 Apr 11 2018 mnt drwxr-xr-x. 2 root root 6 Apr 11 2018 opt dr-xr-xr-x. 267 root root 0 Aug 14 08:04 proc dr-xr-x---. 2 root root 114 Nov 13 2020 root drwxr-xr-x. 11 root root 148 Nov 13 2020 run lrwxrwxrwx. 1 root root 8 Nov 13 2020 sbin -> usr/sbin drwxr-xr-x. 2 root root 6 Apr 11 2018 srv dr-xr-xr-x. 13 root root 0 Aug 14 06:17 sys drwxrwxrwt. 7 root root 132 Nov 13 2020 tmp drwxr-xr-x. 13 root root 155 Nov 13 2020 usr drwxr-xr-x. 18 root root 238 Nov 13 2020 var ``` #### 3.2.2.测试ENTRYPOINT ``` [root@localhost dockerfile]# vim dockerfile-entrypoint-test [root@localhost dockerfile]# cat dockerfile-entrypoint-test FROM centos:7 ENTRYPOINT ["ls","-a"] [root@localhost dockerfile]# docker build -f dockerfile-entrypoint-test -t entrypoint:0.1 . [+] Building 15.4s (5/5) FINISHED docker:default => [internal] load build definition from dockerfile-entrypoint-test 0.0s => => transferring dockerfile: 151B 0.0s => [internal] load .dockerignore 0.0s => => transferring context: 2B 0.0s => [internal] load metadata for docker.io/library/centos:7 15.3s => CACHED [1/1] FROM docker.io/library/centos:7@sha256:9d4bcbbb213dfd745b58be38b13b996ebb5ac315fe75711bd618426a630e0987 0.0s => exporting to image 0.0s => => exporting layers 0.0s => => writing image sha256:5184c7d459a0111136914d90d94e7121f7fce3c2746b17b74fc2f1e808a37da8 0.0s => => naming to docker.io/library/entrypoint:0.1 0.0s [root@localhost dockerfile]# docker run entrypoint:0.1 . .. .dockerenv anaconda-post.log bin dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var [root@localhost dockerfile]# docker run entrypoint:0.1 -l #可以直接追加命令 total 12 drwxr-xr-x. 1 root root 6 Aug 14 08:09 . drwxr-xr-x. 1 root root 6 Aug 14 08:09 .. -rwxr-xr-x. 1 root root 0 Aug 14 08:09 .dockerenv -rw-r--r--. 1 root root 12114 Nov 13 2020 anaconda-post.log lrwxrwxrwx. 1 root root 7 Nov 13 2020 bin -> usr/bin drwxr-xr-x. 5 root root 340 Aug 14 08:09 dev drwxr-xr-x. 1 root root 66 Aug 14 08:09 etc drwxr-xr-x. 2 root root 6 Apr 11 2018 home lrwxrwxrwx. 1 root root 7 Nov 13 2020 lib -> usr/lib lrwxrwxrwx. 1 root root 9 Nov 13 2020 lib64 -> usr/lib64 drwxr-xr-x. 2 root root 6 Apr 11 2018 media drwxr-xr-x. 2 root root 6 Apr 11 2018 mnt drwxr-xr-x. 2 root root 6 Apr 11 2018 opt dr-xr-xr-x. 265 root root 0 Aug 14 08:09 proc dr-xr-x---. 2 root root 114 Nov 13 2020 root drwxr-xr-x. 11 root root 148 Nov 13 2020 run lrwxrwxrwx. 1 root root 8 Nov 13 2020 sbin -> usr/sbin drwxr-xr-x. 2 root root 6 Apr 11 2018 srv dr-xr-xr-x. 13 root root 0 Aug 14 06:17 sys drwxrwxrwt. 7 root root 132 Nov 13 2020 tmp drwxr-xr-x. 13 root root 155 Nov 13 2020 usr drwxr-xr-x. 18 root root 238 Nov 13 2020 var ``` ### 3.3.构建一个Tomcat镜像 ##### 3.3.1.准备镜像文件 Tomcat压缩包,jdk的压缩包 ``` [root@localhost tomcat]# ls apache-tomcat-9.0.78.tar.gz jdk-8u201-linux-x64.tar.gz readme.txt ``` ##### 3.3.2.编写dockerfile文件,官方命名:`Dockerfile` 构建的时候会自动寻找这个文件,就不要-f指定 ``` FROM centos8:0.1 MAINTAINER zxw<zxw@xxjsjl.cn> # 把宿主机当前上下文的readme.txt拷贝到容器/usr/local/路径下 COPY readme.txt /usr/local/cincontainer.txt # 把java和tomcat添加到容器中(会自动解压) ADD jdk-8u201-linux-x64.tar.gz /usr/local/ ADD apache-tomcat-9.0.78.tar.gz /usr/local/ # 安装vim编辑器 RUN yum -y install vim # 设置启动访问的初始位置,即工作目录,登录落脚点 ENV MYPATH /usr/local WORKDIR $MYPATH # 配置java和tomcat的环境变量 ENV JAVA_HOME /usr/local/jdk1.8.0_201 ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar ENV CATALINE_HOME /usr/local/apache-tomcat-9.0.78 ENV CATALINE_BASE /usr/local/apache-tomcat-9.0.78 ENV PATH $PATH:$JAVA_HOME/bin:$CATALINE_HOME/lib:$CATALINE_HOME/bin EXPOSE 8080 # 容器启动后要执行的命令 CMD /usr/local/apache-tomcat-9.0.78/bin/startup.sh && tail -F /usr/local/apache-tomcat-9.0.78/bin/logs/catalina.out ``` ##### 3.3.3.构建镜像 ``` [root@localhost tomcat]# docker build -t mytomcat . [+] Building 56.1s (11/11) FINISHED docker:default => [internal] load build definition from Dockerfile 0.0s => => transferring dockerfile: 732B 0.0s => [internal] load .dockerignore 0.0s => => transferring context: 2B 0.0s => [internal] load metadata for docker.io/library/centos:7 15.3s => CACHED [1/6] FROM docker.io/library/centos:7@sha256:9d4bcbbb213dfd745b58be38b13b996ebb5ac315fe75711bd618426a630e0987 0.0s => [internal] load build context 3.7s => => transferring context: 161.75MB 3.7s => [2/6] COPY readme.txt /usr/local/readme.txt 0.3s => [3/6] ADD jdk-8u11-linux-x64.tar.gz /usr/local/ 8.1s => [4/6] ADD apache-tomcat-9.0.22.tar.gz /usr/local/ 0.1s => [5/6] RUN yum -y install vim 27.3s => [6/6] WORKDIR /usr/local 0.0s => exporting to image 1.2s => => exporting layers 1.2s => => writing image sha256:1b36d60ec8b7fec6519ea1e630451c0c97d66b1f058a34a357ef8ffb537ae798 0.0s => => naming to docker.io/library/mytomcat ``` ##### 3.3.4.使用镜像快速创建容器 ``` [root@localhost tomcat]# docker run -d -p 8080:8080 mytomcat:0.1 [root@localhost tomcat]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 32240222b32d mytomcat:0.1 "/bin/sh -c '/usr/lo…" 3 minutes ago Up 3 minutes 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp recursing_agnesi ``` ##### 3.3.5.简单测试访问 ![](/media/202309/image-20230815162227869.png) ##### 3.3.6.创建容器并挂载本地目录,发布一个简单页面 (1)创建容器,命名tomcat01,并挂载相关目录 ``` [root@localhost ~]# docker run -d -it -p 8080:8080 --name tomcat01 -v /usr/local/tomcat/webapps:/usr/local/apache-tomcat-9.0.78/webapps -v /usr/local/tomcat/tomcatlogs:/usr/local/apache-tomcat-9.0.78/logs mytomcat:0.1 c37b47bf75e8cb0aa7abd8c089527d00acc041fcecd8b0b85d5e759fb4e098ac [root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c37b47bf75e8 mytomcat:0.1 "/bin/sh -c '/usr/lo…" 8 seconds ago Up 5 seconds 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp tomcat01 ``` (2)添加test目录,及测试文件 ``` [root@localhost webapps]# mkdir test [root@localhost test]# vim index.html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>index page</title> </head> <body> <h1>my index page</h1> </body> </html> ``` (3)重启容器测试访问 ``` [root@localhost test]# docker restart tomcat01 ``` ![](/media/202309/image-20230816150729197.png) ### 3.4.构建一个基于rpm安装的mysql镜像 > 通过rmp包二进制安装构建mysql镜像,基础镜像是有centos7 ##### 3.4.1.创建目录及所需文件 ``` [root@localhost ~]# mkdir mysql ``` ##### 3.4.2编写dockerfile文件 ````shell [root@localhost mysql]# vim Dockerfie FROM centos:centos7.9.2009 MAINTAINER zxw<zxw@xxjsjl.cn> VOLUME ["mysql_1","mysql_2"] RUN yum install -y wget && yum -y install https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm && rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022 RUN yum -y install mysql-community-server EXPOSE 3306 CMD ["/usr/sbin/init"] ```` ##### 3.4.3.构建mysql镜像 ```shell [root@localhost mysql]# docker build -t mysql57:1.0 . [+] Building 4.5s (7/7) FINISHED docker:default => [internal] load build definition from Dockerfile 0.0s => => transferring dockerfile: 440B 0.0s => [internal] load .dockerignore 0.0s => => transferring context: 2B 0.0s => [internal] load metadata for docker.io/library/centos:centos7.9.2009 0.0s => [1/3] FROM docker.io/library/centos:centos7.9.2009 0.0s => CACHED [2/3] RUN yum install -y wget && yum -y install https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm && rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022 0.0s => CACHED [3/3] RUN yum -y install mysql-community-server 0.0s => exporting to image 4.4s => => exporting layers 4.4s => => writing image sha256:726c077a2aebfb9c1816b4444e3d2b9d19365d29e69d4bcaaa29791d0abb4a0e 0.0s => => naming to docker.io/library/mysql57:1.0 0.0s ``` ##### 3.4.4.创建一个容器 ```shell [root@localhost mysql]# docker run -itd --name mysql01 --privileged=true mysql57:1.0 /usr/sbin/init 965bb59093a03f4ed6d47d0e2c9da68eaae7a9b95feca3aca367a72d01892a24 [root@localhost mysql]# docker exec -it mysql01 systemctl status mysqld ● mysqld.service - MySQL Server Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled) Active: activating (start) since Thu 2023-08-24 08:55:52 UTC; 5s ago Docs: man:mysqld(8) http://dev.mysql.com/doc/refman/en/using-systemd.html Process: 113 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS) Process: 54 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS) CGroup: /docker/965bb59093a03f4ed6d47d0e2c9da68eaae7a9b95feca3aca367a72d01892a24/system.slice/mysqld.service └─115 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid Aug 24 08:55:52 965bb59093a0 systemd[1]: Starting MySQL Server... Aug 24 08:55:56 965bb59093a0 systemd[1]: New main PID 115 does not belong to service, and PID file is not owned by root. Refusing. Aug 24 08:55:56 965bb59093a0 systemd[1]: New main PID 115 does not belong to service, and PID file is not owned by root. Refusing. #出现报错信息,原因是映射cgroup内核限制资源目录,解决方法,添加映射目录-v /sys/fs/cgroup:/sys/fs/cgroup [root@localhost mysql]# docker run -itd --name mysql02 -v /sys/fs/cgroup:/sys/fs/cgroup --privileged=true mysql57:1.0 /usr/sbin/init bdfdc08c475058623460f7cab15d389fc13d5dd908cae687922c497ce1b9029a [root@localhost mysql]# docker exec -it mysql02 systemctl status mysqld ● mysqld.service - MySQL Server Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled) Active: active (running) since Thu 2023-08-24 08:57:06 UTC; 1s ago Docs: man:mysqld(8) http://dev.mysql.com/doc/refman/en/using-systemd.html Process: 114 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS) Process: 54 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS) Main PID: 116 (mysqld) CGroup: /docker/bdfdc08c475058623460f7cab15d389fc13d5dd908cae687922c497ce1b9029a/system.slice/mysqld.service └─116 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid Aug 24 08:57:02 bdfdc08c4750 systemd[1]: Starting MySQL Server... Aug 24 08:57:06 bdfdc08c4750 systemd[1]: Started MySQL Server. ``` ### 3.5.构建一个基于rpm安装的php8.0镜像 ##### 3.5.1.创建目录及所需文件 ```shell [root@localhost ~]# mkdir php ``` ##### 3.5.2.编写Dockerfile文件 ```shell [root@localhost php]# vim Dockerfie FROM centos:centos7.9.2009 MAINTAINER zxw<zxw@xxjsjl.cn> VOLUME ["php80_1","php80_2"] RUN yum install -y wget && yum -y install https://mirrors.tuna.tsinghua.edu.cn/remi/enterprise/remi-release-7.rpm && yum -y install yum-utils RUN yum -y install php80-php-cli php80-php-common php80-php-devel php80-php-embedded php80-php-fpm php80-php-gd php80-php-mbstring php80-php-mysqlnd php80-php-pdo php80-php-opcache php80-php-xml php80-php-soap RUN ln -sf /opt/remi/php80/root/usr/bin/php* /usr/local/bin RUN ln -s /usr/lib/systemd/system/php80-php-fpm.service /etc/systemd/system/multi-user.target.wants/php80-php-fpm.service EXPOSE 9000 CMD ["/usr/sbin/init"] ``` ##### 3.5.3.构建镜像 ```shell [root@localhost php]# docker build -t php80:1.0 . [+] Building 0.3s (9/9) FINISHED docker:default => [internal] load build definition from Dockerfile 0.0s => => transferring dockerfile: 759B 0.0s => [internal] load .dockerignore 0.0s => => transferring context: 2B 0.0s => [internal] load metadata for docker.io/library/centos:centos7.9.2009 0.0s => [1/5] FROM docker.io/library/centos:centos7.9.2009 0.0s => CACHED [2/5] RUN yum install -y wget && yum -y install https://mirrors.tuna.tsinghua.edu.cn/remi/enterprise/remi-release-7.rpm && yum -y install yum-utils 0.0s => CACHED [3/5] RUN yum -y install php80-php-cli php80-php-common php80-php-devel php80-php-embedded php80-php-fpm php80-php-gd php80-php-mbstring php80-php-mysqlnd php80-php-pdo php80-php-opcache php80-p 0.0s => CACHED [4/5] RUN ln -sf /opt/remi/php80/root/usr/bin/php* /usr/local/bin 0.0s => [5/5] RUN ln -s /usr/lib/systemd/system/php80-php-fpm.service /etc/systemd/system/multi-user.target.wants/php80-php-fpm.service 0.3s => exporting to image 0.0s => => exporting layers 0.0s => => writing image sha256:df46ab6f91c2b8774e76a01686e111633fba615f10fc7c9900ab1b969e1a5d16 0.0s => => naming to docker.io/library/php80:1.0 0.0s ``` ##### 3.5.4.创建一个容器 ```shell [root@localhost php]# docker run -itd --name php8 --privileged=true php80:1.0 /usr/sbin/init 6851d8f92ef490c94eb23a44c1a69e2ae4a665ffadd638daa631745c167d659c [root@localhost php]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 6851d8f92ef4 php80:1.0 "/usr/sbin/init" 2 seconds ago Up 2 seconds 9000/tcp php8 [root@localhost php]# docker exec -it php8 systemctl status php80-php-fpm ● php80-php-fpm.service - The PHP FastCGI Process Manager Loaded: loaded (/usr/lib/systemd/system/php80-php-fpm.service; enabled; vendor preset: disabled) Active: active (running) since Thu 2023-08-24 07:45:29 UTC; 25s ago Main PID: 54 (php-fpm) Status: "Processes active: 0, idle: 5, Requests: 0, slow: 0, Traffic: 0req/sec" CGroup: /docker/6851d8f92ef490c94eb23a44c1a69e2ae4a665ffadd638daa631745c167d659c/system.slice/php80-php-fpm.service ├─54 php-fpm: master process (/etc/opt/remi/php80/php-fpm.conf) ├─60 php-fpm: pool www ├─61 php-fpm: pool www ├─62 php-fpm: pool www ├─63 php-fpm: pool www └─64 php-fpm: pool www ‣ 54 php-fpm: master process (/etc/opt/remi/php80/php-fpm.conf) Aug 24 07:45:29 6851d8f92ef4 systemd[1]: Starting The PHP FastCGI Process Manager... Aug 24 07:45:29 6851d8f92ef4 systemd[1]: Started The PHP FastCGI Process Manager. ``` ### 3.6.构建一个基于rpm安装的nginx镜像 ##### 3.6.1.创建目录及所需文件 ```shell [root@localhost ~]# mkdir nginx [root@localhost nginx]# touch 111 > index.html ``` ##### 3.6.2.编写Dockfile文件 ```shell [root@localhost nginx]# vim Dockerfie FROM centos:centos7.9.2009 MAINTAINER zxw<zxw@xxjsjl.cn> VOLUME ["nginx_1","nginx_2"] RUN yum install -y wget RUN wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo RUN yum install -y nginx RUN echo "daemon off;" >> /etc/nginx/nginx.conf EXPOSE 80 443 CMD ["/usr/sbin/nginx"] ``` ##### 3.6.3.构建镜像 ```shell [root@localhost nginx]# docker build -t mynginx . [+] Building 26.8s (11/11) FINISHED docker:default => [internal] load build definition from Dockerfile 0.0s => => transferring dockerfile: 428B 0.0s => [internal] load .dockerignore 0.0s => => transferring context: 2B 0.0s => [internal] load metadata for docker.io/library/centos:centos7.9.2009 0.0s => CACHED [1/6] FROM docker.io/library/centos:centos7.9.2009 0.0s => [internal] load build context 0.0s => => transferring context: 101B 0.0s => [2/6] RUN yum install -y wget 10.9s => [3/6] RUN wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo 0.3s => [4/6] RUN yum install -y nginx 14.1s => [5/6] RUN echo "daemon off;" >> /etc/nginx/nginx.conf 0.3s => [6/6] ADD index.html /usr/share/nginx/html/index.html 0.0s => exporting to image 1.0s => => exporting layers 1.0s => => writing image sha256:41c69e2c47dba70d1dbb89a1929f117fd3b680ee17a79cb15878e48ed51b1346 0.0s => => naming to docker.io/library/mynginx 0.0s ``` ##### 3.6.4.运行一个容器测试访问 ```shell [root@localhost nginx]# docker run -itd --name nginx -p 80:80 mynginx [root@localhost nginx]# docker exec -it nginx nginx -v nginx version: nginx/1.20.1 ``` ![](/media/202309/image-20230823171432720.png) ### 4.基于源码包构建myslq、php、nginx、tomcat等镜像 #### 4.1.基于源码包构建nginx镜像 ##### 4.1.1.编写Dockerfile文件 ```shell [root@localhost ~]# mkdir nginx [root@localhost nginx]# vim Dockerfile ##基础镜像来源 FROM centos:7 ##编者信息 MAINTAINER zxw<zxw@xxjsjl.cn> ##安装wget工具和下载阿里云yum源 RUN yum install wget -y RUN wget http://mirrors.aliyun.com/repo/Centos-7.repo -O /etc/yum.repos.d/Centos-7.repo ##安装编译nginx所需要的依赖以及mack工具 RUN yum install -y gcc gcc-c++ glibc make autoconf openssl openssl-devel && yum install -y pcre-devel libxslt-devel gd-devel GeoIP GeoIP-devel GeoIP-data RUN yum install -y make ##下载nginx源码包至指定目录并解压以及创建nginx启动用户 ENV NG_VER=nginx-1.24.0 WORKDIR /user/local/src RUN wget http://nginx.org/download/$NG_VER.tar.gz && tar xzvf $NG_VER.tar.gz RUN useradd -M -s /sbin/nologin nginx ##进入nginx解压后的目录进行编译(参数)和安装 WORKDIR /user/local/src/$NG_VER RUN ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-file-aio --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module --with-http_image_filter_module --with-http_geoip_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_stub_status_module && make && make install ##/usr/local/nginx/sbin 被添加到 PATH 中,意味着系统可以在该路径中找到并执行 nginx 命令 ENV PATH /usr/local/nginx/sbin:$PATH ##暴露端口 EXPOSE 80 443 ##启动nginx ENTRYPOINT ["nginx"] ##告诉容器在启动时以前台方式运行 Nginx,并执行 CMD ["-g","daemon off;"] ``` ##### 4.1.2.构建镜像 ```shell [root@localhost nginx]# docker build -t nginx1.24.0:0.1 . [+] Building 33.9s (14/14) FINISHED docker:default => [internal] load build definition from Dockerfile 0.0s => => transferring dockerfile: 1.31kB 0.0s => [internal] load .dockerignore 0.0s => => transferring context: 2B 0.0s => [internal] load metadata for docker.io/library/centos:7 0.0s => [ 1/10] FROM docker.io/library/centos:7 0.0s => CACHED [ 2/10] RUN yum install wget -y 0.0s => CACHED [ 3/10] RUN wget http://mirrors.aliyun.com/repo/Centos-7.repo -O /etc/yum.repos.d/Centos-7.repo 0.0s => CACHED [ 4/10] RUN yum install -y gcc gcc-c++ glibc make autoconf openssl openssl-devel && yum install -y pcre-devel libxslt-devel gd-devel GeoIP GeoIP-devel Ge 0.0s => CACHED [ 5/10] RUN yum install -y make 0.0s => CACHED [ 6/10] WORKDIR /user/local/src 0.0s => [ 7/10] RUN wget http://nginx.org/download/nginx-1.24.0.tar.gz && tar xzvf nginx-1.24.0.tar.gz 2.4s => [ 8/10] RUN useradd -M -s /sbin/nologin nginx 0.4s => [ 9/10] WORKDIR /user/local/src/nginx-1.24.0 0.0s => [10/10] RUN ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-file-aio --with-http_ssl_module --with-http_realip_module --with-http_addit 28.6s => exporting to image 2.3s => => exporting layers 2.3s => => writing image sha256:516d3a979c77ff748964df10db5f1b850cd6b9927fa4286fdce19882040690aa 0.0s => => naming to docker.io/library/nginx1.24.0:0.1 0.0s [root@localhost nginx]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx1.24.0 0.1 516d3a979c77 3 minutes ago 1.08GB ``` ##### 4.1.3.启动容器测试 ```shell [root@localhost nginx]# docker run -itd -p 80:80 --name nginx nginx1.24.0:0.1 f3be054228501f568ccef647d262b1d67dcb1fec75d62236d05614346a74ca8e [root@localhost nginx]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f3be05422850 nginx1.24.0:0.1 "nginx -g 'daemon of…" 2 seconds ago Up 1 second 0.0.0.0:80->80/tcp, :::80->80/tcp, 443/tcp nginx ``` ![](/media/202309/image-20230826111408266.png) #### 4.2.基于源码包构建php8.0镜像 > 提前准备源码包:oniguruma-6.9.4.tar.gz、php-8.0.0.tar.gz ```shell [root@localhost php]# ls Centos-8.repo Dockerfile oniguruma-6.9.4.tar.gz php-8.0.0.tar.gz ``` ##### 4.2.1.编写Dockerfile文件 ```shell [root@localhost php]#vim Dockerfile #this docker file #VERSION 1 FROM centos:latest MAINTAINER zxw@xxjsjl.cn ## 清除yum源 RUN rm -rf /etc/yum.repos.d/* COPY Centos-8.repo /etc/yum.repos.d/Centos-8.repo RUN yum install -y wget && yum install -y make RUN wget http://mirrors.aliyun.com/repo/epel-archive-8.repo -O /etc/yum.repos.d/epel-archive-8.repo RUN yum install -y epel-release ##安装编译oniguruma的依赖、配置编译安装oniguruma RUN yum install autoconf automake libtool -y ADD oniguruma-6.9.4.tar.gz /usr/local/src/ WORKDIR /usr/local/src/oniguruma-6.9.4 RUN ./autogen.sh && ./configure --prefix=/usr RUN make && make install ##安装编译安装php8.0的依赖 RUN yum -y install gcc gcc-c++ libxml2-devel \ openssl-devel curl-devel libjpeg-devel libpng-devel \ libicu-devel freetype-devel openldap-devel openldap \ openldap-devel libxml2-devel openssl openssl-devel \ sqlite-devel libcurl-devel.x86_64 libpng.x86_64 \ libpng-devel.x86_64 freetype-devel libicu-devel.x86_64 \ glibc-headers libzip-devel ##安装编译php8.0以及相关模块 ADD php-8.0.0.tar.gz /usr/local/src WORKDIR /usr/local/src/php-8.0.0 RUN ./configure \ --prefix=/usr/local/php \ --with-config-file-path=/usr/local/php/etc \ --with-mysqli=shared,mysqlnd \ --with-pdo-mysql \ --enable-opcache \ --enable-gd \ --with-iconv \ --with-zip \ --enable-inline-optimization \ --disable-debug \ --disable-rpath \ --enable-shared \ --enable-xml \ --enable-bcmath \ --enable-shmop \ --enable-sysvsem \ --enable-mbregex \ --enable-mbstring \ --enable-ftp \ --enable-pcntl \ --enable-sockets \ --with-xmlrpc \ --enable-soap \ --without-pear \ --with-gettext \ --enable-session \ --with-curl \ --with-jpeg \ --with-freetype \ --enable-fpm \ --with-fpm-user=nginx \ --with-fpm-group=nginx \ --without-gdbm \ --enable-fast-install \ --disable-fileinfo RUN make && make install ## 创建php启动用户 RUN groupadd nginx && useradd -g nginx -s /sbin/nologin -M nginx ## 配置php解析器 RUN cp php.ini-production /usr/local/php/etc/php.ini ## 配置php进程管理器文件 RUN cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf ## 配置PHP-FPM的一个配置文件,用于配置PHP-FPM进程池中的单个进程管理器 RUN cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf ## 将 PHP-FPM 的启动脚本拷贝到系统的启动目录,以便在系统启动时自动启动 PHP-FPM RUN cp sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm ## 通过设置权限为 744 RUN chmod 744 /etc/rc.d/init.d/php-fpm ## 命令 “/etc/init.d/php-fpm start” 用于启动 PHP-FPM 服务 RUN /etc/init.d/php-fpm start ## 将 PHP-FPM 添加到系统的服务管理器中。该命令会在系统的服务配置目录中创建一个启动配置文件,以便在系统启动时自动启动 PHP-FPM 服务。 RUN chkconfig --add php-fpm ## 将 PHP-FPM 服务设置为开机自启动。执行这个命令后,系统会将 PHP-FPM 服务添加到开机自启动的服务列表中,并在每次系统启动时自动启动该服务。 RUN chkconfig php-fpm on ## 将指定的路径添加到系统的环境变量中 RUN echo "PATH=/usr/local/php/bin:/usr/local/php/lib:\$PATH" >>/etc/profile ## 用于将 “export PATH” 添加到系统的环境变量配置文件 “/etc/profile” 中。使用 “export PATH” 声明,确保环境变量 PATH 在当前环境中可用。添加这个声明后,系统将以指定的路径为基准来搜索可执行文件,从而使得系统可以在终端中运行你所添加的可执行文件。 RUN echo "export PATH" >>/etc/profile RUN rm -rf /usr/local/src/* WORKDIR /usr/local/php EXPOSE 9000 ## 用于手动启动 PHP-FPM 的命令 RUN /usr/local/php/sbin/php-fpm CMD ["/usr/sbin/init"] ``` ##### 4.2.2.构建镜像 ```shell [root@localhost php]# docker build -t php80:1.0 . ``` ![](/media/202309/image-20230827171250784.png) ##### 4.2.3.启动容器测试服务运行情况 ```shell [root@localhost php]# docker run -itd --name php2 --privileged=true php80:1.0 /usr/sbin/init 789a5c964eb172e2840e3bcb11afbb9c5da10f60c34b12a4d7572ea5b8e9979e [root@localhost php]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 789a5c964eb1 php80:1.0 "/usr/sbin/init" 2 seconds ago Up 1 second 9000/tcp php2 bb658bf094a3 centos:latest "/bin/bash" 33 minutes ago Up 3 minutes centos [root@localhost php]# docker exec -it php2 ps -ef UID PID PPID C STIME TTY TIME CMD root 1 0 1 09:55 ? 00:00:00 /usr/sbin/init root 31 1 1 09:55 ? 00:00:00 /usr/lib/systemd/systemd-journald root 35 1 4 09:55 ? 00:00:00 /usr/lib/systemd/systemd-udevd dbus 59 1 0 09:55 ? 00:00:00 /usr/bin/dbus-daemon --system --address=systemd: --nofork --nopidfile --systemd-activation --syslog-only root 63 1 0 09:55 ? 00:00:00 php-fpm: master process (/usr/local/php/etc/php-fpm.conf) nginx 64 63 0 09:55 ? 00:00:00 php-fpm: pool www nginx 65 63 0 09:55 ? 00:00:00 php-fpm: pool www root 155 0 0 09:55 pts/1 00:00:00 ps -ef ``` #### 4.3.基于源码包构建mysql8.0.33镜像 > 提前准备相应文件 ##### 4.3.1.编写dockerfile文件 ```shell [root@localhost]# mkdir mysql [root@localhost mysql]#ls boost_1_77_0.tar.bz2 cmake-3.9.2.tar.gz Dockerfile mysql-boost-8.0.33.tar.gz [root@localhost mysql]#vim Dockerfile FROM centos:7 MAINTAINER zxw@xxjsjl.cn ## Centos_Os_Version=CentOS Linux release 7.9.2009 (Core) ## 配置yum及导入相关文件 RUN rm -rf /etc/yum.repos.d/* ADD cmake-3.9.2.tar.gz /usr/local/src/ ADD mysql-boost-8.0.33.tar.gz /usr/local/src COPY boost_1_77_0.tar.bz2 /usr/local/src/boost/boost_1_77_0.tar.bz2 COPY installed_packages.txt /root/installed_packages.txt COPY Centos-7.repo /etc/yum.repos.d/Centos-7.repo COPY epel-7.repo /etc/yum.repos.d/epel-7.repo ## 安装依赖 RUN yum install -y $(cat installed_packages.txt) devtoolset-11-gcc devtoolset-11-gcc-c++ devtoolset-11-binutils ## 编译安装cmake-3.9.2 WORKDIR /usr/local/src/cmake-3.9.2 RUN ./configure RUN make && make install RUN ln -s /usr/local/bin/cmake /usr/bin/ ## 编译及安装mysql-8.0.33 WORKDIR /usr/local/src/mysql-8.0.33 RUN cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql/ \ -DMYSQL_UNIX_ADDR=/tmp/mysql.sock \ -DMYSQL_DATADIR=/usr/local/mysql/data/ \ -DDEFAULT_CHARSET=utf8 \ -DDEFAULT_COLLATION=utf8_general_ci \ -DWITH_EXTRA_CHARSETS=all \ -DWITH_MYISAM_STORAGE_ENGINE=1 \ -DWITH_INNOBASE_STORAGE_ENGINE=1 \ -DWITH_MEMORY_STORAGE_ENGINE=1 \ -DWITH_READLINE=1 \ -DWITH_INNODB_MEMCACHED=1 \ -DWITH_DEBUG=OFF \00 -DWITH_ZLIB=bundled \ -DENABLED_LOCAL_INFILE=1 \ -DENABLED_PROFILING=ON \ -DMYSQL_MAINTAINER_MODE=OFF \ -DMYSQL_TCP_PORT=3306 \ -DFORCE_INSOURCE_BUILD=1 \ -WITH_BOOST=/usr/local/src/boost/ RUN make -j 4 && make install ## 配置mysql启动用户及设置权限 RUN groupadd mysql && useradd -g mysql -s /sbin/nologin -M mysql RUN mkdir /usr/local/mysql/data/ RUN chown -R mysql.mysql /usr/local/mysql RUN chown -R mysql.mysql /usr/local/mysql/data ## 配置my.cnf文件 RUN echo "[mysqld]" >/etc/my.cnf RUN echo "bind-address=0.0.0.0" >>/etc/my.cnf RUN echo "port=3306" >>/etc/my.cnf RUN echo "basedir=/usr/local/mysql" >>/etc/my.cnf RUN echo "datadir=/usr/local/mysql/data" >>/etc/my.cnf RUN echo "default-authentication-plugin=mysql_native_password" >>/etc/my.cnf RUN echo "character-set-server=utf8mb4" >>/etc/my.cnf RUN echo "collation-server=utf8mb4_0900_ai_ci" >>/etc/my.cnf ## # 初始化数据库 RUN /usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data ## 复制启动文件及设置开机启动 RUN cp support-files/mysql.server /etc/rc.d/init.d/mysqld RUN chkconfig mysqld on ## 添加环境变量 RUN echo "PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH" >>/etc/profile RUN echo "export PATH" >>/etc/profile RUN source /etc/profile ## 添加环境变量 RUN service mysqld start CMD ["/usr/sbin/init"]:: ``` ## 6、发布镜像DockerHub或阿里云 ### 6.1.把镜像发布到DockerHub #### 6.1.1.DockerHub地址,注册一个账号 ``` https://hub.docker.com/ ``` #### 6.2.2.登录自己的DockerHub ``` [root@localhost ~]# docker login -u [账号] Password: https://docs.docker.com/engine/reference/commandline/login#credentials-store Login Succeeded ``` #### 6.2.3.提交自己的镜像 ``` [root@localhost ~]# docker push [作者名字]/[镜像名称]:[版本号] ``` #push镜像有问题?增加tag标签。 ``` [root@localhost ~]# docker tag [作者名字]/[镜像名称]:[版本号] ``` ### 6.2.把镜像发布到阿里云镜像服务 #### 6.2.1.登录到阿里云 #### 6.2.2.找到容器镜像服务并设置 (1)创建命名空间 ![](/media/202309/image-20230816154853732.png) (2)创建镜像仓库 ![](/media/202309/image-20230816154912941.png) (3)相关页面信息 #基础信息 ![](/media/202309/image-20230816155146747.png) #发布操作 ![](/media/202309/image-20230816155237542.png) #### 6.2.3.将本地镜像发布至阿里云仓库 (1)登录到阿里云仓库 ``` [root@localhost ~]# docker login --username=起风了4606 registry.cn-hangzhou.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 [root@localhost ~]# ``` (2)生成镜像tag并push #生成tag ``` [root@localhost ~]# docker tag 209d8c830416 registry.cn-hangzhou.aliyuncs.com/local_zxw_0616/mytomcat:0.1 ``` #push镜像 ``` [root@localhost ~]# docker push registry.cn-hangzhou.aliyuncs.com/local_zxw_0616/mytomcat:0.1 The push refers to repository [registry.cn-hangzhou.aliyuncs.com/local_zxw_0616/mytomcat] 5f70bf18a086: Pushed 66d32dae2002: Pushed 09aa69d518ed: Pushed 3eb0769d6105: Pushed 3e459d597ee0: Pushed 93157c47d58e: Pushed 74ddd0ec08fa: Pushed 0.1: digest: sha256:3af1281eccdab7e1ff8039712cb97465e9b55cb5dd0b6de1eec943c65a9bc4f1 size: 1787 ``` ![](/media/202309/image-20230816161317343.png)
admin
Sept. 19, 2023, 5 p.m.
转发文档
Collection documents
Last
Next
手机扫码
Copy link
手机扫一扫转发分享
Copy link
IT集中营导航
IT集中营导航
Markdown文件
share
link
type
password
Update password