Docker_入门?只要这篇就够了!(纯干货适合0基础小白)

Docker_入门?只要这篇就够了!(纯干货适合0基础小白)

一起来开启你的Docker之路吧~

关键词: Docker; mac; Docker中使用gdb无法进入断点,无法调试;

如果您觉得这篇文写得不错;或者您也像博主一样在找资料的时候被垃圾转载、胡乱复制的博文气的肝颤;请务必用万分之一秒的时间为本文点个赞,既对作者表示认同,为后面来这儿的朋友提供一下对这篇博文质量的参考。

写在前面

  • 这篇博客适合谁?

    • 对于Docker并不了解,只是有一点模糊的感觉,觉得Docker可以当成虚拟机用之类的

    • 只是下载了Docker软件,对于怎么配置,怎么玩,第一步干什么,完全一无所知

    • 本文适用于mac,PC用户不保证文章的效果,现在离开来得及

  • 网上Docker相关的教程都泛滥了,为啥还要写,为啥我要看你的文章

    • 首先欢迎你,能搜索到我的博客就是缘分

    • 其次,确实,现在Docker相关的文章真的太多了,那我为什么还要写呢?原因有三:

      • 其一,文章是很多,各种1小时入门什么的,相信你已经在知乎看过很多了,我也看过, 因为我就是从知乎那几篇教程为起点,自己抠出来的,现在把自己的心得和经验贴出来,我觉得既是对自己负责也能服务他人。

      • 其二,我从对Docker只有及其模糊(真的是模糊,我除了知道Docker是跟虚拟系统有关其他啥都不知道),到现在起码一问一不知(对应一问三不知)的状态,大概花了8个小时。这期间全靠我从网络上搜索到的资料。现在用我的话写出来,我觉得对于跟我相同开始情况的小白用户,能省不少时间,所以我要写。

      • 其三,因为我自己是纯靠搜索到的教程和技术文章学习的Docker,我自然是懂得干涩的语言对学习的拖慢。所以我自己写的时候,自然会考虑到这个问题。本文尽量不用技术用语,尽量使用通俗易懂的文字,为进一步拉低Docker的入门门槛作出我自己的贡献。

  • 这篇文章主要是哪方面的Docker文章,纯入门?你做了哪些工作?

    • 很高兴你问了我这个问题。本文主要:

      • 首先对于Docker的概念作出我的解释,尽量通俗易懂

      • 接着针对我用Docker的目的,从最开始的软件下载,到最后push一个自己的镜像到hub,整个过程以白话的形式描述出来,降低入门门槛,节省你的学习时间

    • 我做了哪些工作?这是个好问题!

      • 首先,我一直都很好奇Docker这个东西,但从来没有机会去使用它。但是,这学期OS课的lab,纯Linux系统编程,虽然使用双系统或者虚拟机都是纯小学生的难易程度了,也不麻烦,但我想,为什么不用Docker呢?用双系统或者虚拟机也太过小儿科!所以,我就这样接触了Docker

      • 那么我使用Docker实现了什么?在lab中,我们需要用到的是纯终端下的gcc工具链还有vim等,那么这就是我需要的全部软件了!于是我在Docker中实现了「gcc+gdb +vim」环境,并push到了hub中。

    • 参考资料会在文后统一附上,谢谢!

好了,闲话说完,我们开始吧!

入门Docker,你要下载什么?注册什么?

  • Docker.app你肯定是要下载的!

Docker for mac,这个你肯定要下载:点我下载Docker for mac的社区免费版本

  • 注册Docker官方账号,你需要它!

注册一个Docker的官方账号,有利而无害,相信我!点我注册Docker Cloud官方账号

Docker安装好,账号也注册后,点击桌面顶栏的Docker图标,点击sign in,登陆你的Docker账号。

  • 注册DaoCloud账号,获取加速服务!

不可否认,有时直接从Docker官方往本地pull镜像会十分缓慢。。。这时我们可以通过国内的Docker服务提供商免费获取加速pull镜像服务,阿里网易好像都有这种服务,我选择的是DaoCloud:点我注册DaoCloud账号

注册后登陆DaoCloud,找到这个按钮:


u=3389061731,4211517201&fm=15&gp=0.jpg

接着,按照它的步骤,为自己添加Docker加速服务
![AMXN07DUOCDH1()[%2((J9.png

  • 到此准备工作结束,进入下一部分!

要想入门Docker,首先你需要理解Docker!

我也不扯淡了,直接上我对Docker的理解:(Copyright © https://blog.csdn.net/weixin_45129876. All Rights Reserved)

Docker,可以说是一个终端命令行的虚拟机,但更准确的说法,其实应该是一个虚拟环境。比如,你想要在PC上无缝使用Linux么?那么虚拟机并不是你唯一的出路,你还有Docker!我更愿意称Docker为一个容器,当然这只是Docker的一个狭义解释,Docker不止是一个容器。Docker包含3个重要概念:

  • 一个,是镜像(Image),镜像是静态的、可以被用户互相分享的文件。我们玩过双系统和虚拟机的人都知道,首先你需要一个.iso镜像,才能安装系统。Docker中的镜像也是这个东西,镜像是静态的,你不能对他操作,只能pull别人的镜像或者push自己的镜像。

  • 还有一个,是容器(Container),前面说过,镜像是静态不可操作的,只能被分享和下载,那什么是能被操作的呢?就是容器里!容器可以理解为镜像的动态状态,也就是我们虚拟机中系统装好后的状态,其实这么说是不对的,容器最符合的描述应该是Linux的iso文件的Live CD模式,比如我们玩双系统时都进入过Live CD模式,不安装系统而直接进入系统,很神奇是吧,Docker的容器就是这个概念,只不过更加轻量更加迅速便捷。但是Live CD的害处就是你关机后作出的修改安装的软件全部gg,容器也是一样,一旦被直接推出,之前安装的gcc啊vim啊啥的就会全部gg掉。如果要保存修改,就需要将当前容器封装成一个新的镜像,这样下次启动这个新的镜像后之前作出的修改还都在。

  • 最后,是仓库(Repository)。各位在前面看到我写的pull和push什么的,有没有晕?不知道各位对于git熟悉不熟悉,Docker中的仓库很像git的代码仓库,你可以pull自己之前push到自己仓库的镜像到本地,也可以pull别人push到公共仓库的镜像到自己本地。说白了就是百度云盘,你可以上传(push)自己做好环境的Docker上去,也可以下载(pull)自己云端的镜像到本地。同时,我们知道百度云最大的特点就是分享(你懂的嘿嘿嘿),类比Docker,如果你得到百度云分享链接(别人的镜像名字、标签和别人的用户名),你还可以下载(pull)别人分享的镜像到自己的本地,别人也可以下载(pull)你的镜像,因为Docker仓库都是公共的。当然,每个免费用户有一个名额把自己的一个镜像设为私有,也就是禁止被分享给别人,类比百度云上你自己保存的而没有被生成分享链接的小姐姐。

接下来来张高大上的概念图,各位看个热闹2333

docker3.png

接下来就是实战了!打开你的iTerm2!(我是iTerm2党2333,打开普通terminal也行,这个不影响)

  • 查看Docker版本信息

<pre lang="Python" spellcheck="false" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); margin-top: 15px; margin-bottom: 15px; width: inherit; white-space: normal; font-size: 0.9em; display: block; break-inside: avoid; text-align: left; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; position: relative !important;">终端输入: docker version 12</pre>

显示的我的版本信息

<pre lang="Python" spellcheck="false" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); margin-top: 15px; margin-bottom: 15px; width: inherit; white-space: normal; font-size: 0.9em; display: block; break-inside: avoid; text-align: left; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; position: relative !important;">~$ docker version Client: Version: 17.06.2-ce API version: 1.30 Go version: go1.8.3 Git commit: cec0b72 Built: Tue Sep 5 20:12:06 2017 OS/Arch: darwin/amd64 ... ... 1234567891011</pre>

不过我一般不需要那么多信息,所以一直用的是docker -v命令

  • 是时候pull你的第一个镜像下来的!

Docker安装好后是不会自带镜像的,你需要从仓库自己pull一个镜像下来,自己制作自己的镜像也是一个道理,你可以通过在已有的镜像基础上生成自己的镜像或者看一下这篇博客: 随便百度的如何创建自己Docker镜像得到的教程

Docker镜像官方好像只提供Linux,这个很容易想,windows和mac是要交钱的吧大概?

搜索ubuntu的Docker镜像docker search ubuntu,这个如果你想要什么centos,直接改就行,不区分写法,我的返回结果:

<pre lang="Python" spellcheck="false" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); margin-top: 15px; margin-bottom: 15px; width: inherit; white-space: normal; font-size: 0.9em; display: block; break-inside: avoid; text-align: left; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; position: relative !important;">~$ docker search ubuntu NAME DESCRIPTION STARS OFFICIAL AUTOMATED ubuntu Ubuntu is a Debian-based Linux operating s... 6636 [OK] dorowu/ubuntu-desktop-lxde-vnc Ubuntu with openssh-server and NoVNC 131 [OK] rastasheep/ubuntu-sshd Dockerized SSH service, built on top of of... 105 [OK] ansible/ubuntu14.04-ansible Ubuntu 14.04 LTS with ansible 86 [OK] ubuntu-upstart Upstart is an event-based replacement for ... 80 [OK] neurodebian NeuroDebian provides neuroscience research... 40 [OK] ubuntu-debootstrap debootstrap --variant=minbase --components... 31 [OK] ... ... 123456789101112</pre>

拉取官方最新版的ubuntu镜像:docker pull ubuntu:latest,其中的latest是一个标签(tag),表示是当前最新版本。你应该得到的信息,类似这样的

<pre lang="Python" spellcheck="false" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); margin-top: 15px; margin-bottom: 15px; width: inherit; white-space: normal; font-size: 0.9em; display: block; break-inside: avoid; text-align: left; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; position: relative !important;">~$ docker pull ubuntu:latest Trying to pull repository docker.io/library/ubuntu ... latest: Pulling from docker.io/library/ubuntu aed158d74952: Pull complete 773ae8273d14: Pull complete d1d487w88782: Pull complete cd3d6cd6c0cf: Pull complete 8d73bu79120c: Pull complete Digest: sha256:35bc48a1ca97c3f74rhf378hj92hd82j29i4hf4hf84nf0dhnsid232de8d8 Status: Downloaded newer image for docker.io/ubuntu:latest 12345678910</pre>

你输入的命令实际上相当于docker pull registry.hub.docker.com/ubuntu:latest命令,即从注册服务器registry.hub.docker.com中的名为ubuntu的仓库中下载标签为latest的镜像。

由于我的lab统一要求,ubuntu需要14.04版本,所以我在hub里面搜了搜,找到了一个用户分享的14.04 64位纯净镜像(base image),下面将他的镜像扒下来

<pre lang="" spellcheck="false" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); margin-top: 15px; margin-bottom: 15px; width: inherit; white-space: normal; font-size: 0.9em; display: block; break-inside: avoid; text-align: left; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; position: relative !important;">~$ docker pull chug/ubuntu14.04x64 1</pre>

这个用户还有很多其他版本的ubuntu系统,12 13 14的32位64位都有,全是纯净镜像。

  • 查看你本地的镜像仓库!

把初始镜像拉下来后,就可以启动它了,不过,可以先使用docker images命令查看你自己的本地镜像,我随便找了个例子,你的也应该是类似这样的:

<pre lang="Python" spellcheck="false" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); margin-top: 15px; margin-bottom: 15px; width: inherit; white-space: normal; font-size: 0.9em; display: block; break-inside: avoid; text-align: left; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; position: relative !important;">~$ docker images REPOSITORY TAG IMAGE ID CREATED SIZE docker.io/ubuntu 16.04 e4415b714b62 11 days ago 128.1 MB docker.io/ubuntu latest e4415b714b62 11 days ago 128.1 MB docker.io/ubuntu 12.04 aefa163f7a7e 11 days ago 103.5 MB docker.io/centos latest 0584b3d2cf6d 3 weeks ago 196.5 MB 123456</pre>

从网上一个教程中找到以下说法,比我说的好,那就看这个吧!

在列出信息中,可以看到几个字段信息:

  • 来自于哪个仓库,比如 ubuntu
  • 镜像的标记,比如 16.04
  • 它的 ID 号(唯一),比如e4415b714b62
  • 创建时间
  • 镜像大小

其中镜像的 ID 唯一标识了镜像,注意到 ubuntu:16.04 和 ubuntu:latest具有相同的镜像 ID ,说明它们实际上是同一镜像。 TAG 信息用来标记来自同一个仓库的不同镜像。例如 ubuntu 仓库中有多个镜像,通过 TAG 信息来区分发行版本,例如10.04 、 12.04 、 12.10 、 13.04 、 14.04 等。例如可以使用docker run -t -i ubuntu:16.04 /bin/bash命令指定使用镜像ubuntu:16.04来启动一个容器。如果不指定具体的标记,则默认使用latest标记信息。

  • 启动你的镜像并尽情安装软件吧!

以下内容均以我自己pull下来的chug的初始镜像为例:

现在你已经有一个初始的镜像了,注意这个里面是什么都没有的,连vim都没装,是精简到不能再精简的镜像了。

首先启动它:

<pre lang="Python" spellcheck="false" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); margin-top: 15px; margin-bottom: 15px; width: inherit; white-space: normal; font-size: 0.9em; display: block; break-inside: avoid; text-align: left; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; position: relative !important;">~$ docker run -it chug/ubuntu14.04x64 /bin/bash root@aa97ba3292ce:/# 12</pre>

-it 表示运行在交互模式,是-i -t的缩写,即-it是两个参数:-i和-t。前者表示打开并保持stdout,后者表示分配一个终端(pseudo-tty)一般这个模式就是可以启动bash,然后和容器有命令行的交互

可以看到我们终端的字头变成root@aa97ba3292ce:/#了,这就意味着我们的镜像已经创建了一个容器实例。现在我们可以对这个“ubuntu系统”进行操作了

比如安装vim:

<pre lang="Python" spellcheck="false" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); margin-top: 15px; margin-bottom: 15px; width: inherit; white-space: normal; font-size: 0.9em; display: block; break-inside: avoid; text-align: left; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; position: relative !important;">root@aa97ba3292ce:/# apt-get install vim Reading package lists... Done Building dependency tree... Done The following extra packages will be installed: file libexpat1 libffi6 libgpm2 libmagic1 libpython2.7 libpython2.7-minimal libpython2.7-stdlib libsqlite3-0 libssl1.0.0 mime-support vim-common vim-runtime Suggested packages: gpm ctags vim-doc vim-scripts The following NEW packages will be installed: file libexpat1 libffi6 libgpm2 libmagic1 libpython2.7 libpython2.7-minimal libpython2.7-stdlib libsqlite3-0 libssl1.0.0 mime-support vim vim-common vim-runtime 0 upgraded, 14 newly installed, 0 to remove and 0 not upgraded. Need to get 10.7 MB of archives. After this operation, 50.7 MB of additional disk space will be used. Do you want to continue? [Y/n] 12345678910111213</pre>

看到我没有用sudo,是因为本来就已经是超级用户(root)状态了。

同理按照我自己的需求,我安装了gcc和gdb

  • 想退出容器?很简单!

如果使用exit,命令退出,则容器的状态处于Exit,而不是后台运行。如果想让容器一直运行,而不是停止,可以使用快捷键 ctrl+p ctrl+q 退出,此时容器的状态为Up。

查看当前正在运行的容器:

<pre lang="Python" spellcheck="false" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); margin-top: 15px; margin-bottom: 15px; width: inherit; white-space: pre-wrap; font-size: 0.9em; display: block; break-inside: avoid; text-align: left; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; position: relative !important;">~$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
aa97ba3292ce chug/ubuntu14.04x64 "/bin/bash" 7 minutes ago Up 7 minutes relaxed_hoover
123</pre>

看到当前有一个ID为aa97ba3292ce的容器(Copyright © https://blog.csdn.net/weixin_45129876. All Rights Reserved)

启动、停止、重启容器aa97ba3292ce的命令:

<pre lang="Python" spellcheck="false" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); margin-top: 15px; margin-bottom: 15px; width: inherit; white-space: pre-wrap; font-size: 0.9em; display: block; break-inside: avoid; text-align: left; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; position: relative !important;">~docker start aa97ba3292ce ~ docker stop aa97ba3292ce
~$ docker restart aa97ba3292ce
123</pre>

后台启动一个容器后,如果想进入到这个容器,可以使用attach命令:

<pre lang="Python" spellcheck="false" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); margin-top: 15px; margin-bottom: 15px; width: inherit; white-space: pre-wrap; font-size: 0.9em; display: block; break-inside: avoid; text-align: left; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; position: relative !important;">~$ docker attach aa97ba3292ce
1</pre>

  • 软件装完,想保存环境?commit帮你!

将容器转化为一个镜像,即执行commit操作,完成后可使用docker images查看

<pre lang="Python" spellcheck="false" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); margin-top: 15px; margin-bottom: 15px; width: inherit; white-space: pre-wrap; font-size: 0.9em; display: block; break-inside: avoid; text-align: left; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; position: relative !important;">root@aa97ba3292ce:/#exit //先退出容器
~docker commit -m "ubuntu with vim" -a "sgy" aa97ba3292ce sgy/ubuntu:vim ~ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
sgy/ubuntu vim 52166e4475ed 5 seconds ago 358.1 MB
chug/ubuntu14.04x64 latest 0584b3d2cf6d 9 days ago 196.5 MB
123456</pre>

其中,-m指定说明信息;-a指定用户信息;aa97ba3292ce代表容器的id;sgy/ubuntu:vim指定目标镜像的用户名、仓库名和 tag 信息。我这里都是为了博客瞎编的用户名,我自己的用户名也不是sgy,你运行命令的时候使用自己注册Docker时的用户名。

此时Docker中就有了我们新建的镜像sgy/ubuntu:vim,此镜像和原有的ubuntu镜像区别在于多了个vim工具。此时我们利用新镜像创建的容器,本身就自带vim了。

启动新创建的镜像,可以看到vim已经自带了。

<pre lang="Python" spellcheck="false" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); margin-top: 15px; margin-bottom: 15px; width: inherit; white-space: pre-wrap; font-size: 0.9em; display: block; break-inside: avoid; text-align: left; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; position: relative !important;">~$ docker run -it sgy/ubuntu:vim /bin/bash
root@520afc596c51:/# vim --version
VIM - Vi IMproved 7.4 (2013 Aug 10, compiled Apr 4 2017 18:14:54)
...
...
123456</pre>

利用exit退出容器。此时Docker引擎中就有了两个容器,可使用docker ps -a查看。

  • 想要删除容器或者镜像?用这个!

如果想删除容器或者镜像,可以使用rm命令,注意:删除镜像前必须先删除以此镜像为基础的容器(哪怕是已经停止的容器),否则无法删除该镜像,会报错Failed to remove image (e4415b714b62): Error response from daemon: conflict: unable to delete e4415b714b62 (cannot be forced) - image has dependent child images类似这种。

<pre lang="Python" spellcheck="false" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); margin-top: 15px; margin-bottom: 15px; width: inherit; white-space: pre-wrap; font-size: 0.9em; display: block; break-inside: avoid; text-align: left; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; position: relative !important;">~docker rm container_id ~ docker rmi image_id
12</pre>

有的时候尽管删除了全部容器,镜像还是无法删除,这时点击mac顶栏中的docker logo,选择restart,然后再试一次rmi,应该就没问题了。

  • 附上一张高大上的Docker命令图

docker4.png
  • 一次配置,到处使用?那就push到hub上吧!

因为之前已经在Docker.app中登陆了Docker账号,所以现在直接

<pre lang="Python" spellcheck="false" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); margin-top: 15px; margin-bottom: 15px; width: inherit; white-space: pre-wrap; font-size: 0.9em; display: block; break-inside: avoid; text-align: left; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; position: relative !important;">~$ docker push sgy/ubuntu:vim
1</pre>

就可以了!

下次到了机房,在ubuntu系统中安装Docker,配置好加速器,启动Docker,docker login登陆自己的账号,然后直接~$ docker pull sgy/ubuntu:vim就可以把你push到hub的已经配置好的环境的镜像给扒下来。做完实验,再push上去就ok了!

Docker中安装gcc、gdb时遇到的问题

  • add-apt-repository: command not found

14.04系统解决办法:

<pre lang="Python" spellcheck="false" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); margin-top: 15px; margin-bottom: 15px; width: inherit; white-space: pre-wrap; font-size: 0.9em; display: block; break-inside: avoid; text-align: left; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; position: relative !important;">apt-get install software-properties-common
1</pre>

older版本的系统:

<pre lang="Python" spellcheck="false" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); margin-top: 15px; margin-bottom: 15px; width: inherit; white-space: pre-wrap; font-size: 0.9em; display: block; break-inside: avoid; text-align: left; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; position: relative !important;">apt-get install python-software-properties
1</pre>

  • Docker中使用gdb无法进入断点,无法调试

加上--privileged参数

<pre lang="Python" spellcheck="false" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); margin-top: 15px; margin-bottom: 15px; width: inherit; white-space: pre-wrap; font-size: 0.9em; display: block; break-inside: avoid; text-align: left; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; position: relative !important;">~$ docker run -it --privileged sgy/ubuntu:vim /bin/bash
1</pre>

结语

到此本文就结束了,我说得很清楚,这只是一篇入门教程,可以让你从“一问三不知”到达“一问一不知”的状态,再高层次?那就靠自己了!

hope my blog will be of assistance, peace out : )

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,736评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,167评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,442评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,902评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,302评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,573评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,847评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,562评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,260评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,531评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,021评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,367评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,016评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,068评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,827评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,610评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,514评论 2 269