docker在Ubuntu下1小時快速學習

前言

因爲工做緣由,不少狀況下須要快速學習新的知識,針對docker若是從頭至尾看相關書籍學習會很是慢,因此整理了下docker的經常使用操做,只要跟着本文學習操做,一小時就能掌握docker大部分最經常使用操做方法,也能夠當作工具手冊隨時查找學習,固然本文未涉及的部分,仍是須要經過閱讀書籍學習,這文章的目的是幫助須要快速上手應用的人。因爲寫該文章的時候還比較早,因此所用系統和docker版本比較早,可是基本上其餘版本操做基本一致,就不在從新更換版本從新編寫。php

1、 Ubuntu 14.0.4系統安裝docker

1.1 在線安裝docker

如下操做步驟均在root用戶下操做node

序列
操做步驟
詳細說明
1 檢查內核是否符合要求 Docker 要求 Ubuntu 系統的內核版本高於 3.10 ,建議在 Ubuntu14.04 版本

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

2 安裝docker

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

root@duke:~# curl -sSL 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 "download.docker.com/linux/ubunt…" | apt-key add -qq - >/dev/null
+ sh -c echo "deb [arch=amd64] download.docker.com/linux/ubunt… 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 docs.docker.com/engine/secu…
for more information.
nginx

3 啓動docker 後臺服務

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

4 測試運行hello-world

root@duke: docker run hello-worldgithub

【注意】:若是在sh -c apt-get install -y -qq --no-install-recommends docker-ce >/dev/null這步失敗時,報如下錯誤:web

E: 沒法下載 download.docker.com/linux/ubunt… 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目錄便可。redis

1.2 修改docker默認存儲路徑

Docker使用會佔用大量的磁盤空間。默認的存儲路徑是/var/lib/docker/,通常狀況下/var/lib/路徑是系統默認磁盤容量空間不會很大, 因此會形成docker佔用磁盤滿的問題產生。因此在安裝好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: 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: index.docker.io/v1/
    Experimental: false
    Insecure Registries:
    127.0.0.0/8
    Live Restore Enabled: false

    WARNING: No swap limit support

2、 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
    

3、 鏡像

如下操做步驟均在root用戶下操做

3.1 鏡像查找

  1. 鏡像能夠本身作,也能夠從官方直接下載,在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、 容器

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 

5、倉庫

前面章節下載的鏡像都來自於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。
【修改方法】:
一、在docker1.12.3之前的版本,修改docker的配置文件/etc/systemconfig/docker,重啓docker來解決這個問題。
二、在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

6、建立鏡像--開機啓動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 security.ubuntu.com trusty-security InRelease [65.9 kB]
Ign archive.ubuntu.com trusty InRelease
Get:2 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: 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

7、 存出鏡像

如下操做步驟均在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

8、 載入鏡像

如下操做步驟均在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

9、 docker擁有root權限

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

privileged 參數功能,設置爲true的時候,讓docker的root擁有真正root的權限,能夠調用宿主機硬件等,甚至可讓你在docker中使用docker

10、 docker鏡像下載加速

因爲官方鏡像下載很是緩慢,因此須要加速器進行加速下載,這樣的加速器不少,能夠選擇下面加速器進行下載加速:

  1. 登錄到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 get.daocloud.io/daotools/se… | sh -s 8ad3346c.m.daocloud.io
    docker version >= 1.12
    {"registry-mirrors": ["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": ["8ad3346c.m.daocloud.io"],
     "insecure-registries":["10.0.0.76:5000"]
    }

    "registry-mirrors": ["8ad3346c.m.daocloud.io"] 這個就是加速地址
    "insecure-registries":["10.0.0.76:5000"] 這個是本地倉庫地址

11、 支持外部訪問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. 而後生成新的鏡像便可,在生成新的鏡像前,不能關閉容器,不然,系統還原

12、 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 github.com/NVIDIA/nvid…
--2017-11-29 10:48:50-- github.com/NVIDIA/nvid…
正在解析主機 github.com (github.com)... 192.30.255.113, 192.30.255.112
正在鏈接 github.com (github.com)|192.30.255.113|:443... 已鏈接。
已發出 HTTP 請求,正在等待迴應... 302 Found
位置:github-production-release-asset-2e65be.s3.amazonaws.com/45557469/d4… [跟隨至新的 URL]
--2017-11-29 10:48:53-- github-production-release-asset-2e65be.s3.amazonaws.com/45557469/d4…
正在解析主機 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的官網獲取對應版本的容器,地址爲:hub.docker.com/r/nvidia/cu…

十3、 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
    

十4、 安裝weave網絡控制

Weave和flannel同樣是用於控制網絡層軟件,能夠利用該軟件作多機之間的容器跨主機網絡鏈接。
安裝方法:
一、直接在網上下載weave,而後將之放到/usr/local/bin/目錄下,用chmod a+x /usr/local/bin/weave命令給weave附上執行權限,便可完成安裝
二、 當使用weave時,weave會從官網下載docker鏡像建立容器,從而打開應用,此時,能夠將預先下載好的鏡像存出,例如:weaveworks-weave-2.1.3.tar weaveworks-weavedb.tar weaveworks-weaveexec-2.1.3.tar這3個包,而後在安裝的機器上進行docker鏡像存入。
三、 完成上面操做後,啓動weave,docker就會自動建立weave所依賴的容器。

十5、 應用實例

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


    【備註】:
    一、Weave的 weave launch操做,只須要在每臺機器上執行一次,後續的容器建立只須要添加容器的ip便可
    二、若是遇到下面問題時,關閉防火牆便可,如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

十6、 當docker啓動不了解決方法

直接刪除/var/lib/docker下的內容,就至關於將docker重置,固然images下的能夠不刪除

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

相關文章
相關標籤/搜索