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

1.docker安装

Ubuntu16.04 安装docker

1.docker安装

1.1 Ubuntu16.04 (使用apt-get进行安装)安装docker

<code><span class="hljs-preprocessor"># step 1: 安装必要的一些系统工具</span>
$ sudo apt-<span class="hljs-keyword">get</span> update
$ sudo apt-<span class="hljs-keyword">get</span> -y install apt-transport-https ca-certificates curl software-properties-common
<span class="hljs-preprocessor"># step 2: 安装GPG证书</span>
$ curl -fsSL http:<span class="hljs-comment">//mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -</span>
<span class="hljs-preprocessor"># Step 3: 写入软件源信息</span>
$ sudo add-apt-repository <span class="hljs-string">"deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"</span>
<span class="hljs-preprocessor"># Step 4: 更新并安装 Docker-CE</span>
$ sudo apt-<span class="hljs-keyword">get</span> -y update
$ sudo apt-<span class="hljs-keyword">get</span> -y install docker-ce

<span class="hljs-preprocessor"># 安装指定版本的Docker-CE:</span>
<span class="hljs-preprocessor"># Step 1: 查找Docker-CE的版本:</span>
<span class="hljs-preprocessor"># apt-cache madison docker-ce</span>
<span class="hljs-preprocessor">#   docker-ce | 17.03.1~ce-0~ubuntu-xenial | http://mirrors.aliyun.com/docker-ce/linux/ubuntu xenial/stable amd64 Packages</span>
<span class="hljs-preprocessor">#   docker-ce | 17.03.0~ce-0~ubuntu-xenial | http://mirrors.aliyun.com/docker-ce/linux/ubuntu xenial/stable amd64 Packages</span>
<span class="hljs-preprocessor"># Step 2: 安装指定版本的Docker-CE: (VERSION 例如上面的 17.03.1~ce-0~ubuntu-xenial)</span>
<span class="hljs-preprocessor"># sudo apt-get -y install docker-ce=[VERSION]</span></code>

1.2 CentOS 7 (使用yum进行安装)

<code><span class="hljs-preprocessor"># step 1: 安装必要的一些系统工具</span>
$ sudo yum install -y yum-utils device-mapper-persistent-data lvm2
<span class="hljs-preprocessor"># Step 2: 添加软件源信息</span>
$ sudo yum-config-manager --add-repo http:<span class="hljs-comment">//mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo</span>
<span class="hljs-preprocessor"># Step 3: 更新并安装 Docker-CE</span>
$ sudo yum makecache fast
s$ udo yum -y install docker-ce
<span class="hljs-preprocessor"># Step 4: 开启Docker服务</span>
$ sudo service docker start

<span class="hljs-preprocessor"># 注意:</span>
<span class="hljs-preprocessor"># 官方软件源默认启用了最新的软件,您可以通过编辑软件源的方式获取各个版本的软件包。例如官方并没有将测试版本的软件源置为可用,你可以通过以下方式开启。同理可以开启各种测试版本等。</span>
<span class="hljs-preprocessor"># vim /etc/yum.repos.d/docker-ce.repo</span>
<span class="hljs-preprocessor">#   将 [docker-ce-test] 下方的 enabled=0 修改为 enabled=1</span>
<span class="hljs-preprocessor">#</span>
<span class="hljs-preprocessor"># 安装指定版本的Docker-CE:</span>
<span class="hljs-preprocessor"># Step 1: 查找Docker-CE的版本:</span>
<span class="hljs-preprocessor"># yum list docker-ce.x86_64 --showduplicates | sort -r</span>
<span class="hljs-preprocessor">#   Loading mirror speeds from cached hostfile</span>
<span class="hljs-preprocessor">#   Loaded plugins: branch, fastestmirror, langpacks</span>
<span class="hljs-preprocessor">#   docker-ce.x86_64            17.03.1.ce-1.el7.centos            docker-ce-stable</span>
<span class="hljs-preprocessor">#   docker-ce.x86_64            17.03.1.ce-1.el7.centos            @docker-ce-stable</span>
<span class="hljs-preprocessor">#   docker-ce.x86_64            17.03.0.ce-1.el7.centos            docker-ce-stable</span>
<span class="hljs-preprocessor">#   Available Packages</span>
<span class="hljs-preprocessor"># Step2 : 安装指定版本的Docker-CE: (VERSION 例如上面的 17.03.0.ce.1-1.el7.centos)</span>
<span class="hljs-preprocessor"># sudo yum -y install docker-ce-[VERSION]</span></code>
  • 安装校验
<code>$ sudo docker <span class="hljs-built_in">version</span>
Client:
 Version:      <span class="hljs-number">17.03</span><span class="hljs-number">.0</span>-ce
 API <span class="hljs-built_in">version</span>:  <span class="hljs-number">1.26</span>
 Go <span class="hljs-built_in">version</span>:   go1<span class="hljs-number">.7</span><span class="hljs-number">.5</span>
 Git commit:   <span class="hljs-number">3</span>a232c8
 Built:        Tue Feb <span class="hljs-number">28</span> <span class="hljs-number">07</span>:<span class="hljs-number">52</span>:<span class="hljs-number">04</span> <span class="hljs-number">2017</span>
 OS/Arch:      linux/amd64

Server:
 Version:      <span class="hljs-number">17.03</span><span class="hljs-number">.0</span>-ce
 API <span class="hljs-built_in">version</span>:  <span class="hljs-number">1.26</span> (minimum <span class="hljs-built_in">version</span> <span class="hljs-number">1.12</span>)
 Go <span class="hljs-built_in">version</span>:   go1<span class="hljs-number">.7</span><span class="hljs-number">.5</span>
 Git commit:   <span class="hljs-number">3</span>a232c8
 Built:        Tue Feb <span class="hljs-number">28</span> <span class="hljs-number">07</span>:<span class="hljs-number">52</span>:<span class="hljs-number">04</span> <span class="hljs-number">2017</span>
 OS/Arch:      linux/amd64
 Experimental: <span class="hljs-constant">false</span></code>

1.3 配置镜像加速器(阿里云)

针对Docker客户端版本大于1.10.0的用户,可以通过修改daemon配置文件/etc/docker/daemon.json来使用加速器:

<code>$ <span class="hljs-built_in">sudo</span> mkdir -p /etc/docker
$ <span class="hljs-built_in">sudo</span> tee /etc/docker/daemon.json &lt;&lt;-<span class="hljs-string">'EOF'</span>
{
  <span class="hljs-string">"registry-mirrors"</span>: [<span class="hljs-string">"https://5f2jam6c.mirror.aliyuncs.com"</span>]
}
EOF
$ <span class="hljs-built_in">sudo</span> systemctl daemon-reload
$ <span class="hljs-built_in">sudo</span> systemctl restart docker</code>

1.4 docker命令详解

docker run命令用于运行一个新容器,而启动一个容器需要非常多的信息,所以该命令的参数非常多,参数详解如下:

<code>Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...] 
  -<span class="ruby">a, --attach=[]            登录容器(以docker run -d启动的容器) 
</span>  -<span class="ruby">c, --cpu-shares=<span class="hljs-number">0</span>         设置容器<span class="hljs-constant">CPU</span>权重,在<span class="hljs-constant">CPU</span>共享场景使用 
</span>  -<span class="ruby">-cap-add=[]               添加权限,权限清单详见:<span class="hljs-symbol">http:</span>/<span class="hljs-regexp">/linux.die.net/man</span><span class="hljs-regexp">/7/capabilities</span> 
</span>  -<span class="ruby">-cap-drop=[]              删除权限,权限清单详见:<span class="hljs-symbol">http:</span>/<span class="hljs-regexp">/linux.die.net/man</span><span class="hljs-regexp">/7/capabilities</span> 
</span>  -<span class="ruby">-cidfile=<span class="hljs-string">""</span>               运行容器后,在指定文件中写入容器<span class="hljs-constant">PID</span>值,一种典型的监控系统用法 
</span>  -<span class="ruby">-cpuset=<span class="hljs-string">""</span>                设置容器可以使用哪些<span class="hljs-constant">CPU</span>,此参数可以用来容器独占<span class="hljs-constant">CPU</span> 
</span>  -<span class="ruby">d, --detach=<span class="hljs-keyword">false</span>         指定容器运行于前台还是后台 
</span>  -<span class="ruby">-device=[]                添加主机设备给容器,相当于设备直通 
</span>  -<span class="ruby">-dns=[]                   指定容器的dns服务器 
</span>  -<span class="ruby">-dns-search=[]            指定容器的dns搜索域名,写入到容器的/etc/resolv.conf文件 
</span>  -<span class="ruby">e, --env=[]               指定环境变量,容器中可以使用该环境变量 
</span>  -<span class="ruby">-entrypoint=<span class="hljs-string">""</span>            覆盖image的入口点 
</span>  -<span class="ruby">-env-file=[]              指定环境变量文件,文件格式为每行一个环境变量 
</span>  -<span class="ruby">-expose=[]                指定容器暴露的端口,即修改镜像的暴露端口 
</span>  -<span class="ruby">h, --hostname=<span class="hljs-string">""</span>          指定容器的主机名 
</span>  -<span class="ruby">i, --interactive=<span class="hljs-keyword">false</span>    打开<span class="hljs-constant">STDIN</span>,用于控制台交互 
</span>  -<span class="ruby">-link=[]                  指定容器间的关联,使用其他容器的<span class="hljs-constant">IP</span>、env等信息 
</span>  -<span class="ruby">-lxc-conf=[]              指定容器的配置文件,只有在指定--exec-driver=lxc时使用 
</span>  -<span class="ruby">m, --memory=<span class="hljs-string">""</span>            指定容器的内存上限 
</span>  -<span class="ruby">-name=<span class="hljs-string">""</span>                  指定容器名字,后续可以通过名字进行容器管理,links特性需要使用名字 
</span>  -<span class="ruby">-net=<span class="hljs-string">"bridge"</span>             容器网络设置,待详述 
</span>  -<span class="ruby"><span class="hljs-constant">P</span>, --publish-all=<span class="hljs-keyword">false</span>    指定容器暴露的端口,待详述 
</span>  -<span class="ruby">p, --publish=[]           指定容器暴露的端口,待详述 
</span>  -<span class="ruby">-privileged=<span class="hljs-keyword">false</span>         指定容器是否为特权容器,特权容器拥有所有的capabilities 
</span>  -<span class="ruby">-restart=<span class="hljs-string">""</span>               指定容器停止后的重启策略,待详述 
</span>  -<span class="ruby">-rm=<span class="hljs-keyword">false</span>                 指定容器停止后自动删除容器(不支持以docker run -d启动的容器) 
</span>  -<span class="ruby">-sig-proxy=<span class="hljs-keyword">true</span>           设置由代理接受并处理信号,但是<span class="hljs-constant">SIGCHLD</span>、<span class="hljs-constant">SIGSTOP</span>和<span class="hljs-constant">SIGKILL</span>不能被代理 
</span>  -<span class="ruby">t, --tty=<span class="hljs-keyword">false</span>            分配tty设备,该可以支持终端登录 
</span>  -<span class="ruby">u, --user=<span class="hljs-string">""</span>              指定容器的用户 
</span>  -<span class="ruby">v, --volume=[]            给容器挂载存储卷,挂载到容器的某个目录 
</span>  -<span class="ruby">-volumes-from=[]          给容器挂载其他容器上的卷,挂载到容器的某个目录 
</span>  -<span class="ruby">w, --workdir=<span class="hljs-string">""</span>           指定容器的工作目录 </span></code>
  • 故障处理
    • –restart参数,支持三种逻辑实现:
<code>no:容器退出时不重启 
<span class="hljs-command"><span class="hljs-keyword">on</span>-failure:容器故障退出(返回值非零)时重启 </span>
always:容器退出时总是重启 </code>
  • 端口暴露

    • -P参数:docker自动映射暴露端口;

      # docker自动在host上打开49000到49900的端口,映射到容器(由镜像指定,或者--expose参数指定)的暴露端口;
      docker run -d -P training/webapp
    • -p参数:指定端口或IP进行映射;

      <code>
      <span class="hljs-comment"># host上5000号端口,映射到容器暴露的80端口;</span>
      
      docker run <span class="hljs-operator">-d</span> -p <span class="hljs-number">5000</span>:<span class="hljs-number">80</span> training/webapp
      
      <span class="hljs-comment"># host上127.0.0.1:5000号端口,映射到容器暴露的80端口;</span>
      
      docker run <span class="hljs-operator">-d</span> -p <span class="hljs-number">127.0</span>.<span class="hljs-number">0.1</span>:<span class="hljs-number">5000</span>:<span class="hljs-number">80</span> training/webapp
      
      <span class="hljs-comment"># host上127.0.0.1:随机端口,映射到容器暴露的80端口;</span>
      
      docker run <span class="hljs-operator">-d</span> -p <span class="hljs-number">127.0</span>.<span class="hljs-number">0.1</span>::<span class="hljs-number">5000</span> training/webapp 
      
      <span class="hljs-comment"># 绑定udp端口;</span>
      
      docker run <span class="hljs-operator">-d</span> -p <span class="hljs-number">127.0</span>.<span class="hljs-number">0.1</span>:<span class="hljs-number">5000</span>:<span class="hljs-number">5000</span>/udp training/webapp 
      </code>
  • 网络配置
<code><span class="hljs-comment">#  使用docker daemon指定的网桥</span>
<span class="hljs-comment">--net=bridge:</span>
 <span class="hljs-comment"># 容器使用主机的网络</span>
<span class="hljs-comment">--net=host:</span>
<span class="hljs-comment"># 使用其他容器的网路,共享IP和PORT等网络资源</span>
<span class="hljs-comment">--net=container:NAME_or_ID:</span>
<span class="hljs-comment"># 容器使用自己的网络(类似--net=bridge),但是不进行配置</span>
<span class="hljs-comment">--net=none: </span></code>

未经允许不得转载:冰点网络 » 1.docker安装

分享到:更多 ()

评论 抢沙发

评论前必须登录!