• docker在Ubuntu下1小时快速学习


    前言

    由于工作原因,很多情况下需要快速学习新的知识,针对docker如果从头到尾看相关书籍学习会非常慢,所以整理了下docker的常用操作,只要跟着本文学习操作,一小时就能掌握docker大部分最常用操作方法,也可以当做工具手册随时查找学习,当然本文未涉及的部分,还是需要通过阅读书籍学习,这文章的目的是帮助需要快速上手应用的人。由于写该文章的时候还比较早,所以所用系统和docker版本比较早,但是基本上其他版本操作基本一致,就不在重新更换版本重新编写。

    一、 Ubuntu 14.0.4系统安装docker

    1.1 在线安装docker

    以下操作步骤均在root用户下操作

    序列
    操作步骤
    详细说明
    1 检查内核是否符合要求 Docker 要求 Ubuntu 系统的内核版本高于 3.10 ,建议在 Ubuntu14.04 版本

    root@duke:/var/cache/apt/archives# uname -r
    3.13.0-135-generic

    2 安装docker

    root@duke:~# wget -qO- https://get.docker.com/ | sh

    root@duke:~# curl -sSL https://get.docker.com/ | sh
    # Executing docker install script, commit: 11aa13e
    + sh -c apt-get update -qq >/dev/null
    + sh -c apt-get install -y -qq apt-transport-https ca-certificates curl software-properties-common >/dev/null
    + sh -c curl -fsSL "https://download.docker.com/linux/ubuntu/gpg" | apt-key add -qq - >/dev/null
    + sh -c echo "deb [arch=amd64] https://download.docker.com/linux/ubuntu trusty edge" > /etc/apt/sources.list.d/docker.list
    + [ ubuntu = debian ]
    + sh -c apt-get update -qq >/dev/null
    + sh -c apt-get install -y -qq --no-install-recommends docker-ce >/dev/null
    + sh -c docker version
    Client:
    Version: 17.11.0-ce
    API version: 1.34
    Go version: go1.8.3
    Git commit: 1caf76c
    Built: Mon Nov 20 18:36:37 2017
    OS/Arch: linux/amd64
    Server:
    Version: 17.11.0-ce
    API version: 1.34 (minimum version 1.12)
    Go version: go1.8.3
    Git commit: 1caf76c
    Built: Mon Nov 20 18:35:10 2017
    OS/Arch: linux/amd64
    Experimental: false
    If you would like to use Docker as a non-root user, you should now consider
    adding your user to the "docker" group with something like:
    sudo usermod -aG docker your-user
    Remember that you will have to log out and back in for this to take effect!
    当要以非root用户可以直接运行docker时,需要执行 sudo usermod -aG docker runoob 命令,然后重新登陆,否则会有如下报错
    WARNING: Adding a user to the "docker" group will grant the ability to run
    containers which can be used to obtain root privileges on the
    docker host.
    Refer to https://docs.docker.com/engine/security/security/#docker-daemon-attack-surface
    for more information.

    3 启动docker 后台服务

    root@duke: service docker start
    start: Job is already running: docker
    root@duke:

    4 测试运行hello-world

    root@duke: docker run hello-world

    【注意】:如果在sh -c apt-get install -y -qq --no-install-recommends docker-ce >/dev/null这步失败时,报以下错误:

    E: 无法下载 https://download.docker.com/linux/ubuntu/dists/trusty/pool/edge/amd64/docker-ce_17.11.0~ce-0~ubuntu_amd64.deb Operation too slow. Less than 10 bytes/sec transferred the last 120 seconds

    可以先利用工具下载docker-ce_17.11.0~ce-0~ubuntu_amd64.deb,下好后,将docker-ce_17.11.0~ce-0~ubuntu_amd64.deb放入/var/cache/apt/archives目录即可。

    1.2 修改docker默认存储路径

    Docker使用会占用大量的磁盘空间。默认的存储路径是/var/lib/docker/,一般情况下/var/lib/路径是系统默认磁盘容量空间不会很大, 所以会造成docker占用磁盘满的问题产生。所以在安装好docker后,最好第一时间修改docker的默认存储路径。

    1. 查看docker当前配置

      docker info
      

      root@duke-211:/etc/systemd/system/docker.service.d# docker info
      Containers: 0
      Running: 0
      Paused: 0
      Stopped: 0
      Images: 0
      Server Version: 17.03.2-ce
      Storage Driver: aufs
      Root Dir: /var/lib/docker/aufs
      Backing Filesystem: extfs
      Dirs: 0
      Dirperm1 Supported: true
      Logging Driver: json-file
      Cgroup Driver: cgroupfs
      Plugins:
      Volume: local
      Network: bridge host macvlan null overlay
      Swarm: inactive
      Runtimes: runc
      Default Runtime: runc
      Init Binary: docker-init
      containerd version: 4ab9917febca54791c5f071a9d1f404867857fcc
      runc version: 54296cf40ad8143b62dbcaa1d90e520a2136ddfe
      init version: 949e6fa
      Security Options:
      apparmor
      seccomp
      Profile: default
      Kernel Version: 4.10.0-28-generic
      Operating System: Ubuntu 16.04.3 LTS
      OSType: linux
      Architecture: x86_64
      CPUs: 12
      Total Memory: 62.83 GiB
      Name: duke-211
      ID: IPDB:XJ53:E2RU:ML3E:BA4G:FQBL:GBSW:SBM5:M3PA:JI3G:A2TW:NPKO
      Docker Root Dir: /var/lib/docker 需要修改该路径
      Debug Mode (client): false
      Debug Mode (server): false
      Registry: https://index.docker.io/v1/
      Experimental: false
      Insecure Registries:
      127.0.0.0/8
      Live Restore Enabled: false

      WARNING: No swap limit support

    2. 创建docker配置目录(下面是ubuntu16.04的系统环境命令)

      mkdir -p /etc/systemd/system/docker.service.d
      
    3. 新增docker配置文件(下面是ubuntu16.04的系统环境命令)

      root@duke-211:/data1/docker# cd /etc/systemd/system/docker.service.d
      root@duke-211:/etc/systemd/system/docker.service.d# vi docker-overlay.conf
      [Service]
      ExecStart=
      ExecStart=/usr/bin/dockerd --graph="/home/docker" --storage-driver=overlay

    4. 停止docker

      service docker stop
      
    5. 重载docker配置

      systemctl daemon-reload
      
    6. 启动docker

      service docker start
      
    7. 查看docker当前配置

      root@duke-211:/etc/systemd/system/docker.service.d# docker info
      Containers: 0
      Running: 0
      Paused: 0
      Stopped: 0
      Images: 0
      Server Version: 17.03.2-ce
      Storage Driver: overlay
      Backing Filesystem: extfs
      Supports d_type: true
      Logging Driver: json-file
      Cgroup Driver: cgroupfs
      Plugins:
      Volume: local
      Network: bridge host macvlan null overlay
      Swarm: inactive
      Runtimes: runc
      Default Runtime: runc
      Init Binary: docker-init
      containerd version: 4ab9917febca54791c5f071a9d1f404867857fcc
      runc version: 54296cf40ad8143b62dbcaa1d90e520a2136ddfe
      init version: 949e6fa
      Security Options:
      apparmor
      seccomp
      Profile: default
      Kernel Version: 4.10.0-28-generic
      Operating System: Ubuntu 16.04.3 LTS
      OSType: linux
      Architecture: x86_64
      CPUs: 12
      Total Memory: 62.83 GiB
      Name: duke-212
      ID: OAIE:B5FM:CLEF:G4YS:DNDS:NSKV:JE26:JZYL:GOY3:DDLI:JGDV:OFHX
      Docker Root Dir: /home/docker 已经完成存储目录修改
      Debug Mode (client): false
      Debug Mode (server): false
      Registry: https://index.docker.io/v1/
      Experimental: false
      Insecure Registries:
      127.0.0.0/8
      Live Restore Enabled: false

      WARNING: No swap limit support

    二、 Docker使用非root用户

    通常我们使用Docker的时候都是使用的root,官方说法如下:

    The docker daemon binds to a Unix socket instead of a TCP port. By default that Unix socket is owned by the user root and other users can access it with sudo. For this reason, docker daemon always runs as the root user. 
    To avoid having to use sudo when you use the docker command, create a Unix group called docker and add users to it. When the docker daemon starts, it makes the ownership of the Unix socket read/writable by the docker group.

    下面是使用非root用户操作的步骤:

    1. 创建docker组
      sudo groupadd docker
      
    2. 将当前用户加入docker组
      sudo gpasswd -a ${USER} docker
      
    3. 重新启动docker服务(下面是CentOS7的命令)
      sudo systemctl restart docker
      
    4. 当前用户退出系统重新登陆
    5. 运行docker命令
      docker ps
      

    三、 镜像

    以下操作步骤均在root用户下操作

    3.1 镜像查找

    1. 镜像可以自己做,也可以从官方直接下载,在https://hub.docker.com就可以直接查找下载
    2. 也可以直接使用docker命令进行镜像查找如下表(例:不截断信息的搜索14.04.1的镜像),命令如下
      docker search --no-trunc=true 14.04.1
      
      执行过程如下:
      root@duke:/var/cache/apt/archives# docker search --no-trunc=true 14.04.1 
      NAME                                  DESCRIPTION                                                                                                             STARS                                                                  OFFICIAL            AUTOMATED
      linode/lamp                           LAMP on Ubuntu 14.04.1 LTS Container                                                                                    121                                                                                        
      araczkowski/oracle-apex-ords          Oracle Express Edition 11g Release 2 on Ubuntu 14.04.1 LTS with APEX 5 and ORDS                                         13                                                                                         [OK]
      b7alt/drupal                          Drupal >= 7.34 already installed, SQLite on Nginx, APC, SSH, drush, Ubuntu 14.04.1 LTS                                  5                                                                                          [OK]
      densuke/trusty-jp                     [Obsoleted] Ubuntu Linux 14.04LTS(14.04.1)に日本語の風味を付けておきました                                                   3                                                                                          [OK]
      matriphe/ubuntunginxphp               Ubuntu 14.04.1 (phusion/baseimage-docker) with Nginx and PHP.                                                           1                                                                                          [OK]
      zsoltm/ubuntu-armhf                   Ubuntu 14.04.1 minimal install, latest updates for ARMv7 (armhf) 
      

    3.2 本地镜像查看

    命令如下

    docker images
    

    执行过程如下:

    root@duke:~# docker   images 
    REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
    hello-world         latest              f2a91732366c        6 days ago          1.85kB
    ubuntu              14.04               d6ed29ffda6b        9 days ago          221MB
    

    3.3 镜像添加标签

    镜像可以添加多个标签,可以理解为镜像别名,添加标签的镜像,并没有新生成镜像,只是生成了一个新的连接,就像linux中的ln命令一样,具体操作命令如下:

    root@duke:~# docker images 
    REPOSITORY TAG IMAGE ID CREATED SIZE
    hello-world latest f2a91732366c 7 days ago 1.85kB
    ubuntu 14.04 d6ed29ffda6b 10 days ago 221MB
    registry latest a07e3f32a779 3 weeks ago 33.3MB
    root@duke:~# docker tag ubuntu:14.04 10.0.0.76:5000/test_registry
    root@duke:~# docker images
    REPOSITORY TAG IMAGE ID CREATED SIZE
    hello-world latest f2a91732366c 7 days ago 1.85kB
    ubuntu 14.04 d6ed29ffda6b 10 days ago 221MB
    10.0.0.76:5000/test_registry latest d6ed29ffda6b 10 days ago 221MB
    registry latest a07e3f32a779 3 weeks ago 33.3MB

    四、 容器

    4.1 创建容器

    root@duke:~# docker run -it ubuntu:14.04 bash 该命令是启动+创建容器
    root@ef664677b896:/# ping localhost 
    PING localhost (127.0.0.1) 56(84) bytes of data. 
    64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.054 ms 
    64 bytes from localhost (127.0.0.1): icmp_seq=2 ttl=64 time=0.026 ms 
    ^C 
    --- localhost ping statistics --- 
    2 packets transmitted, 2 received, 0% packet loss, time 999ms 
    rtt min/avg/max/mdev = 0.026/0.040/0.054/0.014 ms 
    root@ef664677b896:/# 
    root@ef664677b896:/# ssh 
    bash: ssh: command not found 
    root@ef664677b896:/# exit 退出容器伪终端,相应的容器也会被终止运行
    exit
    root@duke:~# 

    4.2 查看容器

    root@duke:~# docker ps -a 
    CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 
    ef664677b896 ubuntu:14.04 "bash" 21 minutes ago Exited (127) 2 minutes ago elegant_newton 
    d97b7dc8aadd hello-world "/hello" 2 hours ago Exited (0) 2 hours ago thirsty_jackson 

    4.3 删除容器

    root@duke:~# docker rm d97b7dc8aadd 
    d97b7dc8aadd
    root@duke:~# docker ps -a 
    CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 
    ef664677b896 ubuntu:14.04 "bash" About an hour ago Exited (127) 27 minutes ago elegant_newton 

    有时候会存在大量退出状态的容器,可以使用以下几个方法删除:

    • 方法一:查询所有的容器,过滤出Exited状态的容器,列出容器ID,删除这些容器

      sudo docker rm `docker ps -a|grep Exited|awk '{print $1}'`
      
    • 方法二:删除所有未运行的容器(已经运行的删除不了,未运行的就一起被删除了)

      sudo docker rm $(sudo docker ps -a -q)
      
    • 方法三:根据容器的状态,删除Exited状态的容器

      sudo docker rm $(sudo docker ps -qf status=exited)
      
    • 方法四(官方):Docker 1.13版本以后,可以使用 docker containers prune 命令,删除孤立的容器。

      sudo docker container prune
      

    4.4 终止容器

    root@duke:/var/run# docker ps -a 
    CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 
    ef664677b896 ubuntu:14.04 "bash" 2 hours ago Up 4 seconds elegant_newton
    root@duke:/var/run# docker stop ef664677b896 
    ef664677b896
    root@duke:/var/run# docker ps -a 
    CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 
    ef664677b896 ubuntu:14.04 "bash" 2 hours ago Exited (0) 47 seconds ago elegant_newton 

    4.5 启动容器

    root@duke:/var/run# docker ps -a 
    CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 
    ef664677b896 ubuntu:14.04 "bash" 2 hours ago Exited (127) 2 hours ago elegant_newton 
    root@duke:/var/run# docker start ef664677b896 
    ef664677b896
    root@duke:/var/run# docker ps -a 
    CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 
    ef664677b896 ubuntu:14.04 "bash" 2 hours ago Up 4 seconds elegant_newton 

    4.6 进入容器

    root@duke:/var/run# docker exec -it ef664677b896 /bin/bash 
    root@ef664677b896:/# hostname
    ef664677b896
    root@ef664677b896:/# exit
    exit
    root@duke:/var/run# docker ps -a 
    CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 
    ef664677b896 ubuntu:14.04 "bash" 2 hours ago Up 31 seconds 

    4.7 导出容器

    root@duke2:/data1/duke/docker/container# docker ps -a 
    CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 
    f9e7b88c5b99 ubuntu14.04-cn-ssh-vim:0.1 "/run.sh" 44 hours ago Up 44 hours 0.0.0.0:30001->22/tcp ubuntu14.04-cn-ssh-vim 
    68b9abed5bf8 flyceek/centos7-ssh "/usr/sbin/sshd -D" 2 days ago Up 2 hours 0.0.0.0:30002->22/tcp centos7-ssh 
    b7f4b7a70d99 ubuntu:14.04 "bash" 3 days ago Up 47 hours festive_brown 
    root@duke2:/data1/duke/docker/container# docker export 68b9abed5bf8 > centos7-ssh.tar 
    root@duke2:/data1/duke/docker/container# ls 
    centos7-ssh.tar 

    4.8 导入容器

    root@duke172:/data/docker/container# docker import centos7-ssh.tar centos7-ssh:v0.1 
    sha256:df1bf065ea466b18c32ac4c5492497d4622f8e477cffa216e65078986f4f56d3 
    root@duke172:/data/docker/container# docker images 
    REPOSITORY TAG IMAGE ID CREATED SIZE 
    centos7-ssh v0.1 df1bf065ea46 6 seconds ago 3.37GB 

    五、仓库

    前面章节下载的镜像都来自于hup.docker.com的公共镜像仓库,docker实际上还提供了本地镜像仓库,用于存放自建的镜像或者是从公共仓库中下载的镜像。

    5.1 创建本地仓库

    从公共仓库下载registry镜像,将会自动搭建本地私有仓库。
    默认创建仓库命令如下,此时将会将仓库创建在容器的/tmp/registry下:

    root@duke:~# docker run -d -p 5000:5000 registry

    如果要指定仓库的存放目录,就可以使用-v参数来进行路径指定,命令如下:

    root@duke:~# docker run -d -p 5000:5000 -v /home/docker/registry:/tmp/registry registry 
    Unable to find image 'registry:latest' locally 
    latest: Pulling from library/registry 
    49388a8c9c86: Pull complete 
    e4d43608dd22: Pull complete 
    3a41740f900c: Pull complete 
    e16ef4b76684: Pull complete 
    65f212f7c778: Pull complete 
    Digest: sha256:d837de65fd9bdb81d74055f1dc9cc9154ad5d8d5328f42f57f273000c402c76d 
    Status: Downloaded newer image for registry:latest 
    8893ffeb80dd5746453cfa6ed43f29d0bb640de607d94fef0f31ea080ad15fb7 

    本地仓库创建完成后,会自动启动5000监听端口

    5.2 验证本地仓库

    在能够连接本地镜像服务器的浏览器中输入http://10.0.0.76:5000/v2/进行登录查看,返回json数据就表示安装成功

    【注意】:
    本地仓库是存在版本的
    当前下载的仓库镜像版本为V2
    因此网上使用http://10.0.0.76:5000/v1的查看方式是无法生效的,只会返回404 page not found错误

    5.3 上传镜像到本地仓库

    将自己别名的镜像上传到本地仓库(必须使用tag别名私有仓库地址前缀,否则无法上传到私有镜像库),操作如下:

    root@duke:~# docker push 10.0.0.76:5000/test_registry 
    The push refers to repository [10.0.0.76:5000/test_registry] 
    Get https://10.0.0.76:5000/v2/: http: server gave HTTP response to HTTPS client 
    root@duke:~# 

    但是在上述命令中报错,信息为:

    Get https://10.0.0.76:5000/v2/: http: server gave HTTP response to HTTPS client

    这个问题可能是由于客户端采用https,docker registry未采用https服务所致。
    处理方式是把客户对地址“10.0.0.76:5000”请求改为http。
    【修改方法】:
    1、在docker1.12.3以前的版本,修改docker的配置文件/etc/systemconfig/docker,重启docker来解决这个问题。
    2、在docker1.12.3以后的版本,在/etc/docker/目录下,创建daemon.json文件。在文件中写入{ "insecure-registries":["10.0.0.76:5000"] },保存退出后,重启docker。
    操作步骤如下:

    序列操作步骤
    详细说明
    1 新增配置

    root@duke:~# vim /etc/docker/daemon.json
    { "insecure-registries":["10.0.0.76:5000"] }
    ~
    "/etc/docker/daemon.json" [] 1L, 45C 已写入

    2 重启docker

    root@duke:~# service docker restart
    docker stop/waiting
    docker start/running, process 20676

    3 查看容器状态

    root@duke:~# docker ps -a
    CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
    8893ffeb80dd registry "/entrypoint.sh /etc" About an hour ago Exited (2) 52 seconds ago infallible_swirles
    ef664677b896 ubuntu:14.04 "bash" 18 hours ago Exited (0) 52 seconds ago elegant_newton

    4 启动本地仓库容器

    root@duke:~# docker start 8893ffeb80dd
    8893ffeb80dd

    5 上传镜像到本地仓库

    root@duke:~# docker push 10.0.0.76:5000/test_registry
    The push refers to repository [10.0.0.76:5000/test_registry]
    59482791e4b2: Pushed
    cd514e6bdf2f: Pushed
    02323b2bcb37: Pushed
    c088f4b849d4: Pushed
    c08b59ef4a3d: Pushed
    latest: digest: sha256:f558f2d306f8cb0390426da1e18e9489a870c4e66876f03bed29dec4c6aa62c2 size: 1359

    5.4 查看本地仓库镜像

    【查看方式一】:

    root@duke:~# curl http://10.0.0.76:5000/v2/_catalog 
    {"repositories":["test_registry"]} 
    root@duke:~# curl http://10.0.0.76:5000/v2/test_registry/tags/list 
    {"name":"test_registry","tags":["latest"]} 

    【查看方式二】:
    在能够连接本地镜像服务器的浏览器中输入http://10.0.0.76:5000/v2/_catalog进行登录查看,返回json数据就表示安装成功,如下图:

    详细 curl 操作docker仓库,见官方文档

    5.5 下载本地仓库镜像

    序列操作步骤
    详细说明
    1 查看本地镜像

    root@duke:~# docker images
    REPOSITORY TAG IMAGE ID CREATED SIZE
    hello-world latest f2a91732366c 7 days ago 1.85kB
    10.0.0.76:5000/test_registry latest d6ed29ffda6b 10 days ago 221MB
    ubuntu 14.04 d6ed29ffda6b 10 days ago 221MB
    registry latest a07e3f32a779 3 weeks ago 33.3MB

    2 删除已经存在的镜像

    root@duke:~# docker rmi 10.0.0.76:5000/test_registry
    Untagged: 10.0.0.76:5000/test_registry:latest
    Untagged: 10.0.0.76:5000/test_registry@sha256:f558f2d306f8cb0390426da1e18e9489a870c4e66876f03bed29dec4c6aa62c2

    3 查看本地镜像

    root@duke:~# docker images
    REPOSITORY TAG IMAGE ID CREATED SIZE
    hello-world latest f2a91732366c 7 days ago 1.85kB
    ubuntu 14.04 d6ed29ffda6b 10 days ago 221MB
    registry latest a07e3f32a779 3 weeks ago 33.3MB

    4 查看容器状态

    root@duke:~# docker ps -a
    CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
    8893ffeb80dd registry "/entrypoint.sh /etc …" 4 hours ago Exited (2) 3 minutes ago infallible_swirles
    ef664677b896 ubuntu:14.04 "bash" 21 hours ago Exited (0) 3 hours ago elegant_newton

    5 启动本地仓库容器

    root@duke:~# docker start 8893ffeb80dd
    8893ffeb80dd

    6 从本地仓库下载镜像

    root@duke:~# docker pull 10.0.0.76:5000/test_registry
    Using default tag: latest
    latest: Pulling from test_registry
    Digest: sha256:f558f2d306f8cb0390426da1e18e9489a870c4e66876f03bed29dec4c6aa62c2
    Status: Downloaded newer image for 10.0.0.76:5000/test_registry:latest

    7 查看镜像是否下载

    root@duke:~# docker images
    REPOSITORY TAG IMAGE ID CREATED SIZE
    hello-world latest f2a91732366c 7 days ago 1.85kB
    10.0.0.76:5000/test_registry latest d6ed29ffda6b 10 days ago 221MB
    ubuntu 14.04 d6ed29ffda6b 10 days ago 221MB
    registry latest a07e3f32a779 3 weeks ago 33.3MB

    5.6 设置本地仓库安全校验

    由于有的新版本的docker对安全性要求较高,所以需要仓库支持SSL/TLS的证书,对于内部使用的私有仓库,可以自行配置证书,或者关闭仓库的安全检验。
    仓库关闭证书校验方法如下:

    序列操作步骤
    详细说明
    1 修改配置

    root@duke:~# vi /etc/docker/daemon.json
    { "insecure-registries":["10.0.0.76:5000"] }
    DOCKER_OPTS="--insecure-registry 10.0.0.76:5000"
    ~
    "/etc/docker/daemon.json" 2L, 94C 已写入

    2 重启docker

    root@duke:~# service docker restart

    六、创建镜像--开机启动ssh

    以下操作步骤均在root用户下操作,执行以下命令:

    序列操作步骤
    详细说明
    1 查看容器状态

    root@duke:~# docker ps -a
    CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
    8893ffeb80dd registry "/entrypoint.sh /etc" 5 hours ago Up 44 minutes 0.0.0.0:5000->5000/tcp infallible_swirles
    ef664677b896 ubuntu:14.04 "bash" 22 hours ago Exited (0) 4 hours ago elegant_newton

    2 启动容器

    root@duke:~# docker start ef664677b896
    ef664677b896

    3 进入容器

    root@duke:~# docker exec -it ef664677b896 /bin/bash

    4 安装ssh

    root@ef664677b896:/# ssh
    bash: ssh: command not found
    root@ef664677b896:/# apt-get install ssh
    Reading package lists... Done
    Building dependency tree
    Reading state information... Done
    E: Unable to locate package ssh
    root@ef664677b896:/# apt-get update
    Get:1 http://security.ubuntu.com trusty-security InRelease [65.9 kB]
    Ign http://archive.ubuntu.com trusty InRelease
    Get:2 http://archive.ubuntu.com trusty-updates InRelease [65.9 kB]
    。。。。。。
    Fetched 21.1 MB in 48s (440 kB/s)
    Reading package lists... Done
    root@ef664677b896:/# apt-get install ssh
    Reading package lists... Done
    Building dependency tree
    。。。。。。
    Processing triggers for ca-certificates (20170717~14.04.1) ...
    Updating certificates in /etc/ssl/certs... 148 added, 0 removed; done.
    Running hooks in /etc/ca-certificates/update.d....done.

    5 使用ssh远程登陆

    root@ef664677b896:/# ssh root@10.0.0.11
    The authenticity of host '10.0.0.11 (10.0.0.11)' can't be established.
    ECDSA key fingerprint is 52:0d:55:c6:21:0f:9c:50:b6:37:05:5a:90:13:75:06.
    Are you sure you want to continue connecting (yes/no)? yes
    Warning: Permanently added '10.0.0.11' (ECDSA) to the list of known hosts.
    root@10.0.0.11's password:
    Welcome to Ubuntu 14.04.1 LTS (GNU/Linux 3.13.0-32-generic x86_64)
    * Documentation: https://help.ubuntu.com/
    619 packages can be updated.
    352 updates are security updates.
    Last login: Tue Nov 28 14:07:59 2017 from 10.0.0.76
    root@duke2:~# exit
    登出
    Connection to 10.0.0.11 closed.

    6 创建ssh目录

    创建/var/run/sshd目录,该目录必须存在
    root@ef664677b896:/#mkdir -p /var/run/sshd

    7 启动ssh

    root@ef664677b896:/#/usr/sbin/sshd -D &

    8 修改root密码

    root@ef664677b896:/#passwd root
    Enter new UNIX password:
    Retype new UNIX password:
    passwd: password updated successfully

    9 修改pam限制

    root@ef664677b896:/#sed -ri 's/session required pam_loginuid.so/#session required pam_loginuid.so/g' /etc/pam.d/sshd

    10 修改ssh配置文件

    注释PermitRootLogin without-password
    添加PermitRootLogin yes

    root@ef664677b896:/# vi /etc/ssh/sshd_config
    # Package generated configuration file
    。。。。。。
    LoginGraceTime 120
    #PermitRootLogin without-password
    PermitRootLogin yes

    StrictModes yes

    11 新建容器启动执行脚本

    root@ef664677b896:/#cd / 必须在 / 目录下创建执行脚本
    root@ef664677b896:/#vi run.sh
    #!/bin/bash
    /usr/sbin/sshd -D

    root@ef664677b896:/#chmod +x run.sh

    12 退出容器

    root@ef664677b896:/# exit
    exit

    13 生成包含ssh功能的镜像

    root@duke:~# docker commit -m "ubuntu 14.04 add ssh service" -a "hzw-duke" ef664677b896 ubuntu14.04-ssh:0.1
    sha256:8443c965ca81a638444b6bd0b0f938fbebaa49aed4368084d3f298d3ebacdd2c

    14 查看镜像库

    root@duke:~# docker images
    REPOSITORY TAG IMAGE ID CREATED SIZE
    ubuntu14.04-ssh 0.1 8443c965ca81 14 seconds ago 284MB
    hello-world latest f2a91732366c 7 days ago 1.85kB
    ubuntu 14.04 d6ed29ffda6b 10 days ago 221MB
    10.0.0.76:5000/test_registry latest d6ed29ffda6b 10 days ago 221MB
    registry latest a07e3f32a779 3 weeks ago 33.3MB

    七、 存出镜像

    以下操作步骤均在root用户下操作,执行以下命令:
    方法1:

    序列操作步骤
    详细说明
    1 查看镜像

    root@duke:~# docker images
    REPOSITORY TAG IMAGE ID CREATED SIZE
    ubuntu14.04-ssh 0.1 8443c965ca81 About an hour ago 284MB
    hello-world latest f2a91732366c 7 days ago 1.85kB
    10.0.0.76:5000/test_registry latest d6ed29ffda6b 10 days ago 221MB
    ubuntu 14.04 d6ed29ffda6b 10 days ago 221MB
    registry latest a07e3f32a779 3 weeks ago 33.3MB

    2 存出镜像

    root@duke:~# docker save ubuntu14.04-ssh > ubuntu14.04-ssh.tar

     
    3 查看镜像包

    root@duke:~# ls -alh ubuntu14.04-ssh.tar
    -rw-r--r-- 1 root root 284M 11 月 28 16:37 ubuntu14.04-ssh.tar

    方法2:

    序列操作步骤
    详细说明
    1 查看镜像

    root@duke:~# docker images
    REPOSITORY TAG IMAGE ID CREATED SIZE
    ubuntu14.04-ssh 0.1 8443c965ca81 About an hour ago 284MB
    hello-world latest f2a91732366c 7 days ago 1.85kB
    10.0.0.76:5000/test_registry latest d6ed29ffda6b 10 days ago 221MB
    ubuntu 14.04 d6ed29ffda6b 10 days ago 221MB
    registry latest a07e3f32a779 3 weeks ago 33.3MB

    2 存出镜像

    root@duke:~# docker save --output ubuntu14.04-ssh.tar ubuntu14.04-ssh

    3 查看镜像包

    root@duke:~# ls -alh ubuntu14.04-ssh.tar
    -rw-r--r-- 1 root root 284M 11 月 28 16:37 ubuntu14.04-ssh.tar

    方法3:

    序列操作步骤
    详细说明
    1 查看镜像

    root@duke:~# docker images
    REPOSITORY TAG IMAGE ID CREATED SIZE
    ubuntu14.04-ssh 0.1 8443c965ca81 About an hour ago 284MB
    hello-world latest f2a91732366c 7 days ago 1.85kB
    10.0.0.76:5000/test_registry latest d6ed29ffda6b 10 days ago 221MB
    ubuntu 14.04 d6ed29ffda6b 10 days ago 221MB
    registry latest a07e3f32a779 3 weeks ago 33.3MB

    2 存出镜像

    root@duke:~# docker save -o ubuntu14.04-ssh.tar ubuntu14.04-ssh

    3 查看镜像包

    root@duke:~# ls -alh ubuntu14.04-ssh.tar
    -rw-r--r-- 1 root root 284M 11 月 28 16:37 ubuntu14.04-ssh.tar

    方法4:

    序列操作步骤
    详细说明
    1 查看镜像

    root@duke:~# docker images
    REPOSITORY TAG IMAGE ID CREATED SIZE
    ubuntu14.04-ssh 0.1 8443c965ca81 About an hour ago 284MB
    hello-world latest f2a91732366c 7 days ago 1.85kB
    10.0.0.76:5000/test_registry latest d6ed29ffda6b 10 days ago 221MB
    ubuntu 14.04 d6ed29ffda6b 10 days ago 221MB
    registry latest a07e3f32a779 3 weeks ago 33.3MB

    2 存出镜像

    root@duke:~# docker save -o ubuntu14.04-ssh.tar ubuntu14.04-ssh:0.1

    3 查看镜像包

    root@duke:~# ls -alh ubuntu14.04-ssh.tar
    -rw-r--r-- 1 root root 284M 11 月 28 16:37 ubuntu14.04-ssh.tar

    八、 载入镜像

    以下操作步骤均在root用户下操作,执行以下命令:
    方法1:

    序列操作步骤
    详细说明
    1 查看镜像包

    root@duke:~# ls -alh ubuntu14.04-ssh.tar
    -rw-r--r-- 1 root root 284M 11 月 28 16:37 ubuntu14.04-ssh.tar

    2 载入镜像

    root@duke:~# docker load --input ubuntu14.04-ssh.tar

    3 查看镜像

    root@duke:~# docker images
    REPOSITORY TAG IMAGE ID CREATED SIZE
    ubuntu14.04-ssh 0.1 8443c965ca81 About an hour ago 284MB
    hello-world latest f2a91732366c 7 days ago 1.85kB
    10.0.0.76:5000/test_registry latest d6ed29ffda6b 10 days ago 221MB
    ubuntu 14.04 d6ed29ffda6b 10 days ago 221MB
    registry latest a07e3f32a779 3 weeks ago 33.3MB

    方法2:

    序列操作步骤
    详细说明
    1 查看镜像包

    root@duke:~# ls -alh ubuntu14.04-ssh.tar
    -rw-r--r-- 1 root root 284M 11 月 28 16:37 ubuntu14.04-ssh.tar

    2 载入镜像

    root@duke:~# docker load < ubuntu14.04-ssh.tar

    3 查看镜像

    root@duke:~# docker images
    REPOSITORY TAG IMAGE ID CREATED SIZE
    ubuntu14.04-ssh 0.1 8443c965ca81 About an hour ago 284MB
    hello-world latest f2a91732366c 7 days ago 1.85kB
    10.0.0.76:5000/test_registry latest d6ed29ffda6b 10 days ago 221MB
    ubuntu 14.04 d6ed29ffda6b 10 days ago 221MB
    registry latest a07e3f32a779 3 weeks ago 33.3MB

    九、 docker拥有root权限

    root@duke:~# docker run -it --privileged=true ubuntu:14.04

    privileged 参数功能,设置为true的时候,让docker的root拥有真正root的权限,可以调用宿主机硬件等,甚至可以让你在docker中使用docker

    十、 docker镜像下载加速

    由于官方镜像下载非常缓慢,所以需要加速器进行加速下载,这样的加速器很多,可以选择下面加速器进行下载加速:

    1. 登陆到https://www.daocloud.io/mirror后,进行注册
    2. 注册后就能得到下面信息

    3. 在宿主机上执行以下操作就能完成下载加速,命令如下:

      curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://8ad3346c.m.daocloud.io
      

      执行过程如下:

      root@duke:/opt# curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://8ad3346c.m.daocloud.io
      docker version >= 1.12
      {"registry-mirrors": ["http://8ad3346c.m.daocloud.io"], "insecure-registries":["10.0.0.76:5000"] }
      Success.
      You need to restart docker to take effect: sudo systemctl restart docker.service
      root@duke:/opt# service docker restart

    4. 以上操作实际上就是新增修改/etc/docker/daemon.json文件,信息如下:

      root@duke:/etc/systemd# vi /etc/docker/daemon.json
      {
       "registry-mirrors": ["http://8ad3346c.m.daocloud.io"],
       "insecure-registries":["10.0.0.76:5000"]
      }

      "registry-mirrors": ["http://8ad3346c.m.daocloud.io"] 这个就是加速地址
      "insecure-registries":["10.0.0.76:5000"] 这个是本地仓库地址

    十一、 支持外部访问docker容器

    创建一个外部可以通过ssh访问的docker并且支持和宿主机共享一个目录文件夹的方法:

    1. 执行下面命令创建一个可以通过宿主机30001访问docker容器22端口容器,并且共享/mnt目录,用于系统操作,执行前提是安装好ssh软件

      root@duke:/etc/systemd#docker run -p 30001:22 --name ubuntu14.04-ssh-vim -v /mnt:/mnt -d ubuntu14.04-ssh-vim:0.1 /run.sh

    2. 进入容器后,安装相关软件
    3. 然后生成新的镜像即可,在生成新的镜像前,不能关闭容器,否则,系统还原

    十二、 Nvidia-docker 1.0

    12.1 在线安装nvidia-docker

    命令如下:

    wget -P /tmp https://github.com/NVIDIA/nvidia-docker/releases/download/v1.0.1/nvidia-docker_1.0.1-1_amd64.deb
    

    执行过程如下:

    root@duke:~# wget -P /tmp https://github.com/NVIDIA/nvidia-docker/releases/download/v1.0.1/nvidia-docker_1.0.1-1_amd64.deb
    --2017-11-29 10:48:50-- https://github.com/NVIDIA/nvidia-docker/releases/download/v1.0.1/nvidia-docker_1.0.1-1_amd64.deb
    正在解析主机 github.com (github.com)... 192.30.255.113, 192.30.255.112
    正在连接 github.com (github.com)|192.30.255.113|:443... 已连接。
    已发出 HTTP 请求,正在等待回应... 302 Found
    位置:https://github-production-release-asset-2e65be.s3.amazonaws.com/45557469/d4efc7cc-ff73-11e6-91a2-ce84b8670fcd?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIWNJYAX4CSVEH53A%2F20171129%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20171129T024853Z&X-Amz-Expires=300&X-Amz-Signature=1813c7a54d9cbf87cd63f4bae62337eb8a08db86c6e278776f0df49de8a1975f&X-Amz-SignedHeaders=host&actor_id=0&response-content-disposition=attachment%3B%20filename%3Dnvidia-docker_1.0.1-1_amd64.deb&response-content-type=application%2Foctet-stream [跟随至新的 URL]
    --2017-11-29 10:48:53-- https://github-production-release-asset-2e65be.s3.amazonaws.com/45557469/d4efc7cc-ff73-11e6-91a2-ce84b8670fcd?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIWNJYAX4CSVEH53A%2F20171129%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20171129T024853Z&X-Amz-Expires=300&X-Amz-Signature=1813c7a54d9cbf87cd63f4bae62337eb8a08db86c6e278776f0df49de8a1975f&X-Amz-SignedHeaders=host&actor_id=0&response-content-disposition=attachment%3B%20filename%3Dnvidia-docker_1.0.1-1_amd64.deb&response-content-type=application%2Foctet-stream
    正在解析主机 github-production-release-asset-2e65be.s3.amazonaws.com (github-production-release-asset-2e65be.s3.amazonaws.com)... 52.216.129.11
    正在连接 github-production-release-asset-2e65be.s3.amazonaws.com (github-production-release-asset-2e65be.s3.amazonaws.com)|52.216.129.11|:443... 已连接。
    已发出 HTTP 请求,正在等待回应... 200 OK
    长度: 2266050 (2.2M) [application/octet-stream]
    正在保存至: "/tmp/nvidia-docker_1.0.1-1_amd64.deb.1"
    8% [=======> ] 181,822 3.59KB/s 估时 9m 27s
    root@duke:~#dpkg -i /tmp/nvidia-docker*.deb

    12.2 离线安装nvidia-docker

    在官方下载nvidia-docker非常缓慢,可以选择其他渠道进行下载nvidia-docker,通过U盘存入到ubuntu系统后,在进行安装。安装命令如下:

    root@duke:~/nvidia-docker# dpkg -i nvidia-docker_1.0.1-1_amd64_1.deb
    (正在读取数据库 ... 系统当前共安装有 200118 个文件和目录。)
    正准备解包 nvidia-docker_1.0.1-1_amd64_1.deb ...
    正在解包 nvidia-docker (1.0.1-1) ...
    正在设置 nvidia-docker (1.0.1-1) ...
    Configuring user
    Setting up permissions
    nvidia-docker start/running, process 2740
    正在处理用于 ureadahead (0.100.0-16) 的触发器 ...

    12.3 测试nvidia-docker

    root@duke:~/nvidia-docker# nvidia-docker run --rm nvidia/cuda nvidia-smi
    Using default tag: latest
    latest: Pulling from nvidia/cuda
    660c48dd555d: Pull complete
    4c7380416e78: Pull complete
    421e436b5f80: Pull complete
    e4ce6c3651b3: Pull complete
    be588e74bd34: Pull complete
    f597507b3c37: Pull complete
    9c5d4127a23d: Pull complete
    398bf259fcdc: Pull complete
    4f4092762618: Pull complete
    94130a21e154: Pull complete
    Digest: sha256:954c82d2d060f38de13b3d7933b7e1549b25330cc6412008dc1253f3c148448d
    Status: Downloaded newer image for nvidia/cuda:latest
    Wed Nov 29 03:33:46 2017
    +-----------------------------------------------------------------------------+
    | NVIDIA-SMI 384.66 Driver Version: 384.66 |
    |-------------------------------+----------------------+----------------------+
    | GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC |
    | Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. |
    |===============================+======================+======================|
    | 0 GeForce GTX 108... Off | 00000000:02:00.0 Off | N/A |
    | 23% 28C P8 9W / 250W | 10MiB / 11172MiB | 0% Default |
    +-------------------------------+----------------------+----------------------+

    +-----------------------------------------------------------------------------+
    | Processes: GPU Memory |
    | GPU PID Type Process name Usage |
    |=============================================================================|
    | No running processes found |
    +-----------------------------------------------------------------------------+

    12.4 使用nvidia-docker的要素

    • 安装了nvidia-docker不代表可以直接从宿主机中获取显卡驱动,还是需要在容器中安装cuda+cudnn
    • 宿主机如果不安装显卡驱动,就算nvidia-docker启动的容器安装了cuda+cudnn,也是无法使用显卡的
    • 如果想省事的直接获取安装好cuda+cudnn的容器镜像,可以在docker的官网获取对应版本的容器,地址为:https://hub.docker.com/r/nvidia/cuda/

    十三、 Nvidia-docker 2.0

    13.1 在线安装nvidia-docker2.0

    1. 删除nvidia-docker 1.0的相关容器
      docker volume ls -q -f driver=nvidia-docker | xargs -r -I {} -n1 docker ps -q -a -f volume = {} | xargs -r docker rm -f
      
    2. 删除nvidia-docker 1.0
      apt-get purge -y nvidia-docker
      
    3. 添加nvidia-docker的软件源
      添加密钥

      curl -s -L https://nvidia.github.io/nvidia-docker/gpgkey |sudo apt-key add -
      

      获取版本信息给变量

      distribution=$(. /etc/os-release;echo $ID$VERSION_ID)
      

      将nvidia-docker源添加到源列表

      curl -s -L https://nvidia.github.io/nvidia-docker/$distribution/nvidia-docker.list |tee /etc/apt/sources.list.d/nvidia-docker.list
      

      更新源

      apt-get update
      
    4. 安装nvidia-docker2(注意nvidia-docker2必须使用docker-ce 18.06.3版本,否则安装报错

      apt-get install -y nvidia-docker2
      
    5. 设置docker配置
      tee /etc/docker/daemon.json <<EOF
      {
       "runtimes": {
           "nvidia": {
               "path": "/usr/bin/nvidia-container-runtime",
               "runtimeArgs": []
           }
       }
      }
      EOF
      
    6. 重启docker
      pkill -SIGHUP dockerd
      

    13.2 nvidia-docker2.0使用命令

    1. 使用全部gpu
      docker run --runtime=nvidia --rm nvidia/cuda nvidia-smi
      或
      docker run --runtime=nvidia -e NVIDIA_VISIBLE_DEVICES=all --rm nvidia/cuda nvidia-smi
      
    2. 指定使用某个gpu
      docker run --runtime=nvidia -e NVIDIA_VISIBLE_DEVICES=0 --rm nvidia/cuda nvidia-smi
      或
      docker run --runtime=nvidia -e NVIDIA_VISIBLE_DEVICES=1 --rm nvidia/cuda nvidia-smi
      
    3. 指定使用多个gpu
      docker run --runtime=nvidia -e NVIDIA_VISIBLE_DEVICES=0,1 --rm nvidia/cuda nvidia-smi
      

    十四、 安装weave网络控制

    Weave和flannel一样是用于控制网络层软件,可以利用该软件做多机之间的容器跨主机网络连接。
    安装方法:
    1、直接在网上下载weave,然后将之放到/usr/local/bin/目录下,用chmod a+x /usr/local/bin/weave命令给weave附上执行权限,即可完成安装
    2、 当使用weave时,weave会从官网下载docker镜像创建容器,从而打开应用,此时,可以将预先下载好的镜像存出,例如:weaveworks-weave-2.1.3.tar weaveworks-weavedb.tar weaveworks-weaveexec-2.1.3.tar这3个包,然后在安装的机器上进行docker镜像存入。
    3、 完成上面操作后,启动weave,docker就会自动创建weave所依赖的容器。

    十五、 应用实例

    15.1 Nvidia-docke0.1 创建跨机指定CPU、GPU、内存、永久持久卷的跨机网络连接的容器

    15.1.1 硬件及容器配置信息:

    宿主机IP地址容器容器IP地址
    服务器1 10.0.0.213 Ubuntu14.04 192.168.0.2
    服务器2 10.0.0.214 Ubuntu14.04 192.168.0.3

    15.1.2安装weave和bridge-utils:

    weave参考上章节,bridge-utils安装如下:

    root@duke-1:~# apt-get install bridge-utils
    正在读取软件包列表... 完成
    正在分析软件包的依赖关系树
    正在读取状态信息... 完成
    下列【新】软件包将被安装:
    bridge-utils
    升级了 0 个软件包,新安装了 1 个软件包,要卸载 0 个软件包,有 147 个软件包未被升级。
    需要下载 28.6 kB 的归档。
    解压缩后会消耗 102 kB 的额外空间。
    获取:1 http://10.31.48.30/ubuntu xenial/main amd64 bridge-utils amd64 1.5-9ubuntu1 [28.6 kB]
    已下载 28.6 kB,耗时 0秒 (308 kB/s)
    正在选中未选择的软件包 bridge-utils。
    (正在读取数据库 ... 系统当前共安装有 242652 个文件和目录。)
    正准备解包 .../bridge-utils_1.5-9ubuntu1_amd64.deb ...
    正在解包 bridge-utils (1.5-9ubuntu1) ...
    正在处理用于 man-db (2.7.5-1) 的触发器 ...
    正在设置 bridge-utils (1.5-9ubuntu1) ...

    15.1.3 10.0.0.213主机配置

    1. 启动weave

      root@duke-2:~# weave launch
      Network 10.32.0.0/12 overlaps with existing route 10.0.0.128/25 on host
      ERROR: Default --ipalloc-range 10.32.0.0/12 overlaps with existing route on host.
      You must pick another range and set it on all hosts.

    2. 因为在内网环境,导致默认的ip和内网ip存在冲突,所以报错,采用指定ip方式启动

      root@duke-2:~# weave launch --ipalloc-range 10.2.0.0/16
      8b2b34704e7dc5e4eac3c517ada5d79f5a52c488ef9d5a02551e75e757654b52

    3. Weave启动后,就会在docker下创建几个依赖的容器,内容如下:

      root@duke-2:~# docker ps -a
      CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
      8b2b34704e7d weaveworks/weave:2.1.3 "/home/weave/weaver …" 26 seconds ago Up 25 seconds weave
      6ceceb78c749 weaveworks/weaveexec:2.1.3 "data-only" 26 seconds ago Created weavevolumes-2.1.3
      6c0e3cd0fe84 weaveworks/weavedb:latest "data-only" 26 seconds ago Created weavedb
      f46a3490457b ubuntu "/bin/bash" 22 hours ago Up 2 hours a0262000091

    4. 指定GPU、CPU、内存、数据地址、永久卷地址创建容器

      root@duke-2:~# NV_GPU=0 nvidia-docker run -p 30001:22 --name ufo -v /data1/docker_disk/ufo:/work -v /data/caffe_data:/data --cpuset-cpus=0,1,2,3 -m 16g --memory-swap 0 -it -d duke/cuda8-cudnn6-devel-ubuntu14.04-ssh:0.1 /run.sh
      WARNING: Your kernel does not support swap limit capabilities or the cgroup is not mounted. Memory limited without swap.
      c58051baa911b68469146c5090851613692538d6532335042d7e74602592820c
      root@duke-2:~# ls

    5. 分配容器ip

      root@duke-2:/data/caffe_data# weave attach 192.168.0.2/24 ufo
      192.168.0.2


      【备注】:
      1、Weave的 weave launch操作,只需要在每台机器上执行一次,后续的容器创建只需要添加容器的ip即可
      2、如果遇到下面问题时,关闭防火墙即可,如centos7的默认防火墙关闭命令是
      systemctl stop firewalld.service #停止firewall
      systemctl disable firewalld.service #禁止firewall开机启动

      问题形式如下:
      [root@localhost ~]# weave launch --ipalloc-range 10.2.0.0/16
      WARNING: existing iptables rule
      '-A FORWARD -j REJECT --reject-with icmp-host-prohibited'
      will block name resolution via weaveDNS - please reconfigure your firewall.
      517f07cdb502824ad7a7bec5532dd37984b19c49cbab4ce4c8130c4f6e9d7d0c

    15.2 容器自启动

    15.2.1 Restart policy关键字说明

    restart policy在使用docker run启动容器时通过--restart标志指定,这个标志有多个value可选,不同的value有不同的行为,如下表所列:

    关键字
    描述
    no 不自动重启容器. (默认value)
    on-failure 容器发生error而退出(容器退出状态不为0)重启容器
    unless-stopped 在容器已经stop掉或Docker stoped/restarted的时候才重启容器
    always 在容器已经stop掉或Docker stoped/restarted的时候才重启容器

    举个例子:下面的命令启动一个Redis容器,当Redis容器停止后或者Docker被重启时,Redis容器都会重启。

    [root@localhost ~]# docker run -dit --restart=unless-stopped redis

    15.2.1 Restart policy细节

    使用restart policies时需要注意如下细节:

    1. 容器只有在成功启动后restart policy才能生效。这里的"成功启动"是指容器处于up至少10秒且已经处于docker监管。这是避免没有成功启动的容器陷入restart的死循环。
    2. 如果手动(manually)的stop(与前面的explicitly stopped有何区别)一个容器,容器设置的restart policy将会被忽略,除非Docker daemon重启或者容器手动重启。这是避免了另外一种死循环。
    3. restart policies只能用于容器,对于swarm services其restart policies有不通过的配置。

    15.2.2 Docker容器开机自动启动

    在使用docker run启动容器时,使用--restart参数来设置:

    root@duke:~/docker$ docker run --restart=unless-stopped -p 30001:22 --name metis_test --hostname metis -v /home/dilu/metis_docker:/work -d ubuntu18.04-ssh:0.1 /run.sh

    还可以在使用on - failure策略时,指定Docker将尝试重新启动容器的最大次数。默认情况下,Docker将尝试永远重新启动容器。

    root@duke:~/docker$ docker run --restart=on-failure:10 redis

    十六、 当docker启动不了解决方法

    直接删除/var/lib/docker下的内容,就相当于将docker重置,当然images下的可以不删除

    root@duke:~/docker# rm -rf /var/lib/docker/*

  • 相关阅读:
    keras:InternalError: Failed to create session
    centos 常用命令
    centos7 安装gdal2.3.1
    centos mysql初探 -- 配置、基本操作及问题
    machine learning 之 Recommender Systems
    machine learning 之 Anomaly detection
    centos R包 tidyverse安装
    centos 问题解决记录
    R python在无图形用户界面时保存图片
    隐私政策
  • 原文地址:https://www.cnblogs.com/hzw97/p/11736524.html
Copyright © 2020-2023  润新知