ECS上搭建Docker(CentOS7)

  • A+
所属分类:容器
高性能企业级服务器首台5折

本文讲述 Docker 在 CentOS 系统上的部署过程。关于 Ubuntu 系统下安装 Docker,具体实践请参考docker实践文档

适用对象

适用于熟悉Linux操作系统,刚开始使用阿里云ECS的开发者。

主要内容

  • 部署docker
  • docker基本用法
  • 镜像制作

部署Docker

本文主要说明手动安装docker的操作步骤,您也可以选择在云市场购买相应镜像,一键部署云服务器

本文实践操作系统版本为

1
CentOS 7.2 64 3.10.0-514.6.2.el7.x86_64

Docker要求64位的系统且内核版本至少为3.10

  1. 添加yum源。
    1. 1
      <span class="com"><span class="hljs-meta">#</span><span class="bash"> yum install epel-release –y</span></span>
    2. 1
      <span class="com"><span class="hljs-meta">#</span><span class="bash"> yum clean all</span></span>
    3. 1
      <span class="com"><span class="hljs-meta">#</span><span class="bash"> yum list</span></span>
  2. 安装并运行Docker。
    1. 1
      <span class="com"><span class="hljs-meta">#</span><span class="bash"> yum install docker-io –y</span></span>
    2. 1
      <span class="com"><span class="hljs-meta">#</span><span class="bash"> systemctl start docker</span></span>
  3. 检查安装结果。
    1. 1
      <span class="com"><span class="hljs-meta">#</span><span class="bash"> docker info</span></span>

    出现以下xin说明信息则表明安装成功。

    2

Docker基本用法

  1. Docker守护进程管理。
    1. 1
      <span class="com"><span class="hljs-meta">#</span><span class="bash"> systemctl start docker     <span class="hljs-comment">#运行Docker守护进程</span></span></span>
    2. 1
      <span class="com"><span class="hljs-meta">#</span><span class="bash"> systemctl stop docker      <span class="hljs-comment">#停止Docker守护进程</span></span></span>
    3. 1
      <span class="com"><span class="hljs-meta">#</span><span class="bash"> systemctl restart docker   <span class="hljs-comment">#重启Docker守护进程</span></span></span>
  2. 镜像管理。本文使用的是来自阿里云仓库的Apache镜像。
    1. 1
      <span class="com"><span class="hljs-meta">#</span><span class="bash"> docker pull registry.cn-hangzhou.aliyuncs.com/lxepoo/apache-php5</span></span>
    • 修改标签,由于阿里云仓库镜像的镜像名称很长,可以修改镜像标签以便记忆区分。
      1. 1
        <span class="com"><span class="hljs-meta">#</span><span class="bash"> docker tag  registry.cn-hangzhou.aliyuncs.com/lxepoo/apache-php5:latest aliweb:v1</span></span>
    • 查看已有镜像。
      1. 1
        <span class="com"><span class="hljs-meta">#</span><span class="bash"> docker images</span></span>
    • 强制删除镜像。
      1. 1
        <span class="com"><span class="hljs-meta">#</span><span class="bash"> docker  rmi  –f  registry.cn-hangzhou.aliyuncs.com/lxepoo/apache-php5</span></span>
  3. 容器管理。
    • e121d5f99e1e是执行docker images命令查询到的IMAGE ID,使用docker run命令进入容器。
      1. 1
        <span class="com"><span class="hljs-meta">#</span><span class="bash"> docker run –ti e121d5f99e1e /bin/bash</span></span>
    • 使用exit可以退出当前容器。
    • run命令加上–d参数可以在后台运行容器,—name指定容器命名为apache。
      1. 1
        <span class="com"><span class="hljs-meta">#</span><span class="bash"> docker run -d --name apache e121d5f99e1e</span></span>
    • 进入后台运行的容器。
      1. 1
        <span class="com"><span class="hljs-meta">#</span><span class="bash"> docker <span class="hljs-built_in">exec</span> -ti apache /bin/bash</span></span>
    • 将容器做成镜像。
      1. 1
        <span class="com"><span class="hljs-meta">#</span><span class="bash"> docker commit containerID/containerName  newImageName:tag</span></span>
    • 为了方便测试和恢复,先将源镜像运行起来后再做一个命名简单的镜像做测试。
      1. 1
        <span class="com"><span class="hljs-meta">#</span><span class="bash"> docker commit 4c8066cd8c01 apachephp:v1</span></span>
    • 运行容器并将宿主机的8080端口映射到容器里去。
      1. 1
        <span class="com"><span class="hljs-meta">#</span><span class="bash"> docker run -d -p 8080:80 apachephp:v1</span></span>

      在浏览器输入宿主机ip加8080端口访问测试,出现以下内容则说明运行成功。

      1

镜像制作

  1. 准备dockerfile内容。
    1. 1
      <span class="com"><span class="hljs-meta">#</span><span class="bash"> vim Dockerfile </span></span>
    2. 1
      <span class="pln">FROM apachephp</span><span class="pun">:</span><span class="pln">v1  </span><span class="com">#声明基础镜像来源</span>
    3. 1
      <span class="pln">MAINTAINER DTSTACK </span><span class="com">#声明镜像拥有者</span>
    4. 1
      <span class="pln">RUN mkdir </span><span class="pun">/</span><span class="pln">dtstact </span><span class="com">#RUN后面接容器运行前需要执行的命令,由于Dockerfile文件不能超过127行,因此当命令较多时建议写到脚本中执行</span>
    5. 1
      <span class="pln">ENTRYPOINT ping www</span><span class="pun">.</span><span class="pln">aliyun</span><span class="pun">.</span><span class="pln">com </span><span class="com">#开机启动命令,此处最后一个命令需要是可在前台持续执行的命令,否则容器后台运行时会因为命令执行完而退出。</span>
  2. 构建镜像。
    1. 1
      <span class="pln">docker build </span><span class="pun">-</span><span class="pln">t webcentos</span><span class="pun">:</span><span class="pln">v1 </span><span class="pun">.</span>   <span class="com"># . 是Dockerfile文件的路径,不能忽略</span>
    2. 1
      <span class="pln">docker images                    </span><span class="com">#查看是否创建成功</span>
    3. 1
      <span class="pln">docker run </span><span class="pun">–</span><span class="pln">d webcentos</span><span class="pun">:</span><span class="pln">v1       </span><span class="com">#后台运行容器</span>
    4. 1
      <span class="pln">docker ps                        </span><span class="com">#查看当前运行中的容器</span>
    5. 1
      <span class="pln">docker ps </span><span class="pun">–</span><span class="pln">a                     </span><span class="com">#查看所有容器,包括未运行中的</span>
    6. 1
      <span class="pln">docker logs CONTAINER ID</span><span class="pun">/</span><span class="pln">IMAGE   </span><span class="com">#如未查看到刚才运行的容器,则用容器id或者名字查看启动日志排错</span>
    7. 1
      <span class="pln">docker commit fb2844b6c070 dtstackweb</span><span class="pun">:</span><span class="pln">v1 </span><span class="com">#commit 后接容器id 和构建新镜像的名称和版本号。</span>
    8. 1
      <span class="pln">docker images                    </span><span class="com">#列出本地(已下载的和本地创建的)镜像</span>
    9. 1
      <span class="pln">docker push                      </span><span class="com">#将镜像推送至远程仓库,默认为 Docker Hub</span>
  3. 将镜像推送到registry。
    1. 1
      <span class="pln">docker login </span><span class="pun">--</span><span class="pln">username</span><span class="pun">=</span><span class="pln">dtstack_plus registry</span><span class="pun">.</span><span class="pln">cn</span><span class="pun">-</span><span class="pln">shanghai</span><span class="pun">.</span><span class="pln">aliyuncs</span><span class="pun">.</span><span class="pln">com </span><span class="com">#执行后输入镜像仓库密码</span>
    2. 1
      <span class="pln">docker tag </span><span class="pun">[</span><span class="typ">ImageId</span><span class="pun">]</span><span class="pln"> registry</span><span class="pun">.</span><span class="pln">cn</span><span class="pun">-</span><span class="pln">shanghai</span><span class="pun">.</span><span class="pln">aliyuncs</span><span class="pun">.</span><span class="pln">com</span><span class="pun">/</span><span class="pln">dtstack123</span><span class="pun">/</span><span class="pln">test</span><span class="pun">:[镜像版本号]</span>
    3. 1
      <span class="pln">docker push registry</span><span class="pun">.</span><span class="pln">cn</span><span class="pun">-</span><span class="pln">shanghai</span><span class="pun">.</span><span class="pln">aliyuncs</span><span class="pun">.</span><span class="pln">com</span><span class="pun">/</span><span class="pln">dtstack123</span><span class="pun">/</span><span class="pln">test</span><span class="pun">:[镜像版本号]</span>

    在镜像仓库能查看到镜像版本信息则说明push成功。

    其中[ImageId],[镜像版本号]请您根据自己的镜像信息进行填写。

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: