www.icesr.com
IT运维工程师的摇篮

Docker学习–环境搭建和使用

文章内容

1.Docker环境搭建
2.容器的创建
3.镜像导入/导出
4.常用远程API(命令)

使用Docker最突出的优势有两点:
1.应用打包部署和测试,解决平台和应用依赖的问题;
2.结合虚拟平台的支持,充分利用资源和虚拟化特性;

1.Docker环境搭建

注:CentOS 7 x64
uname -r –确定系统内核版本高于3.10
yum update 确保 yum 包更新到最新

1.1.通过yum

<code><span class="hljs-comment">yum</span> <span class="hljs-literal">-</span><span class="hljs-comment">y</span> <span class="hljs-comment">install</span> <span class="hljs-comment">docker</span>   <span class="hljs-literal">-</span><span class="hljs-literal">-</span><span class="hljs-comment">等待安装完成即可</span></code>

这里写图片描述

1.2.启动Docker服务

<code>service docker <span class="hljs-operator"><span class="hljs-keyword">start</span></span></code>

1.3.安装测试

<code><span class="hljs-title">docker</span> <span class="hljs-built_in">info</span></code>
<code>systemctl enable docker</code>

2.容器的创建

以安装MySQL为例(docker pull直接从镜像库中获取)

2.1.创建相关映射目录

<code><span class="hljs-title">cd</span> /usr/local/etc/

<span class="hljs-title">mkdir</span> mysql_d   <span class="hljs-comment">--根目录</span>
<span class="hljs-title">mkdir</span> ./mysql_d/<span class="hljs-typedef"><span class="hljs-keyword">data</span>      <span class="hljs-comment">--(映射)数据目录</span></span>
<span class="hljs-title">mkdir</span> ./mysql_d/logs      <span class="hljs-comment">--(映射)日志目录</span>
<span class="hljs-title">mkdir</span> ./mysql_d/conf      <span class="hljs-comment">--(映射)配置文件目录</span>
</code>

2.2.获取MySQL镜像

可以使用阿里的镜像库获取,官方提供的部分镜像可能无法访问而且下载速度较慢;

<code>docker pull registry<span class="hljs-built_in">.</span><span class="hljs-literal">cn</span><span class="hljs-attribute">-hangzhou</span><span class="hljs-built_in">.</span>aliyuncs<span class="hljs-built_in">.</span>com/origin/mysql<span class="hljs-subst">-</span><span class="hljs-number">5.7</span><span class="hljs-number">.17</span><span class="hljs-attribute">-shanghai</span><span class="hljs-attribute">-zone</span></code>

2.3.创建MySQL容器

<code><span class="hljs-preprocessor">#到mysql_d目录执行</span>
docker run 
<span class="hljs-preprocessor">#端口映射</span>
-p <span class="hljs-number">33306</span>:<span class="hljs-number">3306</span> 
<span class="hljs-preprocessor">#容器名</span>
--name mysql-docker 
<span class="hljs-preprocessor">#目录挂载--配置文件</span>
-v /usr/local/etc/mysql_d/conf:/etc/mysql/conf
<span class="hljs-preprocessor">#目录挂载--日志文件</span>
-v /usr/local/etc/mysql_d/logs:/etc/mysql/logs 
<span class="hljs-preprocessor">#目录挂载--数据文件</span>
-v /usr/local/etc/mysql_d/data:/etc/mysql/mysql_data 
<span class="hljs-preprocessor">#启动参数--root密码</span>
-e MYSQL_ROOT_PASSWORD=root
<span class="hljs-preprocessor">#镜像--镜像名称:Tag </span>
-d registry.cn-hangzhou.aliyuncs.com/origin/mysql-<span class="hljs-number">5.7</span><span class="hljs-number">.17</span>-shanghai-zone:latest</code>

目录映射对于docker容器和宿主机是共享的,而对于不同的镜像配置文件、日志存放的目录不一定相同,

2.4.测试远程连接

可通过MySQL客户端连接映射出的宿主机33306端口

这里写图片描述

3.镜像导入/导出

如果我们在已有的容器上做了修改(比如新建了数据库),在部署到新的环境时并不希望重新部署数据库,可以通过Docker创建的镜像并导出;

3.1.修改原有容器

新建文件: /usr/local/origin.txt

<code><span class="hljs-built_in">cd</span> /usr/local/
touch origin.txt
</code>

3.2.使用原有容器创建镜像

使用ps命令查看当前容器列表,获取容器的ID:

<code>docker ps
CONTAINER ID        IMAGE                                                                        COMMAND                  CREATED             STATUS              PORTS                     NAMES
<span class="hljs-number">4e43</span>f17f1e79        registry<span class="hljs-built_in">.</span><span class="hljs-literal">cn</span><span class="hljs-attribute">-hangzhou</span><span class="hljs-built_in">.</span>aliyuncs<span class="hljs-built_in">.</span>com/origin/mysql<span class="hljs-subst">-</span><span class="hljs-number">5.7</span><span class="hljs-number">.17</span><span class="hljs-attribute">-shanghai</span><span class="hljs-attribute">-zone</span>:latest   <span class="hljs-string">"docker-entrypoint.sh"</span>   <span class="hljs-number">27</span> minutes ago      Up <span class="hljs-number">27</span> minutes       <span class="hljs-number">0.0</span><span class="hljs-number">.0</span><span class="hljs-number">.0</span>:<span class="hljs-number">33306</span><span class="hljs-subst">-&gt;</span><span class="hljs-number">3306</span>/tcp   mysql<span class="hljs-attribute">-docker</span></code>

使用commit命令,提交对当前容器的修改:

<code>docker <span class="hljs-operator"><span class="hljs-keyword">commit</span> <span class="hljs-number">4e43</span>f17f1e79 mysql_docker_new</span></code>

使用images命令查看镜像列表:

<code>docker images

REPOSITORY                                                            <span class="hljs-built_in">TAG</span>                 IMAGE ID            CREATED             SIZE
mysql_docker_new                                                      latest              <span class="hljs-number">075</span>fe47e059c        <span class="hljs-number">8</span> seconds ago       <span class="hljs-number">405.6</span> MB
registry<span class="hljs-built_in">.</span><span class="hljs-literal">cn</span><span class="hljs-attribute">-hangzhou</span><span class="hljs-built_in">.</span>aliyuncs<span class="hljs-built_in">.</span>com/origin/mysql<span class="hljs-subst">-</span><span class="hljs-number">5.7</span><span class="hljs-number">.17</span><span class="hljs-attribute">-shanghai</span><span class="hljs-attribute">-zone</span>   latest              <span class="hljs-number">877345</span>fbf968        <span class="hljs-number">6</span> months ago        <span class="hljs-number">405.6</span> MB
</code>

其中mysql_docker_new为我们新创建的镜像;

3.3.使用新镜像创建容器

参照第二节的步骤,创建新的容器mysql_docker_new ,映射的端口到33305;

新的容器不做目录映射或映射到其他位置,以免和原容器出现冲突;

<code>docker run <span class="hljs-attribute">-p</span> <span class="hljs-number">33305</span>:<span class="hljs-number">3306</span> <span class="hljs-subst">--</span>name mysql_docker_new <span class="hljs-attribute">-e</span> MYSQL_ROOT_PASSWORD<span class="hljs-subst">=</span>root <span class="hljs-attribute">-d</span> mysql_docker_new:latest</code>

3.4.新容器测试

查看在原容器新建的文件是否存在:

<code>docker exec -<span class="hljs-keyword">it</span> mysql_docker_new ls /usr/<span class="hljs-built_in">local</span>/

bin  etc  games  <span class="hljs-built_in">include</span>  lib  man  origin.txt  sbin  share  src</code>

从命令可以看出,在原容器中创建的文件,包含在了新的容器中;

3.5.镜像的导入导出

如果需要将对当前容器的修改带入到新的宿主机,可以通过save/load命令来完成:

首先,提交当前容器的修改,创建新的镜像mysql_docker_saved;

<code>docker save &gt; <span class="hljs-regexp">/home/docker</span><span class="hljs-regexp">/mysql_docker_saved.tar mysql_docker_saved:latest</span></code>

导入镜像的命令为:

<code>docker load &lt; <span class="hljs-regexp">/home/docker</span><span class="hljs-regexp">/mysql_docker_saved.tar</span></code>

4.常用远程API(命令)

常用的命令包括:镜像基本操作、容器基本操作、容器管理、其他命令;

参考:
Docker 命令大全

未经允许不得转载:冰点网络 » Docker学习–环境搭建和使用

分享到:更多 ()

评论 抢沙发

评论前必须登录!