Docker 容器—-deepin上实验

Docker 容器

Docker 容器的安装和部署

安装docker

启动并设置开机启动

查看版本信息

root@fff-PC:~ Client: Version: <span class="token number">18.09</span>.1 API version: <span class="token number">1.39</span> Go version: go1.11.6 Git commit: 4c52b90 Built: Sun, <span class="token number">23</span> Jun <span class="token number">2019</span> 01:25:10 +0800 OS/Arch: linux/amd64 Experimental: <span class="token boolean">false</span> Server: Engine: Version: <span class="token number">18.09</span>.1 API version: <span class="token number">1.39</span> <span class="token punctuation">(</span>minimum version <span class="token number">1.12</span><span class="token punctuation">)</span> Go version: go1.11.6 Git commit: 4c52b90 Built: Sat Jun <span class="token number">22</span> <span class="token number">17</span>:25:10 <span class="token number">2019</span> OS/Arch: linux/amd64 Experimental: <span class="token boolean">false</span> root@fff-PC:~
root@fff-PC:~ Client: Version: <span class="token number">18.09</span>.1 API version: <span class="token number">1.39</span> Go version: go1.11.6 Git commit: 4c52b90 Built: Sun, <span class="token number">23</span> Jun <span class="token number">2019</span> 01:25:10 +0800 OS/Arch: linux/amd64 Experimental: <span class="token boolean">false</span> Server: Engine: Version: <span class="token number">18.09</span>.1 API version: <span class="token number">1.39</span> <span class="token punctuation">(</span>minimum version <span class="token number">1.12</span><span class="token punctuation">)</span> Go version: go1.11.6 Git commit: 4c52b90 Built: Sat Jun <span class="token number">22</span> <span class="token number">17</span>:25:10 <span class="token number">2019</span> OS/Arch: linux/amd64 Experimental: <span class="token boolean">false</span> root@fff-PC:~ 
root@fff-PC:~ Client: Version: 18.09.1 API version: 1.39 Go version: go1.11.6 Git commit: 4c52b90 Built: Sun, 23 Jun 2019 01:25:10 +0800 OS/Arch: linux/amd64 Experimental: false Server: Engine: Version: 18.09.1 API version: 1.39 (minimum version 1.12) Go version: go1.11.6 Git commit: 4c52b90 Built: Sat Jun 22 17:25:10 2019 OS/Arch: linux/amd64 Experimental: false root@fff-PC:~

Docker 配置与应用

Docker 镜像和容器的基本操作
镜像的基本操作命令

     
      
     

镜像的导出和导入

   
    
   

容器基本命令

           
            
           

过滤mac 地址和ip 地址

docker inspect -f <span class="token string">'{<!-- -->{.NetworkSettings.MacAddress}}'</span> 597b8cd3ca55 docker inspect -f <span class="token string">'{<!-- -->{.NetworkSettings.IPAddress}}'</span> 597b8cd3ca55
 docker inspect -f <span class="token string">'{<!-- -->{.NetworkSettings.MacAddress}}'</span> 597b8cd3ca55 docker inspect -f <span class="token string">'{<!-- -->{.NetworkSettings.IPAddress}}'</span> 597b8cd3ca55 
docker inspect -f '{{.NetworkSettings.MacAddress}}' 597b8cd3ca55 docker inspect -f '{{.NetworkSettings.IPAddress}}' 597b8cd3ca55

Dockerfile 文件基本语法详解和通用格式

示例:用dockerfile 构建镜像的一般过程: mkdir deepindocker vim Dockerfile docker build -t deepin:20 deepindocker/ docker images
示例:用dockerfile 构建镜像的一般过程: mkdir deepindocker  vim Dockerfile  docker build -t deepin:20 deepindocker/  docker images  
示例:用dockerfile 构建镜像的一般过程: mkdir deepindocker vim Dockerfile docker build -t deepin:20 deepindocker/ docker images

简记:

FROM deepinproject/deepin MAINTAINER deepin RUN <span class="token function">apt</span> update RUN <span class="token function">apt</span> -y <span class="token function">install</span> openssh-server RUN <span class="token function">useradd</span> -m deepin RUN /bin/echo <span class="token string">"deepin:deepin"</span> <span class="token operator">|</span> chpasswd EXPOSE <span class="token number">22</span> CMD <span class="token punctuation">[</span><span class="token string">"/usr/sbin/sshd"</span>,<span class="token string">"-D"</span><span class="token punctuation">]</span>
FROM deepinproject/deepin  MAINTAINER deepin  RUN <span class="token function">apt</span> update  RUN <span class="token function">apt</span> -y <span class="token function">install</span> openssh-server RUN <span class="token function">useradd</span> -m deepin RUN /bin/echo <span class="token string">"deepin:deepin"</span> <span class="token operator">|</span> chpasswd EXPOSE <span class="token number">22</span>  CMD <span class="token punctuation">[</span><span class="token string">"/usr/sbin/sshd"</span>,<span class="token string">"-D"</span><span class="token punctuation">]</span>  
FROM deepinproject/deepin MAINTAINER deepin RUN apt update RUN apt -y install openssh-server RUN useradd -m deepin RUN /bin/echo "deepin:deepin" | chpasswd EXPOSE 22 CMD ["/usr/sbin/sshd","-D"]

Dockerfile 参数详解
(1)FROM

FROM 指定基础镜像,必须是第一条指令 FROM nginx RUN <span class="token builtin class-name">echo</span> <span class="token string">'<h1>Hello,Docker!</h1>'</span> <span class="token operator">></span> /usr/share/nginx/html/index.html 注: Docker Hub 上有很多高质量的服务类的官方镜像可以拿来直接使用,比如:nginx 、 redis 、mysql 、php 、mongo <span class="token punctuation">\</span> tomcat 等,可以在其中找最符合的一个进行定制, 另外也有一些方便开发、构建、运行各种语言的镜像,比如:node 、python 、golang 等 如果没有找到对应服务的镜像,官方镜像中还提供了一些更为基础的操作系统镜像,比 如:ubuntu 、debian 、fedora 、centos 、deepin 等,也可以利用这些操作系统提供的软 件库
FROM 指定基础镜像,必须是第一条指令  FROM nginx RUN <span class="token builtin class-name">echo</span> <span class="token string">'<h1>Hello,Docker!</h1>'</span> <span class="token operator">></span> /usr/share/nginx/html/index.html 注: Docker Hub 上有很多高质量的服务类的官方镜像可以拿来直接使用,比如:nginx 、 redis 、mysql 、php 、mongo <span class="token punctuation">\</span> tomcat 等,可以在其中找最符合的一个进行定制, 另外也有一些方便开发、构建、运行各种语言的镜像,比如:node 、python 、golang 等 如果没有找到对应服务的镜像,官方镜像中还提供了一些更为基础的操作系统镜像,比 如:ubuntu 、debian 、fedora 、centos 、deepin 等,也可以利用这些操作系统提供的软 件库 
FROM 指定基础镜像,必须是第一条指令 FROM nginx RUN echo '<h1>Hello,Docker!</h1>' > /usr/share/nginx/html/index.html 注: Docker Hub 上有很多高质量的服务类的官方镜像可以拿来直接使用,比如:nginx 、 redis 、mysql 、php 、mongo \ tomcat 等,可以在其中找最符合的一个进行定制, 另外也有一些方便开发、构建、运行各种语言的镜像,比如:node 、python 、golang 等 如果没有找到对应服务的镜像,官方镜像中还提供了一些更为基础的操作系统镜像,比 如:ubuntu 、debian 、fedora 、centos 、deepin 等,也可以利用这些操作系统提供的软 件库

(2)RUN

RUN :用来执行命令行命令,格式有两种: 1. shell 格式:RUN <span class="token operator"><</span>命令<span class="token operator">></span>,就像直接在命令行中输入的命令一样 2. <span class="token builtin class-name">exec</span> 格式:RUN <span class="token punctuation">[</span><span class="token string">"可执行文件"</span>,“参数1”,“参数2<span class="token punctuation">]</span>,更像是函数调用中的 格式 注意:每一个RUN 命令都会在docker 镜像中新建一层,所以应该尽量少用RUN 命令,而且要在RUN 的最后要做必要的清除工作
RUN :用来执行命令行命令,格式有两种: 1. shell 格式:RUN <span class="token operator"><</span>命令<span class="token operator">></span>,就像直接在命令行中输入的命令一样 2. <span class="token builtin class-name">exec</span> 格式:RUN <span class="token punctuation">[</span><span class="token string">"可执行文件"</span>,“参数1”,“参数2”<span class="token punctuation">]</span>,更像是函数调用中的 格式 注意:每一个RUN 命令都会在docker 镜像中新建一层,所以应该尽量少用RUN 命令,而且要在RUN 的最后要做必要的清除工作 
RUN :用来执行命令行命令,格式有两种: 1. shell 格式:RUN <命令>,就像直接在命令行中输入的命令一样 2. exec 格式:RUN ["可执行文件",“参数1”,“参数2”],更像是函数调用中的 格式 注意:每一个RUN 命令都会在docker 镜像中新建一层,所以应该尽量少用RUN 命令,而且要在RUN 的最后要做必要的清除工作

一般的方式:构建层次太多,未做清理工作

FROM deepin:stretch RUN <span class="token function">apt-get</span> update RUN <span class="token function">apt-get</span> <span class="token function">install</span> -y gcc libc6-dev <span class="token function">make</span> <span class="token function">wget</span> RUN <span class="token function">wget</span> -O redis.tar.gz <span class="token string">"http://download.redis.io/releases/redis-5.0.3.tar.gz"</span> RUN <span class="token function">mkdir</span> -p /usr/src/redis RUN <span class="token function">tar</span> -xzf redis.tar.gz -C /usr/src/redis --strip-components<span class="token operator">=</span><span class="token number">1</span> RUN <span class="token function">make</span> -C /usr/src/redis RUN <span class="token function">make</span> -C /usr/src/redis <span class="token function">install</span>
FROM deepin:stretch RUN <span class="token function">apt-get</span> update RUN <span class="token function">apt-get</span> <span class="token function">install</span> -y gcc libc6-dev <span class="token function">make</span> <span class="token function">wget</span> RUN <span class="token function">wget</span> -O redis.tar.gz <span class="token string">"http://download.redis.io/releases/redis-5.0.3.tar.gz"</span> RUN <span class="token function">mkdir</span> -p /usr/src/redis RUN <span class="token function">tar</span> -xzf redis.tar.gz -C /usr/src/redis --strip-components<span class="token operator">=</span><span class="token number">1</span> RUN <span class="token function">make</span> -C /usr/src/redis RUN <span class="token function">make</span> -C /usr/src/redis <span class="token function">install</span> 
FROM deepin:stretch RUN apt-get update RUN apt-get install -y gcc libc6-dev make wget RUN wget -O redis.tar.gz "http://download.redis.io/releases/redis-5.0.3.tar.gz" RUN mkdir -p /usr/src/redis RUN tar -xzf redis.tar.gz -C /usr/src/redis --strip-components=1 RUN make -C /usr/src/redis RUN make -C /usr/src/redis install

更好的方式:一层构建,并在最后清理压缩包等缓存文件

FROM deepin:stretch RUN <span class="token assign-left variable">buildDeps</span><span class="token operator">=</span><span class="token string">'gcc libc6-dev make wget'</span> <span class="token punctuation">\</span> <span class="token operator">&&</span> <span class="token function">apt-get</span> update <span class="token punctuation">\</span> <span class="token operator">&&</span> <span class="token function">apt-get</span> <span class="token function">install</span> -y <span class="token variable">$buildDeps</span> <span class="token punctuation">\</span> <span class="token operator">&&</span> <span class="token function">wget</span> -O redis.tar.gz <span class="token string">"http://download.redis.io/releases/redis-5.0.3.tar.gz"</span> <span class="token punctuation">\</span> <span class="token operator">&&</span> <span class="token function">mkdir</span> -p /usr/src/redis <span class="token punctuation">\</span> <span class="token operator">&&</span> <span class="token function">tar</span> -xzf redis.tar.gz -C /usr/src/redis --strip-components<span class="token operator">=</span><span class="token number">1</span> <span class="token punctuation">\</span> <span class="token operator">&&</span> <span class="token function">make</span> -C /usr/src/redis <span class="token punctuation">\</span> <span class="token operator">&&</span> <span class="token function">make</span> -C /usr/src/redis <span class="token function">install</span> <span class="token punctuation">\</span> <span class="token operator">&&</span> <span class="token function">rm</span> -rf /var/lib/apt/lists/* <span class="token punctuation">\</span> <span class="token operator">&&</span> <span class="token function">rm</span> redis.tar.gz <span class="token punctuation">\</span> <span class="token operator">&&</span> <span class="token function">rm</span> -r /usr/src/redis <span class="token punctuation">\</span> <span class="token operator">&&</span> <span class="token function">apt-get</span> purge -y --auto-remove <span class="token variable">$buildDeps</span>
FROM deepin:stretch RUN <span class="token assign-left variable">buildDeps</span><span class="token operator">=</span><span class="token string">'gcc libc6-dev make wget'</span> <span class="token punctuation">\</span> <span class="token operator">&&</span> <span class="token function">apt-get</span> update <span class="token punctuation">\</span> <span class="token operator">&&</span> <span class="token function">apt-get</span> <span class="token function">install</span> -y <span class="token variable">$buildDeps</span> <span class="token punctuation">\</span> <span class="token operator">&&</span> <span class="token function">wget</span> -O redis.tar.gz <span class="token string">"http://download.redis.io/releases/redis-5.0.3.tar.gz"</span> <span class="token punctuation">\</span> <span class="token operator">&&</span> <span class="token function">mkdir</span> -p /usr/src/redis <span class="token punctuation">\</span> <span class="token operator">&&</span> <span class="token function">tar</span> -xzf redis.tar.gz -C /usr/src/redis --strip-components<span class="token operator">=</span><span class="token number">1</span> <span class="token punctuation">\</span> <span class="token operator">&&</span> <span class="token function">make</span> -C /usr/src/redis <span class="token punctuation">\</span> <span class="token operator">&&</span> <span class="token function">make</span> -C /usr/src/redis <span class="token function">install</span> <span class="token punctuation">\</span> <span class="token operator">&&</span> <span class="token function">rm</span> -rf /var/lib/apt/lists/* <span class="token punctuation">\</span> <span class="token operator">&&</span> <span class="token function">rm</span> redis.tar.gz <span class="token punctuation">\</span> <span class="token operator">&&</span> <span class="token function">rm</span> -r /usr/src/redis <span class="token punctuation">\</span> <span class="token operator">&&</span> <span class="token function">apt-get</span> purge -y --auto-remove <span class="token variable">$buildDeps</span> 
FROM deepin:stretch RUN buildDeps='gcc libc6-dev make wget' \ && apt-get update \ && apt-get install -y $buildDeps \ && wget -O redis.tar.gz "http://download.redis.io/releases/redis-5.0.3.tar.gz" \ && mkdir -p /usr/src/redis \ && tar -xzf redis.tar.gz -C /usr/src/redis --strip-components=1 \ && make -C /usr/src/redis \ && make -C /usr/src/redis install \ && rm -rf /var/lib/apt/lists/* \ && rm redis.tar.gz \ && rm -r /usr/src/redis \ && apt-get purge -y --auto-remove $buildDeps

(3)Dockerfile 上下文

构建新的镜像 -t :指定镜像名称和tag <span class="token builtin class-name">.</span> : 上下文,表示将本路径下的所有文件打包上传到docker daemon,进行定制镜像 $ docker build -t nginx:v3 <span class="token builtin class-name">.</span>
构建新的镜像 -t :指定镜像名称和tag <span class="token builtin class-name">.</span> : 上下文,表示将本路径下的所有文件打包上传到docker daemon,进行定制镜像 $ docker build -t nginx:v3 <span class="token builtin class-name">.</span> 
构建新的镜像 -t :指定镜像名称和tag . : 上下文,表示将本路径下的所有文件打包上传到docker daemon,进行定制镜像 $ docker build -t nginx:v3 .

(4)COPY

用来从构建上下文目录中<span class="token operator"><</span>原路径<span class="token operator">></span>的文件/目录复制到新一层镜像内的<span class="token operator"><</span>目标路径<span class="token operator">></span>位置, 格式有两种: 1,shell 格式:COPY <span class="token punctuation">[</span>--chown<span class="token operator">=</span><span class="token operator"><</span>user<span class="token operator">></span>:<span class="token operator"><</span>group<span class="token operator">></span><span class="token punctuation">]</span> <span class="token operator"><</span>原路径<span class="token operator">></span><span class="token punctuation">..</span>.<span class="token operator"><</span>目标路径<span class="token operator">></span>2,exec 合适:COPY <span class="token punctuation">[</span>--chown<span class="token operator">=</span><span class="token operator"><</span>user<span class="token operator">></span>:<span class="token operator"><</span>group<span class="token operator">></span><span class="token punctuation">]</span> <span class="token punctuation">[</span><span class="token string">"原路径1"</span><span class="token punctuation">..</span>. <span class="token string">"<目标路径>"</span><span class="token punctuation">]</span> 原路径:可以是多个,甚至可以是通配符 目标路径:可以是容器内的绝对路径,也可以是相对于工作目录的相对路径<span class="token punctuation">(</span>工作目录可以 用WORKDIR 指令来指定,不需要事先创建,会自动创建<span class="token punctuation">)</span> 注: COPY 会将原文件的各种数据都保留,比如读、写、执行权限,可以通过 --chown<span class="token operator">=</span><span class="token operator"><</span>user<span class="token operator">></span>:<span class="token operator"><</span>group<span class="token operator">></span> 选项来改变文件的所属用户及所属组。
用来从构建上下文目录中<span class="token operator"><</span>原路径<span class="token operator">></span>的文件/目录复制到新一层镜像内的<span class="token operator"><</span>目标路径<span class="token operator">></span>位置, 格式有两种: 1,shell 格式:COPY <span class="token punctuation">[</span>--chown<span class="token operator">=</span><span class="token operator"><</span>user<span class="token operator">></span>:<span class="token operator"><</span>group<span class="token operator">></span><span class="token punctuation">]</span> <span class="token operator"><</span>原路径<span class="token operator">></span><span class="token punctuation">..</span>.<span class="token operator"><</span>目标路径<span class="token operator">></span> 2,exec 合适:COPY <span class="token punctuation">[</span>--chown<span class="token operator">=</span><span class="token operator"><</span>user<span class="token operator">></span>:<span class="token operator"><</span>group<span class="token operator">></span><span class="token punctuation">]</span> <span class="token punctuation">[</span><span class="token string">"原路径1"</span>,<span class="token punctuation">..</span>. <span class="token string">"<目标路径>"</span><span class="token punctuation">]</span> 原路径:可以是多个,甚至可以是通配符 目标路径:可以是容器内的绝对路径,也可以是相对于工作目录的相对路径<span class="token punctuation">(</span>工作目录可以 用WORKDIR 指令来指定,不需要事先创建,会自动创建<span class="token punctuation">)</span> 注: COPY 会将原文件的各种数据都保留,比如读、写、执行权限,可以通过 --chown<span class="token operator">=</span><span class="token operator"><</span>user<span class="token operator">></span>:<span class="token operator"><</span>group<span class="token operator">></span> 选项来改变文件的所属用户及所属组。 
用来从构建上下文目录中<原路径>的文件/目录复制到新一层镜像内的<目标路径>位置, 格式有两种: 1,shell 格式:COPY [--chown=<user>:<group>] <原路径>...<目标路径> 2,exec 合适:COPY [--chown=<user>:<group>] ["原路径1"... "<目标路径>"] 原路径:可以是多个,甚至可以是通配符 目标路径:可以是容器内的绝对路径,也可以是相对于工作目录的相对路径(工作目录可以 用WORKDIR 指令来指定,不需要事先创建,会自动创建) 注: COPY 会将原文件的各种数据都保留,比如读、写、执行权限,可以通过 --chown=<user>:<group> 选项来改变文件的所属用户及所属组。

(5)ADD

ADD <span class="token builtin class-name">:</span> 和COPY 指令的功能,性质基本一致,也可以通过--chown 改变文件所属用户和 所属组,但是在COPY 的基础上增加了一些功能: 1,原路径为URL :Docker 会试图下载这个文件放到目标路径去,默认下载后的文 件权限为600,如果想要修改权限或者下载的是压缩包,需要解压,则还需要额外的一 层RUN 进行调整,还不如直接用RUN 指令用wget 进行下载,处理权限,解压缩, 然后清理无用文件更合理,所以该命令不常用,而且不推荐使用。 2,原路径为tar 压缩包:如果压缩文件格式为gzip , <span class="token function">bzip2</span> 以及xz 的情况下,ADD 指令将自动解压这个压缩文件到<span class="token operator"><</span>目标路径<span class="token operator">></span> 去,只有此种情况适合使用ADD 指令。 注: ADD 指令可能会使镜像构建缓存失效,从而可能会令镜像的构建变的比较缓慢。
ADD <span class="token builtin class-name">:</span> 和COPY 指令的功能,性质基本一致,也可以通过--chown 改变文件所属用户和 所属组,但是在COPY 的基础上增加了一些功能: 1,原路径为URL :Docker 会试图下载这个文件放到目标路径去,默认下载后的文 件权限为600,如果想要修改权限或者下载的是压缩包,需要解压,则还需要额外的一 层RUN 进行调整,还不如直接用RUN 指令用wget 进行下载,处理权限,解压缩, 然后清理无用文件更合理,所以该命令不常用,而且不推荐使用。 2,原路径为tar 压缩包:如果压缩文件格式为gzip , <span class="token function">bzip2</span> 以及xz 的情况下,ADD 指令将自动解压这个压缩文件到<span class="token operator"><</span>目标路径<span class="token operator">></span> 去,只有此种情况适合使用ADD 指令。 注: ADD 指令可能会使镜像构建缓存失效,从而可能会令镜像的构建变的比较缓慢。 
ADD : 和COPY 指令的功能,性质基本一致,也可以通过--chown 改变文件所属用户和 所属组,但是在COPY 的基础上增加了一些功能: 1,原路径为URL :Docker 会试图下载这个文件放到目标路径去,默认下载后的文 件权限为600,如果想要修改权限或者下载的是压缩包,需要解压,则还需要额外的一 层RUN 进行调整,还不如直接用RUN 指令用wget 进行下载,处理权限,解压缩, 然后清理无用文件更合理,所以该命令不常用,而且不推荐使用。 2,原路径为tar 压缩包:如果压缩文件格式为gzip , bzip2 以及xz 的情况下,ADD 指令将自动解压这个压缩文件到<目标路径> 去,只有此种情况适合使用ADD 指令。 注: ADD 指令可能会使镜像构建缓存失效,从而可能会令镜像的构建变的比较缓慢。

(6)CMD

CMD <span class="token builtin class-name">:</span> 和RUN 指令相似,也是两种格式: 1,shell 格式:CMD <span class="token operator"><</span>命令<span class="token operator">></span>2,exec 格式:CMD <span class="token punctuation">[</span><span class="token string">"可执行文件"</span>,“参数1”,“参数2<span class="token punctuation">..</span>.<span class="token punctuation">]</span>3,参数格式列表:在指定了ENTRYPOINT 指令后,用CMD 指定具体的参数 CMD 指令用于指定默认的容器主进程的启动命令的,例如ubuntu 官方镜相默认的CMD 是bash ,我们也可以在容器运行时指定运行别的命令,如: $ docker run -it ubuntu 注: 在指令格式上,一般推荐使用exec 格式,这类格式在解析时会被解析为JSON 数组, 因此一定要用双引号“而不要使用单引号。 CMD <span class="token builtin class-name">echo</span> <span class="token environment constant">$HOME</span> CMD <span class="token punctuation">[</span><span class="token string">"sh"</span> <span class="token string">"-c"</span> <span class="token string">"echo <span class="token environment constant">$HOME</span>"</span><span class="token punctuation">]</span> 注: 容器的前台执行和后台执行问题 Docker 不是虚拟机,容器中的应用都应该以前台执行,而不能像虚拟机用systemd 去 启动后台服务,容器内没有后台服务的概念。例如: CMD <span class="token function">service</span> nginx start CMD <span class="token punctuation">[</span><span class="token string">"sh"</span> <span class="token string">"-c"</span> <span class="token string">"service nginx start"</span><span class="token punctuation">]</span> CMD <span class="token punctuation">[</span><span class="token string">"nginx"</span>, <span class="token string">"-g"</span>, <span class="token string">"daemon off;"</span><span class="token punctuation">]</span>
CMD <span class="token builtin class-name">:</span> 和RUN 指令相似,也是两种格式: 1,shell 格式:CMD <span class="token operator"><</span>命令<span class="token operator">></span> 2,exec 格式:CMD <span class="token punctuation">[</span><span class="token string">"可执行文件"</span>,“参数1”,“参数2”<span class="token punctuation">..</span>.<span class="token punctuation">]</span> 3,参数格式列表:在指定了ENTRYPOINT 指令后,用CMD 指定具体的参数 CMD 指令用于指定默认的容器主进程的启动命令的,例如ubuntu 官方镜相默认的CMD 是bash ,我们也可以在容器运行时指定运行别的命令,如:  $ docker run -it ubuntu   注: 在指令格式上,一般推荐使用exec 格式,这类格式在解析时会被解析为JSON 数组, 因此一定要用双引号“而不要使用单引号。  CMD <span class="token builtin class-name">echo</span> <span class="token environment constant">$HOME</span>  CMD <span class="token punctuation">[</span><span class="token string">"sh"</span> <span class="token string">"-c"</span> <span class="token string">"echo <span class="token environment constant">$HOME</span>"</span><span class="token punctuation">]</span> 注: 容器的前台执行和后台执行问题 Docker 不是虚拟机,容器中的应用都应该以前台执行,而不能像虚拟机用systemd 去 启动后台服务,容器内没有后台服务的概念。例如:   CMD <span class="token function">service</span> nginx start   CMD <span class="token punctuation">[</span><span class="token string">"sh"</span> <span class="token string">"-c"</span> <span class="token string">"service nginx start"</span><span class="token punctuation">]</span>   CMD <span class="token punctuation">[</span><span class="token string">"nginx"</span>, <span class="token string">"-g"</span>, <span class="token string">"daemon off;"</span><span class="token punctuation">]</span> 
CMD : 和RUN 指令相似,也是两种格式: 1,shell 格式:CMD <命令> 2,exec 格式:CMD ["可执行文件",“参数1”,“参数2”...] 3,参数格式列表:在指定了ENTRYPOINT 指令后,用CMD 指定具体的参数 CMD 指令用于指定默认的容器主进程的启动命令的,例如ubuntu 官方镜相默认的CMD 是bash ,我们也可以在容器运行时指定运行别的命令,如: $ docker run -it ubuntu 注: 在指令格式上,一般推荐使用exec 格式,这类格式在解析时会被解析为JSON 数组, 因此一定要用双引号“而不要使用单引号。 CMD echo $HOME CMD ["sh" "-c" "echo $HOME"] 注: 容器的前台执行和后台执行问题 Docker 不是虚拟机,容器中的应用都应该以前台执行,而不能像虚拟机用systemd 去 启动后台服务,容器内没有后台服务的概念。例如: CMD service nginx start CMD ["sh" "-c" "service nginx start"] CMD ["nginx", "-g", "daemon off;"]

(7)ENTRYPOINT

ENTRYPOINT:格式和RUN 指令格式一样,分为exec 格式和shell 格式,目的和
CMD 一样,都是在指定容器启动程序及参数;
当指定了ENTRYPOINT 后,CMD 的含义就发生了变化,不再是直接的运行其命令,
而是将CMD 的内容作为参数传给ENTRYPOINT 指令,换句话说实际执行时,将变为:

<span class="token operator"><</span>ENTRYPOINT<span class="token operator">></span><span class="token string">"<CMD>"</span>
<span class="token operator"><</span>ENTRYPOINT<span class="token operator">></span><span class="token string">"<CMD>"</span> 
<ENTRYPOINT>"<CMD>"

用处1 :让镜像变成向命令一样使用:
ENTRYPOINT 的使用

ENTRYPOINT <span class="token punctuation">[</span><span class="token string">"curl"</span>, <span class="token string">"-s"</span>, <span class="token string">"https://ip.cn"</span><span class="token punctuation">]</span>
ENTRYPOINT <span class="token punctuation">[</span><span class="token string">"curl"</span>, <span class="token string">"-s"</span>, <span class="token string">"https://ip.cn"</span><span class="token punctuation">]</span> 
ENTRYPOINT ["curl", "-s", "https://ip.cn"]

用处2 :应用运行前的准备工作:比如数据库配置,初始化工作,此时可以传ENTRYPOINT一个脚本,然后通过CMD 指定参数,在脚本最后执行。

示例:

FROM aduser RUN <span class="token function">groupadd</span> -S redis <span class="token operator">&&</span> <span class="token function">useradd</span> -S -G redis redis <span class="token punctuation">..</span><span class="token punctuation">..</span><span class="token punctuation">..</span>. ENTRYPOINT <span class="token punctuation">[</span><span class="token string">"docker-entrypoint.sh"</span><span class="token punctuation">]</span> <span class="token number">7</span> EXPOSE <span class="token number">6379</span> CMD <span class="token punctuation">[</span><span class="token string">"redis-server"</span><span class="token punctuation">]</span>
FROM aduser RUN <span class="token function">groupadd</span> -S redis <span class="token operator">&&</span> <span class="token function">useradd</span> -S -G redis redis <span class="token punctuation">..</span><span class="token punctuation">..</span><span class="token punctuation">..</span>. ENTRYPOINT <span class="token punctuation">[</span><span class="token string">"docker-entrypoint.sh"</span><span class="token punctuation">]</span> <span class="token number">7</span> EXPOSE <span class="token number">6379</span> CMD <span class="token punctuation">[</span><span class="token string">"redis-server"</span><span class="token punctuation">]</span> 
FROM aduser RUN groupadd -S redis && useradd -S -G redis redis ....... ENTRYPOINT ["docker-entrypoint.sh"] 7 EXPOSE 6379 CMD ["redis-server"]
<span class="token keyword">if</span> <span class="token punctuation">[</span> <span class="token string">"<span class="token variable">$1</span>"</span> <span class="token operator">=</span> <span class="token string">'redis-server'</span> -a <span class="token string">"<span class="token variable"><span class="token variable">$(</span><span class="token function">id</span> -u<span class="token variable">)</span></span>"</span> <span class="token operator">=</span> <span class="token string">'0'</span> <span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">then</span> <span class="token function">chown</span> -R redis <span class="token builtin class-name">exec</span> su-exec redis <span class="token string">"<span class="token variable">$0</span>"</span> <span class="token string">"<span class="token variable">$@</span>"</span> <span class="token keyword">fi</span> <span class="token builtin class-name">exec</span> <span class="token string">"<span class="token variable">$@</span>"</span>
  <span class="token keyword">if</span> <span class="token punctuation">[</span> <span class="token string">"<span class="token variable">$1</span>"</span> <span class="token operator">=</span> <span class="token string">'redis-server'</span> -a <span class="token string">"<span class="token variable"><span class="token variable">$(</span><span class="token function">id</span> -u<span class="token variable">)</span></span>"</span> <span class="token operator">=</span> <span class="token string">'0'</span> <span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">then</span> <span class="token function">chown</span> -R redis <span class="token builtin class-name">exec</span> su-exec redis <span class="token string">"<span class="token variable">$0</span>"</span> <span class="token string">"<span class="token variable">$@</span>"</span> <span class="token keyword">fi</span> <span class="token builtin class-name">exec</span> <span class="token string">"<span class="token variable">$@</span>"</span> 
if [ "$1" = 'redis-server' -a "$(id -u)" = '0' ]; then chown -R redis exec su-exec redis "$0" "$@" fi exec "$@"

(8)ENV
ENV : 用来设置环境变量,格式有两种:

<span class="token number">1</span>,ENV <span class="token operator"><</span>key<span class="token operator">></span> <span class="token operator"><</span>value<span class="token operator">></span> <span class="token number">2</span>,ENV <span class="token operator"><</span>key<span class="token operator"><span class="token file-descriptor important">1</span>></span><span class="token operator">=</span><span class="token operator"><</span>value<span class="token operator"><span class="token file-descriptor important">1</span>></span> <span class="token operator"><</span>key<span class="token operator"><span class="token file-descriptor important">2</span>></span><span class="token operator">=</span><span class="token operator"><</span>value<span class="token operator"><span class="token file-descriptor important">2</span>></span><span class="token punctuation">..</span>.
<span class="token number">1</span>,ENV <span class="token operator"><</span>key<span class="token operator">></span> <span class="token operator"><</span>value<span class="token operator">></span> <span class="token number">2</span>,ENV <span class="token operator"><</span>key<span class="token operator"><span class="token file-descriptor important">1</span>></span><span class="token operator">=</span><span class="token operator"><</span>value<span class="token operator"><span class="token file-descriptor important">1</span>></span> <span class="token operator"><</span>key<span class="token operator"><span class="token file-descriptor important">2</span>></span><span class="token operator">=</span><span class="token operator"><</span>value<span class="token operator"><span class="token file-descriptor important">2</span>></span><span class="token punctuation">..</span>. 
1,ENV <key> <value> 2,ENV <key1>=<value1> <key2>=<value2>...

在设置了环境变量之后,无论是后面的其它指令,如RUN ,还是运行时的应用,都可以直接使用这里定义的环境变量

#定义环境变量

ENV <span class="token assign-left variable">VERSION</span><span class="token operator">=</span><span class="token number">1.0</span> <span class="token assign-left variable">DEBUG</span><span class="token operator">=</span>ON <span class="token punctuation">\</span> <span class="token assign-left variable">NAME</span><span class="token operator">=</span><span class="token string">"Happy Feet"</span>
ENV <span class="token assign-left variable">VERSION</span><span class="token operator">=</span><span class="token number">1.0</span> <span class="token assign-left variable">DEBUG</span><span class="token operator">=</span>ON <span class="token punctuation">\</span> <span class="token assign-left variable">NAME</span><span class="token operator">=</span><span class="token string">"Happy Feet"</span> 
ENV VERSION=1.0 DEBUG=ON \ NAME="Happy Feet"

(9)ARG

ARG <span class="token builtin class-name">:</span> 构建参数,格式: ARG <span class="token operator"><</span>参数名<span class="token operator">></span><span class="token punctuation">[</span><span class="token operator">=</span><span class="token operator"><</span>默认值<span class="token operator">></span><span class="token punctuation">]</span> 构建参数和ENV 的效果一样,都是设置环境变量 所不同的是,ARG 所设置的是构建环境的环境变量,在将来容器运行时是不会存在这 些环境变量的。
ARG <span class="token builtin class-name">:</span> 构建参数,格式: ARG <span class="token operator"><</span>参数名<span class="token operator">></span><span class="token punctuation">[</span><span class="token operator">=</span><span class="token operator"><</span>默认值<span class="token operator">></span><span class="token punctuation">]</span> 构建参数和ENV 的效果一样,都是设置环境变量 所不同的是,ARG 所设置的是构建环境的环境变量,在将来容器运行时是不会存在这 些环境变量的。 
ARG : 构建参数,格式: ARG <参数名>[=<默认值>] 构建参数和ENV 的效果一样,都是设置环境变量 所不同的是,ARG 所设置的是构建环境的环境变量,在将来容器运行时是不会存在这 些环境变量的。

(10)VOLUME

VOLUME:定义匿名卷,格式为: <span class="token number">1</span>,VOLUME <span class="token punctuation">[</span><span class="token string">"<路径1>”,”<路径2>"</span><span class="token punctuation">..</span>.<span class="token punctuation">]</span> <span class="token number">2</span>,VOLUME <span class="token operator"><</span>路径<span class="token operator">></span> 之前说过,容器运行时应该尽量保持容器存储层不发生写操作,对于数据库类需要保存动态数据的应用,其数据库文件应该保存在卷中,为了防止运行时用户忘记将动态文件所保存目录挂载为卷,在Dockerfile 中,我们可以事先指定某些目录挂载为匿名卷,这样在运行时如果用户不指定挂载,其应用也可以正常运行,不会向容器存储层写入大量数据。 VOLUME /data docker run -d -v mydata:/data xxxx
VOLUME:定义匿名卷,格式为: <span class="token number">1</span>,VOLUME <span class="token punctuation">[</span><span class="token string">"<路径1>”,”<路径2>"</span><span class="token punctuation">..</span>.<span class="token punctuation">]</span> <span class="token number">2</span>,VOLUME <span class="token operator"><</span>路径<span class="token operator">></span> 之前说过,容器运行时应该尽量保持容器存储层不发生写操作,对于数据库类需要保存动态数据的应用,其数据库文件应该保存在卷中,为了防止运行时用户忘记将动态文件所保存目录挂载为卷,在Dockerfile 中,我们可以事先指定某些目录挂载为匿名卷,这样在运行时如果用户不指定挂载,其应用也可以正常运行,不会向容器存储层写入大量数据。  VOLUME /data   docker run -d -v mydata:/data xxxx 
VOLUME:定义匿名卷,格式为: 1,VOLUME ["<路径1>”,”<路径2>"...] 2,VOLUME <路径> 之前说过,容器运行时应该尽量保持容器存储层不发生写操作,对于数据库类需要保存动态数据的应用,其数据库文件应该保存在卷中,为了防止运行时用户忘记将动态文件所保存目录挂载为卷,在Dockerfile 中,我们可以事先指定某些目录挂载为匿名卷,这样在运行时如果用户不指定挂载,其应用也可以正常运行,不会向容器存储层写入大量数据。 VOLUME /data docker run -d -v mydata:/data xxxx

(11)EXPOSE

EXPOSE:声明端口,格式为: EXPOSE <span class="token operator"><</span>端口<span class="token operator"><span class="token file-descriptor important">1</span>></span> <span class="token punctuation">[</span><span class="token operator"><</span>端口<span class="token operator"><span class="token file-descriptor important">2</span>></span><span class="token punctuation">..</span>.<span class="token punctuation">]</span> 该条指令是声明运行时容器提供的服务端口,这只是一个声明,在运行时并不会因为这 个声明应用就会开启这个端口的服务。这样声明带来两个好处: <span class="token number">1</span>,帮助镜像使用者理解这个镜像服务的守护端口,以方便配置映射 <span class="token number">2</span>,在运行时使用随机端口映射,也就是docker run -P 时,会自动随机映射EXPOSE 的端口 注: 要将EXPOSE 和在运行时使用-p <span class="token operator"><</span>宿主端口<span class="token operator">></span>:<span class="token operator"><</span>容器端口<span class="token operator">></span> 区分开来。-p 是映射宿主端 口和容器端口,就是将容器的对应端口服务公开给外界访问,而EXPOSE 仅仅是声明容器 打算使用什么端口而已,并不会在宿主进行端口映射。
EXPOSE:声明端口,格式为: EXPOSE <span class="token operator"><</span>端口<span class="token operator"><span class="token file-descriptor important">1</span>></span> <span class="token punctuation">[</span><span class="token operator"><</span>端口<span class="token operator"><span class="token file-descriptor important">2</span>></span><span class="token punctuation">..</span>.<span class="token punctuation">]</span> 该条指令是声明运行时容器提供的服务端口,这只是一个声明,在运行时并不会因为这 个声明应用就会开启这个端口的服务。这样声明带来两个好处: <span class="token number">1</span>,帮助镜像使用者理解这个镜像服务的守护端口,以方便配置映射 <span class="token number">2</span>,在运行时使用随机端口映射,也就是docker run -P 时,会自动随机映射EXPOSE 的端口 注: 要将EXPOSE 和在运行时使用-p <span class="token operator"><</span>宿主端口<span class="token operator">></span>:<span class="token operator"><</span>容器端口<span class="token operator">></span> 区分开来。-p 是映射宿主端 口和容器端口,就是将容器的对应端口服务公开给外界访问,而EXPOSE 仅仅是声明容器 打算使用什么端口而已,并不会在宿主进行端口映射。 
EXPOSE:声明端口,格式为: EXPOSE <端口1> [<端口2>...] 该条指令是声明运行时容器提供的服务端口,这只是一个声明,在运行时并不会因为这 个声明应用就会开启这个端口的服务。这样声明带来两个好处: 1,帮助镜像使用者理解这个镜像服务的守护端口,以方便配置映射 2,在运行时使用随机端口映射,也就是docker run -P 时,会自动随机映射EXPOSE 的端口 注: 要将EXPOSE 和在运行时使用-p <宿主端口>:<容器端口> 区分开来。-p 是映射宿主端 口和容器端口,就是将容器的对应端口服务公开给外界访问,而EXPOSE 仅仅是声明容器 打算使用什么端口而已,并不会在宿主进行端口映射。

(12)WORKDIR

WORKDIR <span class="token builtin class-name">:</span> 指定工作目录,格式为: WORKDIR <span class="token operator"><</span>工作目录路径<span class="token operator">></span> 该条指令可以来指定工作目录(或者称为当前目录),以后各层的当前目录就被改为指 定的目录,如果该目录不存在,则会自动建立。
WORKDIR <span class="token builtin class-name">:</span> 指定工作目录,格式为: WORKDIR <span class="token operator"><</span>工作目录路径<span class="token operator">></span> 该条指令可以来指定工作目录(或者称为当前目录),以后各层的当前目录就被改为指 定的目录,如果该目录不存在,则会自动建立。 
WORKDIR : 指定工作目录,格式为: WORKDIR <工作目录路径> 该条指令可以来指定工作目录(或者称为当前目录),以后各层的当前目录就被改为指 定的目录,如果该目录不存在,则会自动建立。

(13)USER:指定当前用户,格式为:

<span class="token environment constant">USER</span> <span class="token operator"><</span>用户名<span class="token operator">></span><span class="token punctuation">[</span>:<span class="token operator"><</span>用户组<span class="token operator">></span><span class="token punctuation">]</span> 该条指令和WORKDIR 相似,都是改变环境状态并影响以后的层,WORKDIR 是改变 工作目录,<span class="token environment constant">USER</span> 是改变之后层的执行RUN ,CMD 以及ENTRYPOINT 这类命令的身份。 注:如果以root 执行的脚本,在执行期间希望改变身份,比如希望以某个已经建立好的用 户来运行某个服务进程,不要使用su 或者sudo ,这些都需要比较麻烦的配置,而且在TTY 缺失的情况下经常出错,建议使用gosu 。
<span class="token environment constant">USER</span> <span class="token operator"><</span>用户名<span class="token operator">></span><span class="token punctuation">[</span>:<span class="token operator"><</span>用户组<span class="token operator">></span><span class="token punctuation">]</span> 该条指令和WORKDIR 相似,都是改变环境状态并影响以后的层,WORKDIR 是改变 工作目录,<span class="token environment constant">USER</span> 是改变之后层的执行RUN ,CMD 以及ENTRYPOINT 这类命令的身份。 注:如果以root 执行的脚本,在执行期间希望改变身份,比如希望以某个已经建立好的用 户来运行某个服务进程,不要使用su 或者sudo ,这些都需要比较麻烦的配置,而且在TTY 缺失的情况下经常出错,建议使用gosu 。 
USER <用户名>[:<用户组>] 该条指令和WORKDIR 相似,都是改变环境状态并影响以后的层,WORKDIR 是改变 工作目录,USER 是改变之后层的执行RUN ,CMD 以及ENTRYPOINT 这类命令的身份。 注:如果以root 执行的脚本,在执行期间希望改变身份,比如希望以某个已经建立好的用 户来运行某个服务进程,不要使用su 或者sudo ,这些都需要比较麻烦的配置,而且在TTY 缺失的情况下经常出错,建议使用gosu 。

(14)HEALTHCHECK

HEALTHCHECK:健康检查,格式为:

HEALTHCHECK <span class="token punctuation">[</span>选项<span class="token punctuation">]</span> CMD <span class="token operator"><</span>命令<span class="token operator">></span> :设置检查容器健康状况的命令 HEALTHCHECK NONE :如果基础镜像有健康检查指令,使用这行可以屏蔽掉其健 康检查指令 options: --interval<span class="token operator">=</span><span class="token operator"><</span>间隔<span class="token operator">></span> :两次健康检查的间隔,默认为30s<span class="token punctuation">;</span> --timeout<span class="token operator">=</span><span class="token operator"><</span>时长<span class="token operator">></span>:健康检查命令运行超时时间,如果超过这个时间,本次健康检查 就被视为失败,默认30s<span class="token punctuation">;</span> --retries<span class="token operator">=</span><span class="token operator"><</span>次数<span class="token operator">></span> :当连续失败指定次数后,则将容器状态视为unhealthy ,默认3 次; <span class="token builtin class-name">return</span> value: 0 <span class="token builtin class-name">:</span> 成功 1:失败 2:保留(不要使用这个值)
HEALTHCHECK <span class="token punctuation">[</span>选项<span class="token punctuation">]</span> CMD <span class="token operator"><</span>命令<span class="token operator">></span> :设置检查容器健康状况的命令 HEALTHCHECK NONE :如果基础镜像有健康检查指令,使用这行可以屏蔽掉其健 康检查指令 options: --interval<span class="token operator">=</span><span class="token operator"><</span>间隔<span class="token operator">></span> :两次健康检查的间隔,默认为30s<span class="token punctuation">;</span> --timeout<span class="token operator">=</span><span class="token operator"><</span>时长<span class="token operator">></span>:健康检查命令运行超时时间,如果超过这个时间,本次健康检查 就被视为失败,默认30s<span class="token punctuation">;</span> --retries<span class="token operator">=</span><span class="token operator"><</span>次数<span class="token operator">></span> :当连续失败指定次数后,则将容器状态视为unhealthy ,默认3 次; <span class="token builtin class-name">return</span> value: 0 <span class="token builtin class-name">:</span> 成功 1:失败 2:保留(不要使用这个值) 
HEALTHCHECK [选项] CMD <命令> :设置检查容器健康状况的命令 HEALTHCHECK NONE :如果基础镜像有健康检查指令,使用这行可以屏蔽掉其健 康检查指令 options: --interval=<间隔> :两次健康检查的间隔,默认为30s; --timeout=<时长>:健康检查命令运行超时时间,如果超过这个时间,本次健康检查 就被视为失败,默认30s; --retries=<次数> :当连续失败指定次数后,则将容器状态视为unhealthy ,默认3 次; return value: 0 : 成功 1:失败 2:保留(不要使用这个值)

(15)ONBUILD

ONBUILD:后构建指令,格式为: ONBUILD <span class="token operator"><</span>其它指令<span class="token operator">></span> ONBUILD 是一个特殊的指令,它后面跟的是其它指令,比如RUN,COPY 等,而这 些指令,在当前镜像构建时并不会被执行。只有当以以前镜像为基础镜像,去构建下一级镜 像的时候才会被执行。
ONBUILD:后构建指令,格式为: ONBUILD <span class="token operator"><</span>其它指令<span class="token operator">></span> ONBUILD 是一个特殊的指令,它后面跟的是其它指令,比如RUN,COPY 等,而这 些指令,在当前镜像构建时并不会被执行。只有当以以前镜像为基础镜像,去构建下一级镜 像的时候才会被执行。 
ONBUILD:后构建指令,格式为: ONBUILD <其它指令> ONBUILD 是一个特殊的指令,它后面跟的是其它指令,比如RUN,COPY 等,而这 些指令,在当前镜像构建时并不会被执行。只有当以以前镜像为基础镜像,去构建下一级镜 像的时候才会被执行。

如何发布镜像到公有仓库

如何发布镜像到私有仓库

实例:搭建本地镜像仓库
在deepin1 上,运行仓库

docker pull docker.io/registry docker run -it --name<span class="token operator">=</span>deepinregistry docker.io/registry <span class="token function">sh</span> <span class="token function">find</span> / -name registry <span class="token builtin class-name">exit</span> docker stop deepinregistry docker <span class="token function">rm</span> deepinregistry docker run --name<span class="token operator">=</span>deepinregistry --restart<span class="token operator">=</span>always -d -p <span class="token number">5000</span>:5000 -v /deepinregistry:/var/lib/registry docker.io/registry
docker pull docker.io/registry docker run -it --name<span class="token operator">=</span>deepinregistry docker.io/registry <span class="token function">sh</span> <span class="token function">find</span> / -name registry  <span class="token builtin class-name">exit</span> docker stop deepinregistry docker <span class="token function">rm</span> deepinregistry  docker run --name<span class="token operator">=</span>deepinregistry --restart<span class="token operator">=</span>always -d -p <span class="token number">5000</span>:5000 -v /deepinregistry:/var/lib/registry docker.io/registry 
docker pull docker.io/registry docker run -it --name=deepinregistry docker.io/registry sh find / -name registry exit docker stop deepinregistry docker rm deepinregistry docker run --name=deepinregistry --restart=always -d -p 5000:5000 -v /deepinregistry:/var/lib/registry docker.io/registry

修改本机docker 的启动参数并重启docker:

<span class="token function">vim</span> /etc/docker/daemon.json <span class="token punctuation">{<!-- --></span><span class="token string">"registry-mirrors"</span><span class="token builtin class-name">:</span> <span class="token punctuation">[</span><span class="token string">"https://registry.docker-cn.com"</span><span class="token punctuation">]</span>,<span class="token string">"insecure-registries"</span>:<span class="token punctuation">[</span><span class="token string">"192.168.200.10:5000"</span><span class="token punctuation">]</span><span class="token punctuation">}</span>
<span class="token function">vim</span> /etc/docker/daemon.json <span class="token punctuation">{<!-- --></span><span class="token string">"registry-mirrors"</span><span class="token builtin class-name">:</span> <span class="token punctuation">[</span><span class="token string">"https://registry.docker-cn.com"</span><span class="token punctuation">]</span>,<span class="token string">"insecure-registries"</span>:<span class="token punctuation">[</span><span class="token string">"192.168.200.10:5000"</span><span class="token punctuation">]</span><span class="token punctuation">}</span> 
vim /etc/docker/daemon.json {"registry-mirrors": ["https://registry.docker-cn.com"],"insecure-registries":["192.168.200.10:5000"]}

#必须把192.168.200.10:5000 添加为信任的安全registry,否则报错,多个registry 可在192.168.200.10:5000 后面继续写,用逗号分隔

systemctl restart docker
systemctl restart docker 
systemctl restart docker

尝试push 镜相到私有仓库:

docker tag docker.io/httpd:2.4 <span class="token number">192.168</span>.200.10:5000/deepinimages/deepinhttpd:2.4 docker push <span class="token number">192.168</span>.200.10:5000/deepinimages/deepinhttpd:2.4 <span class="token function">curl</span> <span class="token number">192.168</span>.200.201:5000/v2/_catalog
docker tag docker.io/httpd:2.4 <span class="token number">192.168</span>.200.10:5000/deepinimages/deepinhttpd:2.4 docker push <span class="token number">192.168</span>.200.10:5000/deepinimages/deepinhttpd:2.4  <span class="token function">curl</span> <span class="token number">192.168</span>.200.201:5000/v2/_catalog  
docker tag docker.io/httpd:2.4 192.168.200.10:5000/deepinimages/deepinhttpd:2.4 docker push 192.168.200.10:5000/deepinimages/deepinhttpd:2.4 curl 192.168.200.201:5000/v2/_catalog

在deepin2 上安装docker 环境

<span class="token function">curl</span> <span class="token number">192.168</span>.200.201:5000/v2/_catalog firefox http://192.168.200.201:5000/v2/_catalog docker pull <span class="token number">192.168</span>.200.201:5000/deepinimages/deepinhttpd:2.4
<span class="token function">curl</span> <span class="token number">192.168</span>.200.201:5000/v2/_catalog firefox http://192.168.200.201:5000/v2/_catalog docker pull <span class="token number">192.168</span>.200.201:5000/deepinimages/deepinhttpd:2.4  
curl 192.168.200.201:5000/v2/_catalog firefox http://192.168.200.201:5000/v2/_catalog docker pull 192.168.200.201:5000/deepinimages/deepinhttpd:2.4

#如果报错Get https://192.168.200.201:5000/v2/_catalog: http: server gave HTTP response to HTTPS client,需要在deepin2 上的/etc/docker/daemon.json 文件中添加本地仓库信任

登陆容器内执行操作

新建一个nginx 容器 docker pull nginx:latest docker run -it nginx:latest <span class="token function">bash</span> docker <span class="token function">ps</span> -a docker start 9725ff87872e 进入nginx 容器 docker <span class="token builtin class-name">exec</span> -it 9725ff87872e <span class="token function">bash</span> 修改nginx 主页 开启新的终端执行以下命令: docker inspect 9725ff87872e <span class="token function">apt</span> <span class="token function">install</span> -y <span class="token function">curl</span> <span class="token function">curl</span> <span class="token number">172.17</span>.0.2 //可以访问到容器提供的web 页面
新建一个nginx 容器 docker pull nginx:latest docker run -it nginx:latest <span class="token function">bash</span> docker <span class="token function">ps</span> -a  docker start 9725ff87872e  进入nginx 容器 docker <span class="token builtin class-name">exec</span> -it 9725ff87872e <span class="token function">bash</span> 修改nginx 主页   开启新的终端执行以下命令: docker inspect 9725ff87872e <span class="token function">apt</span> <span class="token function">install</span> -y <span class="token function">curl</span> <span class="token function">curl</span> <span class="token number">172.17</span>.0.2 //可以访问到容器提供的web 页面 
新建一个nginx 容器 docker pull nginx:latest docker run -it nginx:latest bash docker ps -a docker start 9725ff87872e 进入nginx 容器 docker exec -it 9725ff87872e bash 修改nginx 主页 开启新的终端执行以下命令: docker inspect 9725ff87872e apt install -y curl curl 172.17.0.2 //可以访问到容器提供的web 页面

Apache2 容器的搭建方式

实例:httpd 容器

docker pull docker.io/httpd:2.4 docker run -d -P --name<span class="token operator">=</span>deepinhttpd docker.io/httpd:2.4 <span class="token function">ps</span> aux <span class="token operator">|</span> <span class="token function">grep</span> httpd docker <span class="token function">top</span> deepinhttpd docker inspect deepinhttpd <span class="token operator">|</span> <span class="token function">grep</span> IP
docker pull docker.io/httpd:2.4  docker run -d -P --name<span class="token operator">=</span>deepinhttpd docker.io/httpd:2.4  <span class="token function">ps</span> aux <span class="token operator">|</span> <span class="token function">grep</span> httpd  docker <span class="token function">top</span> deepinhttpd  docker inspect deepinhttpd <span class="token operator">|</span> <span class="token function">grep</span> IP  
docker pull docker.io/httpd:2.4 docker run -d -P --name=deepinhttpd docker.io/httpd:2.4 ps aux | grep httpd docker top deepinhttpd docker inspect deepinhttpd | grep IP

浏览器打开:http://172.17.0.2/
其它管理操作:

docker port deepinhttpd docker stop deepinhttpd docker <span class="token function">rm</span> deepinhttpd docker run -d -p <span class="token number">18888</span>:80 --name<span class="token operator">=</span><span class="token string">"deepinhttpd"</span> --restart<span class="token operator">=</span>always docker.io/httpd:2.4 参数说明: -P 自动分配端口 -p 手动分配端口,默认是协议为tcp 可不写,udp 协议可用-p <span class="token number">18888</span>:80/udp 格式 -d 后台运行 --restart<span class="token operator">=</span>always 重启后自动启动
docker port deepinhttpd  docker stop deepinhttpd  docker <span class="token function">rm</span> deepinhttpd  docker run -d -p <span class="token number">18888</span>:80 --name<span class="token operator">=</span><span class="token string">"deepinhttpd"</span> --restart<span class="token operator">=</span>always docker.io/httpd:2.4 参数说明: -P 自动分配端口 -p 手动分配端口,默认是协议为tcp 可不写,udp 协议可用-p <span class="token number">18888</span>:80/udp 格式 -d 后台运行 --restart<span class="token operator">=</span>always 重启后自动启动 
docker port deepinhttpd docker stop deepinhttpd docker rm deepinhttpd docker run -d -p 18888:80 --name="deepinhttpd" --restart=always docker.io/httpd:2.4 参数说明: -P 自动分配端口 -p 手动分配端口,默认是协议为tcp 可不写,udp 协议可用-p 18888:80/udp 格式 -d 后台运行 --restart=always 重启后自动启动

如何映射文件和文件夹
映射文件和文件夹

docker run -d -p <span class="token number">18888</span>:80 -v /deepinhttpd/html:/usr/local/apache2/htdocs/ --name<span class="token operator">=</span>deepinhttpd docker.io/httpd:2.4 <span class="token builtin class-name">echo</span> deepin <span class="token operator">></span> /deepinhttpd/html/index.html/
docker run -d -p <span class="token number">18888</span>:80 -v /deepinhttpd/html:/usr/local/apache2/htdocs/ --name<span class="token operator">=</span>deepinhttpd docker.io/httpd:2.4  <span class="token builtin class-name">echo</span> deepin <span class="token operator">></span> /deepinhttpd/html/index.html/ 
docker run -d -p 18888:80 -v /deepinhttpd/html:/usr/local/apache2/htdocs/ --name=deepinhttpd docker.io/httpd:2.4 echo deepin > /deepinhttpd/html/index.html/

通过交互终端进入运行中的容器,后面可加脚本

docker <span class="token builtin class-name">exec</span> -it deepinhttpd <span class="token function">bash</span> <span class="token function">cat</span> /usr/local/apache2/htdocs/index.html <span class="token builtin class-name">exit</span>
docker <span class="token builtin class-name">exec</span> -it deepinhttpd <span class="token function">bash</span> <span class="token function">cat</span> /usr/local/apache2/htdocs/index.html <span class="token builtin class-name">exit</span> 
docker exec -it deepinhttpd bash cat /usr/local/apache2/htdocs/index.html exit

拷贝容器内文件到真机

docker <span class="token function">cp</span> deepinhttpd:/usr/local/apache2/conf/httpd.conf /tmp/ <span class="token function">cat</span> /tmp/httpd.conf
docker <span class="token function">cp</span> deepinhttpd:/usr/local/apache2/conf/httpd.conf /tmp/ <span class="token function">cat</span> /tmp/httpd.conf 
docker cp deepinhttpd:/usr/local/apache2/conf/httpd.conf /tmp/ cat /tmp/httpd.conf

关闭容器、删除容器、镜像

docker stop deepinhttpd docker <span class="token function">rm</span> deepinhttpd docker rmi httpd:2.4 <span class="token function">rm</span> -rf /deepinhttpd/
docker stop deepinhttpd docker <span class="token function">rm</span> deepinhttpd docker rmi httpd:2.4 <span class="token function">rm</span> -rf /deepinhttpd/ 
docker stop deepinhttpd docker rm deepinhttpd docker rmi httpd:2.4 rm -rf /deepinhttpd/

Mysql 数据库容器的搭建方式

实例:mysql 容器

docker pull mysql:5.6 docker run -d -p <span class="token number">33060</span>:3306 -e <span class="token assign-left variable">MYSQL_ROOT_PASSWORD</span><span class="token operator">=</span>deepin -e <span class="token assign-left variable">MYSQL_USER</span><span class="token operator">=</span>deepin -e <span class="token assign-left variable">MYSQL_PASSWORD</span><span class="token operator">=</span>deepin -e <span class="token assign-left variable">MYSQL_DATABASE</span><span class="token operator">=</span>deepin -v /deepinmysql:/var/lib/mysql --name<span class="token operator">=</span>deepinmysql docker.io/mysql:5.6
docker pull mysql:5.6 docker run -d -p <span class="token number">33060</span>:3306 -e <span class="token assign-left variable">MYSQL_ROOT_PASSWORD</span><span class="token operator">=</span>deepin -e <span class="token assign-left variable">MYSQL_USER</span><span class="token operator">=</span>deepin -e <span class="token assign-left variable">MYSQL_PASSWORD</span><span class="token operator">=</span>deepin -e <span class="token assign-left variable">MYSQL_DATABASE</span><span class="token operator">=</span>deepin -v /deepinmysql:/var/lib/mysql --name<span class="token operator">=</span>deepinmysql docker.io/mysql:5.6  
docker pull mysql:5.6 docker run -d -p 33060:3306 -e MYSQL_ROOT_PASSWORD=deepin -e MYSQL_USER=deepin -e MYSQL_PASSWORD=deepin -e MYSQL_DATABASE=deepin -v /deepinmysql:/var/lib/mysql --name=deepinmysql docker.io/mysql:5.6

配置mysql 源/etc/apt/source.list

deb <span class="token punctuation">[</span>arch<span class="token operator">=</span>amd64<span class="token punctuation">]</span> https://download.docker.com/linux/debian stretch stable deb http://mirrors.163.com/deepin/ unstable main contrib non-free
deb <span class="token punctuation">[</span>arch<span class="token operator">=</span>amd64<span class="token punctuation">]</span> https://download.docker.com/linux/debian stretch stable deb http://mirrors.163.com/deepin/ unstable main contrib non-free 
deb [arch=amd64] https://download.docker.com/linux/debian stretch stable deb http://mirrors.163.com/deepin/ unstable main contrib non-free

安装mysql 客户端

<span class="token function">apt-get</span> <span class="token function">install</span> -y mysql-server docker inspect deepinmysql <span class="token operator">|</span> <span class="token function">grep</span> IP mysql -h172.17.0.2 -P33060 -uroot -pdeepin
<span class="token function">apt-get</span> <span class="token function">install</span> -y mysql-server docker inspect deepinmysql <span class="token operator">|</span> <span class="token function">grep</span> IP mysql -h172.17.0.2 -P33060 -uroot -pdeepin 
apt-get install -y mysql-server docker inspect deepinmysql | grep IP mysql -h172.17.0.2 -P33060 -uroot -pdeepin

MySQL> show databases;

14.3 项目实战

1.部署web 服务器并对外提供服务
要求:
在server1 上布署apache2 容器,端口设置为8080
将主页设置为deepin will be the best OS.
2.本地私有镜像仓库的部署
要求
在server1 上布置私有镜像仓库
配置docker, 上传busybox 镜相
3.编写Dockerfile 完成LAMP 架构的部署
要求:
在server2 上布署mysql 容器
在server2 上布置wordpress 容器
使用浏览器配置wordpress 使其对外提供web 服务

原文链接:https://blog.csdn.net/qq_27546717/article/details/120584012?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522168994567316782425126562%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fblog.%2522%257D&request_id=168994567316782425126562&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~blog~first_rank_ecpm_v1~times_rank-21-120584012-null-null.268%5Ev1%5Ekoosearch&utm_term=docker%E3%80%81wordpress%E3%80%81wordpress%E5%BB%BA%E7%AB%99%E3%80%81wordpress%E4%B8%BB%E9%A2%98%E3%80%81%E5%AE%B9%E5%99%A8%E9%95%9C%E5%83%8F%E3%80%81

© 版权声明
THE END
喜欢就支持一下吧
点赞6 分享