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

docker常用命令示例

拉取镜像

<code>docker pull hub<span class="hljs-preprocessor">.c</span><span class="hljs-number">.163</span><span class="hljs-preprocessor">.com</span>/library/mysql:<span class="hljs-number">5.7</span></code>

查看本地镜像

<code>docker images</code>

查看曾经运行过的容器

<code>docker ps <span class="hljs-operator">-a</span> </code>

查看正在运行的容器

<code>docker ps </code>

删除一个不处于运行中的容器

<code>docker rm mysql<span class="hljs-attribute">-master</span> </code>

强制删除运行中的容器

<code>docker rm <span class="hljs-attribute">-f</span> mysql<span class="hljs-attribute">-master</span> </code>

启动MySQL容器

<code>docker run <span class="hljs-subst">--</span>name mysql<span class="hljs-attribute">-master</span> <span class="hljs-attribute">-e</span> MYSQL_ROOT_PASSWORD<span class="hljs-subst">=</span><span class="hljs-string">'123456'</span> <span class="hljs-attribute">-d</span> hub<span class="hljs-built_in">.</span>c<span class="hljs-number">.163</span><span class="hljs-built_in">.</span>com/library/mysql:<span class="hljs-number">5.7</span></code>

用镜像ID来启动容器

<code>docker run <span class="hljs-subst">--</span>name mysql<span class="hljs-attribute">-master</span> <span class="hljs-attribute">-e</span> MYSQL_ROOT_PASSWORD<span class="hljs-subst">=</span><span class="hljs-string">'123456'</span> <span class="hljs-attribute">-d</span> <span class="hljs-number">573</span>ca163b053</code>

以交互模式在mysql-master中执行bash命令

<code>docker exec <span class="hljs-attribute">-it</span> mysql<span class="hljs-attribute">-master</span> bash </code>

启动MySQL容器并挂载宿主机共享数据卷到容器

<code>docker run /
 -<span class="ruby">-name mysql-master2 /
</span> -<span class="ruby">e <span class="hljs-constant">MYSQL_ROOT_PASSWORD</span>=<span class="hljs-string">'123456'</span> /
</span> -<span class="ruby">v /root/mysql-master/datadir/<span class="hljs-symbol">:/var/lib/mysql/</span>  /
</span> -<span class="ruby">v /root/mysql-master/cnfdir/<span class="hljs-symbol">:/etc/mysql/conf</span>.d/ /
</span> -<span class="ruby">d <span class="hljs-number">573</span>ca163b053</span></code>

启动另一个容器并链接到上一个容器

<code>docker run /
 -<span class="ruby">-name mysql-slave1 /
</span> -<span class="ruby">e <span class="hljs-constant">MYSQL_ROOT_PASSWORD</span>=<span class="hljs-string">'123456'</span> /
</span> -<span class="ruby">v /root/mysql-slave1/datadir/<span class="hljs-symbol">:/var/lib/mysql/</span>  /
</span> -<span class="ruby">v /root/mysql-slave1/cnfdir/<span class="hljs-symbol">:/etc/mysql/conf</span>.d/ /
</span> —link mysql-master2:master /
 -<span class="ruby">d <span class="hljs-number">573</span>ca163b053</span></code>

停止一个容器

<code>docker <span class="hljs-keyword">stop</span> mysql-master</code>

启动原来运行过的容器

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

重启现在正在运行的容器

<code>docker restart mysql<span class="hljs-attribute">-master</span></code>

提交容器为镜像:

<code>docker <span class="hljs-operator"><span class="hljs-keyword">commit</span> 容器名/ID 镜像名:版本</span></code>

导出容器

<code>docker <span class="hljs-keyword">export</span> 容器名/ID &gt; /home/xxx.tar</code>

导入容器

<code>docker <span class="hljs-keyword">import</span> 压缩文件名<span class="hljs-built_in">.</span>tar <span class="hljs-preprocessor">[</span>REPOSITORY<span class="hljs-preprocessor">[</span>:<span class="hljs-built_in">TAG</span><span class="hljs-preprocessor">]</span><span class="hljs-markup">]</span></code>

可以打标签

保存镜像为压缩文件

<code><span class="xml"><span class="hljs-tag"><span class="hljs-attribute">docker</span> <span class="hljs-attribute">save</span> 镜像<span class="hljs-attribute">ID</span> &gt;</span> 压缩文件名.tar</span></code>

导入别人分享的镜像文件

<code>docker <span class="hljs-built_in">load</span> &lt; 压缩文件名.tar</code>

导出 export 与 保存 save 的区别
a.export导出的镜像文件大小小于save保存的镜像
b.export导出(import导入)是根据容器拿到的镜像,再导入时会丢失镜像所有的历史,所以无法进行回滚操作(docker tag );而save保存(load加载)的镜像,没有丢失镜像的历史,可以回滚到之前的层(layer)。(查看方式:docker images –tree)

从容器中拷贝内容到宿主机

<code>docker cp <span class="hljs-constant">CONTAINER</span><span class="hljs-symbol">:PATH</span> <span class="hljs-constant">HOSTDIR</span></code>

未经允许不得转载:冰点网络 » docker常用命令示例

分享到:更多 ()

评论 抢沙发

评论前必须登录!