【0730】docker 入門

25.1 docker簡介

  • 官網 www.docker.com

  • github  https://github.com/docker/docker.github.io

  • 開源的容器引擎,可以讓開發者打包應用以及依賴的庫,然後發佈到任何流行的linux發行版上,移植很方便

  • 由 go 語言編寫,基於 apache2.0 協議發佈

  • 基於 linux kernel,要想在 win 下運行需要藉助一個 vm(虛擬機)來實現

  • 自 2013 年開始,近些年發展迅猛

  • docker 從1.13x開始,版本分爲社區版 ce 和企業版 ee,並且基於年月的時間線形式,當前最新穩定版爲 17.09 參考http://blog.csdn.net/chenhaifeng2016/article/details/6806241

Docker和傳統的虛擬化比較:

1212.png

Docker的優勢:

  • 啓動非常快,秒級實現

  • 資源利用率高,一臺高配置服務器可以跑上千個docker容器

  • 更快的交付和部署,一次創建和配置後,可以在任意地方運行

  • 內核級別的虛擬化,不需要額外的hypevisor支持,會有更高的性能和效率

  • 易遷移,平臺依賴性不強

12133.png

Docker核心概念:

  • 鏡像,是一個只讀的模板,類似於安裝系統用到的那個iso文件,我們通過鏡像來完成各種應用的部署。

  • 容器,鏡像類似於操作系統,而容器類似於虛擬機本身。它可以被啓動、開始、停止、刪除等操作,每個容器都是相互隔離的。

  • 倉庫,存放鏡像的一個場所,倉庫分爲公開倉庫和私有倉庫。 最大的公開倉庫是Docker hub(hub.docker.com),國內公開倉庫(dockerpool.com)


25.2 安裝docker

1、下載 yum 源,並安裝

[root@arslinux-01 ~]# curl https://download.docker.com/linux/centos/docker-ce.repo -o  /etc/yum.repos.d/docker.repo
[root@arslinux-01 ~]# yum install -y docker-ce

速度比較慢,也可以直接下載rpm包

https://download.docker.com/linux/centos/7/x86_64/stable/Packages/

上傳到 linux 下並 yum 安裝,可以自動解決依賴 yum install -y docker-ce-xxxx.rpm

2、啓動 docker

[root@arslinux-01 ~]# systemctl start docker


25.3 鏡像管理

  • 下載 centos 鏡像

[root@arslinux-01 ~]# docker pull centos

速度比較慢,可以使用 docker 加速器

  • 配置 docker 加速器

[root@arslinux-01 ~]# vim /etc/docker/daemon.json
{
"registry-mirrors": ["https://dhq9bx4f.mirror.aliyuncs.com"]
}

說明:這個url爲加速器地址,需要去ali雲申請

配置完加速器,重啓 docker,再次 docker pull centos 會快很多

  • docker image              查看本地鏡像

  • docker search xxx          搜索鏡像,其中 xxx 爲關鍵詞

  • docker tag 鏡像名 標籤      給鏡像打標籤(就是更改鏡像名稱)

[root@arslinux-01 ~]# docker search centos
NAME                               DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
centos                             The official build of CentOS.                   5493                [OK]
ansible/centos7-ansible            Ansible on Centos7                              122                                     [OK]
jdeathe/centos-ssh                 CentOS-6 6.10 x86_64 / CentOS-7 7.6.1810 x86…   111                                     [OK]
consol/centos-xfce-vnc             Centos container with "headless" VNC session…   96                                      [OK]
centos/mysql-57-centos7            MySQL 5.7 SQL database server                   59
imagine10255/centos6-lnmp-php56    centos6-lnmp-php56                              57                                      [OK]
tutum/centos                       Simple CentOS docker image with SSH access      44
centos/postgresql-96-centos7       PostgreSQL is an advanced Object-Relational …   39
kinogmt/centos-ssh                 CentOS with SSH                                 28                                      [OK]
pivotaldata/centos-gpdb-dev        CentOS image for GPDB development. Tag names…   10
guyton/centos6                     From official centos6 container with full up…   9                                       [OK]
drecom/centos-ruby                 centos ruby                                     6                                       [OK]
pivotaldata/centos                 Base centos, freshened up a little with a Do…   3
mamohr/centos-java                 Oracle Java 8 Docker image based on Centos 7    3                                       [OK]
darksheer/centos                   Base Centos Image -- Updated hourly             3                                       [OK]
pivotaldata/centos-mingw           Using the mingw toolchain to cross-compile t…   2
pivotaldata/centos-gcc-toolchain   CentOS with a toolchain, but unaffiliated wi…   2
miko2u/centos6                     CentOS6 日本語環境                                   2                                       [OK]
ovirtguestagent/centos7-atomic     The oVirt Guest Agent for Centos 7 Atomic Ho…   2
indigo/centos-maven                Vanilla CentOS 7 with Oracle Java Developmen…   1                                       [OK]
blacklabelops/centos               CentOS Base Image! Built and Updates Daily!     1                                       [OK]
mcnaughton/centos-base             centos base image                               1                                       [OK]
pivotaldata/centos7-dev            CentosOS 7 image for GPDB development           0
smartentry/centos                  centos with smartentry                          0                                       [OK]
pivotaldata/centos6.8-dev          CentosOS 6.8 image for GPDB development         0
[root@arslinux-01 ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              latest              3556258649b2        2 weeks ago         64.2MB
arslinux            latest              9f38484d220f        4 months ago        202MB
centos              latest              9f38484d220f        4 months ago        202MB
  • 如果想更改上方 TAG 處信息,可以用 : 將 REPOSITORY : TAG 分開

[root@arslinux-01 ~]# docker tag centos test111:190808
[root@arslinux-01 ~]# docker tag centos test111:1234
[root@arslinux-01 ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              latest              3556258649b2        2 weeks ago         64.2MB
arslinux            latest              9f38484d220f        4 months ago        202MB
centos              latest              9f38484d220f        4 months ago        202MB
test111             1234                9f38484d220f        4 months ago        202MB
test111             190808              9f38484d220f        4 months ago        202MB

這裏的 TAG 和命令中的 tag 不是一個概念

  • docker run -itd 鏡像名          把鏡像啓動爲容器

  • docker ps              查看運行的容器

  • docker ps -a          查看所有容器(包括未運行的)

[root@arslinux-01 ~]# docker run -itd centos
9d06f43b687172d3cd5ab13529645482f2a9d6fad286c0dd82a5783863db2617
[root@arslinux-01 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
9d06f43b6871        centos              "/bin/bash"         5 seconds ago       Up 3 seconds                            zealous_mestorf
[root@arslinux-01 ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
9d06f43b6871        centos              "/bin/bash"         3 minutes ago       Up 3 minutes                            zealous_mestorf

說明:-i 表示讓容器的標準輸入打開,-t 表示分配一個僞終端,-d 表示後臺啓動,要把 -i -t -d 放到鏡像名字前面

  • docker rmi 鏡像名          刪除指定鏡像

[root@arslinux-01 ~]# docker rmi test111
Error: No such image: test111
[root@arslinux-01 ~]# docker rmi test111:1234
Untagged: test111:1234
[root@arslinux-01 ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              latest              3556258649b2        2 weeks ago         64.2MB
arslinux            latest              9f38484d220f        4 months ago        202MB
centos              latest              9f38484d220f        4 months ago        202MB
test111             190808              9f38484d220f        4 months ago        202MB

其中後面的參數可以是 tag,如果是 tag 時,實際上是刪除該 tag。當後面的參數爲鏡像 ID 時,則會徹底刪除整個鏡像,所有標籤也會一同刪除

不加 TAG 刪除鏡像,實際 TAG 就是 latest ,docker rmi test111 就是 docker rmi test111:latest


25.4 通過容器創建鏡像

1、啓動容器

[root@arslinux-01 ~]# docker run -itd centos
77fbe7aeaee649478aae095af386e6b7796e96e865bb95f7d673875fdf6767ec
[root@arslinux-01 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
77fbe7aeaee6        centos              "/bin/bash"         15 seconds ago      Up 12 seconds                           blissful_yonath

2、進入容器    docker exec -it xxxxx  bash 

[root@arslinux-01 ~]# docker exec -it 77fbe7 bash
[root@77fbe7aeaee6 /]# df -h
Filesystem      Size  Used Avail Use% Mounted on
overlay          28G  6.1G   22G  22% /
tmpfs            64M     0   64M   0% /dev
tmpfs           487M     0  487M   0% /sys/fs/cgroup
shm              64M     0   64M   0% /dev/shm
/dev/sda3        28G  6.1G   22G  22% /etc/hosts
tmpfs           487M     0  487M   0% /proc/asound
tmpfs           487M     0  487M   0% /proc/acpi
tmpfs           487M     0  487M   0% /proc/scsi
tmpfs           487M     0  487M   0% /sys/firmware
[root@77fbe7aeaee6 /]# free
total        used        free      shared  buff/cache   available
Mem:         995896      654636       73140        2620      268120      133448
Swap:       1999868      169472     1830396

說明:其中xxxxx爲容器id,這個id可以用docker ps查看,最後面的bash爲進入容器後我們要執行的命令,這樣就可以打開一個終端

進入到該容器中,我們做一些變更,比如安裝一些東西,然後針對這個容器進行創建新的鏡像

3、容器中的操作和機器中操作沒有區別

[root@77fbe7aeaee6 /]# yum install -y net-tools
[root@77fbe7aeaee6 /]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
    inet 172.17.0.2  netmask 255.255.0.0  broadcast 172.17.255.255
    ether 02:42:ac:11:00:02  txqueuelen 0  (Ethernet)
    RX packets 3381  bytes 14925255 (14.2 MiB)
    RX errors 0  dropped 0  overruns 0  frame 0
    TX packets 2448  bytes 135606 (132.4 KiB)
    TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
    
lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
    inet 127.0.0.1  netmask 255.0.0.0
    loop  txqueuelen 1000  (Local Loopback)
    RX packets 0  bytes 0 (0.0 B)
    RX errors 0  dropped 0  overruns 0  frame 0
    TX packets 0  bytes 0 (0.0 B)
    TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
    
[root@77fbe7aeaee6 /]# exit
[root@arslinux-01 ~]# ifconfig |grep docker
docker0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
    inet 172.17.0.1  netmask 255.255.0.0  broadcast 172.17.255.255
    inet6 fe80::42:c8ff:fe99:eec1  prefixlen 64  scopeid 0x20<link>
    ether 02:42:c8:99:ee:c1  txqueuelen 0  (Ethernet)
    RX packets 2448  bytes 101334 (98.9 KiB)
    RX errors 0  dropped 0  overruns 0  frame 0
    TX packets 3373  bytes 14924599 (14.2 MiB)
    TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

容器中的網卡在宿主機上也出現了

每開啓一個容器,都會有一個虛擬網卡讓容器和虛擬機通信

[root@arslinux-01 ~]# ifconfig |grep veth
veth47c82bd: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
    inet6 fe80::5014:d2ff:fe64:e486  prefixlen 64  scopeid 0x20<link>
    ether 52:14:d2:64:e4:86  txqueuelen 0  (Ethernet)
    RX packets 2448  bytes 135606 (132.4 KiB)
    RX errors 0  dropped 0  overruns 0  frame 0
    TX packets 3381  bytes 14925255 (14.2 MiB)
    TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

4、將容器做成鏡像 docker commit -m "具體操作" -a "操作人" 容器id 新鏡像名稱

[root@arslinux-01 ~]# docker commit -m "install net-tools" -a "arslinux" 77fbe7aeaee6 centos_with_net
sha256:9c213599fd8f289da3333e0a0ddb55d8f208c020e3ace833461d2f0706cdd4d6
[root@arslinux-01 ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos_with_net     latest              9c213599fd8f        4 seconds ago       314MB
centos              latest              8f92b9698cb1        3 minutes ago       314MB
ubuntu              latest              3556258649b2        2 weeks ago         64.2MB
arslinux            latest              9f38484d220f        4 months ago        202MB
test111             190808              9f38484d220f        4 months ago        202MB

說明:-m 加一些改動信息,-a 指定作者相關信息  77fbe7aeaee6 這一串爲容器id,再後面爲新鏡像的名字

5、打開新鏡像的容器並進入

[root@arslinux-01 ~]# docker run -itd centos_with_net
b1778f203633755d801b022e6db3c07d781f010acf4681cfe5f86b7639b18d6f
[root@arslinux-01 ~]# docker exec -it b1778f bash
[root@b1778f203633 /]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
    inet 172.17.0.3  netmask 255.255.0.0  broadcast 172.17.255.255
    ether 02:42:ac:11:00:03  txqueuelen 0  (Ethernet)
    RX packets 8  bytes 656 (656.0 B)
    RX errors 0  dropped 0  overruns 0  frame 0
    TX packets 0  bytes 0 (0.0 B)
    TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
    inet 127.0.0.1  netmask 255.0.0.0
    loop  txqueuelen 1000  (Local Loopback)
    RX packets 0  bytes 0 (0.0 B)
    RX errors 0  dropped 0  overruns 0  frame 0
    TX packets 0  bytes 0 (0.0 B)
    TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
    
[root@b1778f203633 /]# ping www.baidu.com
PING www.a.shifen.com (180.97.33.107) 56(84) bytes of data.
64 bytes from 180.97.33.107 (180.97.33.107): icmp_seq=5 ttl=127 time=6.15 ms
64 bytes from 180.97.33.107 (180.97.33.107): icmp_seq=6 ttl=127 time=6.53 ms
^C
--- www.a.shifen.com ping statistics ---
6 packets transmitted, 6 received, 0% packet loss, time 14037ms
rtt min/avg/max/mdev = 5.900/6.595/7.355/0.532 ms


25.5 通過模板創建鏡像

1、下載模板 http://download.openvz.org/template/precreated/

本地下完後上傳到虛擬機中

2、cat tar包 | docker import - 鏡像名(自定義)      導入鏡像

[root@arslinux-01 ~]# cat centos-6-x86-minimal.tar.gz |docker import - centos6
sha256:d0fc4ef11ea3c1cd7039fb595bdb51023d1f01af68d7c58c45b201000e12306e
[root@arslinux-01 ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos6             latest              d0fc4ef11ea3        9 seconds ago       512MB
centos_with_net     latest              9c213599fd8f        53 minutes ago      314MB
centos              latest              8f92b9698cb1        56 minutes ago      314MB
ubuntu              latest              3556258649b2        2 weeks ago         64.2MB
arslinux            latest              9f38484d220f        4 months ago        202MB
test111             190808              9f38484d220f        4 months ago        202MB

3、啓動容器,進入

[root@arslinux-01 ~]# docker run -itd centos6
ced4739b62045d77aa6983589a1e43edeacad2b8ec979b77a47b901131cd637
[root@arslinux-01 ~]# docker exec -it ced4739b62 bash
[root@ced4739b6204 /]# cat /etc/issue
CentOS release 6.8 (Final)
Kernel \r on an \m
[root@ced4739b6204 /]# uname -a
Linux ced4739b6204 3.10.0-957.el7.x86_64 #1 SMP Thu Nov 8 23:39:32 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux

docker 用的是虛擬機 linux 的內核,哪怕是 centos6 的docker ,用的依然是虛擬機 centos7 的內核

4、docker save -o 保存的文件名 鏡像名 導出鏡像

[root@arslinux-01 ~]# docker save -o centos7_with_nettool.tar centos_with_net
[root@arslinux-01 ~]# du -sh centos7_with_nettool.tar
307Mcento7_with_nettool.tar

5、先刪除鏡像

[root@arslinux-01 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
ced4739b6204        centos6             "bash"              11 minutes ago      Up 11 minutes                           suspicious_perlman
b1778f203633        centos_with_net     "/bin/bash"         About an hour ago   Up About an hour                        dazzling_bell
77fbe7aeaee6        9f38484d220f        "/bin/bash"         2 hours ago         Up 2 hours                              blissful_yonath
[root@arslinux-01 ~]# docker rm -f b1778f203633
b1778f203633
[root@arslinux-01 ~]# docker rmi 9c213599fd8f
Untagged: centos_with_net:latest
Deleted: sha256:9c213599fd8f289da3333e0a0ddb55d8f208c020e3ace833461d2f0706cdd4d6
[root@arslinux-01 ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos6             latest              d0fc4ef11ea3        12 minutes ago      512MB
centos              latest              8f92b9698cb1        About an hour ago   314MB
ubuntu              latest              3556258649b2        2 weeks ago         64.2MB
arslinux            latest              9f38484d220f        4 months ago        202MB
test111             190808              9f38484d220f        4 months ago        202MB

6、再用 docker load 載入

  • docker load imput 鏡像包

  • docker load < 鏡像包

以上兩種命令皆可

[root@arslinux-01 ~]# docker load < centos7_with_nettool.tar
Loaded image: centos_with_net:latest
[root@arslinux-01 ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos6             latest              d0fc4ef11ea3        16 minutes ago      512MB
centos_with_net     latest              9c213599fd8f        About an hour ago   314MB
centos              latest              8f92b9698cb1        About an hour ago   314MB
ubuntu              latest              3556258649b2        2 weeks ago         64.2MB
arslinux            latest              9f38484d220f        4 months ago        202MB
test111             190808              9f38484d220f        4 months ago        202MB


25.6 容器管理

  • docker create -it 鏡像名 bash      創建容器

  • docker start 容器id      啓動容器

[root@arslinux-01 ~]# docker create -it centos6 bash
03cea99df28677d8b4ade4e1d729d14b3c0d4c8d183cc9e76f239456ed878534
[root@arslinux-01 ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                    PORTS               NAMES
03cea99df286        centos6             "bash"              8 seconds ago       Created                                       hopeful_darwin
ced4739b6204        centos6             "bash"              37 minutes ago      Up 37 minutes                                 suspicious_perlman
77fbe7aeaee6        9f38484d220f        "/bin/bash"         2 hours ago         Up 2 hours                                    blissful_yonath
9d06f43b6871        9f38484d220f        "/bin/bash"         2 days ago          Exited (137) 2 days ago                       zealous_mestorf
[root@arslinux-01 ~]# docker start 03cea99df286
03cea99df286
[root@arslinux-01 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
03cea99df286        centos6             "bash"              29 seconds ago      Up 5 seconds                            hopeful_darwin
ced4739b6204        centos6             "bash"              37 minutes ago      Up 37 minutes                           suspicious_perlman
77fbe7aeaee6        9f38484d220f        "/bin/bash"         2 hours ago         Up 2 hours                              blissful_yonath

啓動容器後,可以使用 docker ps  查看到,有 start 就有 stop,和 restart

docker create 再 docker start 相當於 docker run

  • docker run -it 鏡像名 bash     啓動並進入容器

[root@arslinux-01 ~]# docker run -it centos bash
  • docker run -d   可以讓容器在後臺運行

例如:docker run -d centos bash -c "while :; do echo "123"; sleep 2; done"

  • docker run --name 名稱 -itd 鏡像 bash      給容器自定義名字

[root@arslinux-01 ~]# docker run --name arsenal_pepe -itd centos6 bash
6b975789ec240520e4316699cfb83a9752a84da8890f62b18085ce759d252152
[root@arslinux-01 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
6b975789ec24        centos6             "bash"              22 seconds ago      Up 21 seconds                           arsenal_pepe
03cea99df286        centos6             "bash"              27 minutes ago      Up 27 minutes                           hopeful_darwin
ced4739b6204        centos6             "bash"              About an hour ago   Up About an hour                        suspicious_perlman
77fbe7aeaee6        centos              "/bin/bash"         2 hours ago         Up 2 hours                              blissful_yonath
[root@arslinux-01 ~]# docker exec -it arsenal_pepe bash
[root@6b975789ec24 /]#
  • docker run --rm -it 容器名 bash -c "命令"      命令執行完容器就會退出

[root@arslinux-01 ~]# docker run --rm -it centos bash -c "sleep 15"

--rm 可以讓容器退出後直接刪除,在這裏命令執行完容器就會退出

  • docker logs 容器id      查看容器運行歷史信息

[root@arslinux-01 ~]# docker run -itd centos bash -c "echo 123"
066f53f67367fe0da907c26d7abd2d0c95acff40eaf2a0203fba556787f4ab83
[root@arslinux-01 ~]# docker logs 066f53f67367
123
  • docker attach 容器id      進入後臺運行的容器

  • docker exec -it 容器id bash      臨時打開一個虛擬終端,exit 後,容器依然運行着

attach命令不算好用,比如我們想要退出終端,就得exit了,這樣容器也就退出了,用 exec 比較

  • docker rm 容器id      刪除容器

[root@arslinux-01 ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS                        PORTS               NAMES
066f53f67367        centos              "bash -c 'echo 123'"   5 minutes ago       Exited (0) 5 minutes ago                          sharp_liskov
6b975789ec24        centos6             "bash"                 18 minutes ago      Up 18 minutes                                     arsenal_pepe
944bc4839130        centos              "bash"                 30 minutes ago      Exited (127) 30 minutes ago                       nervous_sammet
37366b75bd90        centos              "/bin/bash"            30 minutes ago      Exited (127) 30 minutes ago                       inspiring_saha
a80eed529a34        centos              "bash"                 31 minutes ago      Exited (0) 30 minutes ago                         hungry_elgamal
32da41e087c2        centos              "bash"                 32 minutes ago      Exited (0) 32 minutes ago                         infallible_tereshkova
985de0f4f889        centos              "bash"                 33 minutes ago      Exited (0) 33 minutes ago                         nifty_nightingale
d55e81eae6aa        centos              "/bin/bash"            35 minutes ago      Exited (0) 33 minutes ago                         condescending_varahamihira
02bccad25efb        centos              "/bin/bash"            38 minutes ago      Exited (0) 37 minutes ago                         heuristic_wu
03cea99df286        centos6             "bash"                 46 minutes ago      Up 45 minutes                                     hopeful_darwin
ced4739b6204        centos6             "bash"                 About an hour ago   Up About an hour                                  suspicious_perlman
77fbe7aeaee6        centos              "/bin/bash"            3 hours ago         Up 3 hours                                        blissful_yonath
9d06f43b6871        centos              "/bin/bash"            2 days ago          Exited (137) 2 days ago                           zealous_mestorf
[root@arslinux-01 ~]# docker rm 066f53f67367
066f53f67367
[root@arslinux-01 ~]# docker rm 944bc4839130
944bc4839130
[root@arslinux-01 ~]# docker rm 37366b75bd90
37366b75bd90
[root@arslinux-01 ~]# docker rm a80eed529a34
a80eed529a34
[root@arslinux-01 ~]# docker rm 32da41e087c2
32da41e087c2
[root@arslinux-01 ~]# docker rm 985de0f4f889
985de0f4f889
[root@arslinux-01 ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
6b975789ec24        centos6             "bash"              19 minutes ago      Up 19 minutes                                   arsenal_pepe
d55e81eae6aa        centos              "/bin/bash"         36 minutes ago      Exited (0) 34 minutes ago                       condescending_varahamihira
02bccad25efb        centos              "/bin/bash"         40 minutes ago      Exited (0) 38 minutes ago                       heuristic_wu
03cea99df286        centos6             "bash"              47 minutes ago      Up 46 minutes                                   hopeful_darwin
ced4739b6204        centos6             "bash"              About an hour ago   Up About an hour                                suspicious_perlman
77fbe7aeaee6        centos              "/bin/bash"         3 hours ago         Up 3 hours                                      blissful_yonath
9d06f43b6871        centos              "/bin/bash"         2 days ago          Exited (137) 2 days ago                         zealous_mestor

——對於啓動狀態的容器,刪除需要加 -f 強行刪除

[root@arslinux-01 ~]# docker rm 03cea99df286
Error response from daemon: You cannot remove a running container 03cea99df28677d8b4ade4e1d729d14b3c0d4c8d183cc9e76f239456ed878534. Stop the container before attempting removal or force remove
[root@arslinux-01 ~]# docker rm -f 03cea99df286
03cea99df286
[root@arslinux-01 ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
6b975789ec24        centos6             "bash"              21 minutes ago      Up 21 minutes                                   arsenal_pepe
d55e81eae6aa        centos              "/bin/bash"         38 minutes ago      Exited (0) 36 minutes ago                       condescending_varahamihira
02bccad25efb        centos              "/bin/bash"         41 minutes ago      Exited (0) 40 minutes ago                       heuristic_wu
ced4739b6204        centos6             "bash"              About an hour ago   Up About an hour                                suspicious_perlman
77fbe7aeaee6        centos              "/bin/bash"         3 hours ago         Up 3 hours                                      blissful_yonath
9d06f43b6871        centos              "/bin/bash"         2 days ago          Exited (137) 2 days ago                         zealous_mestor
  • docker export 容器id > 文件.tar      導出容器

  • cat 文件.tar |docker import - 鏡像名      生成鏡像

[root@arslinux-01 ~]# docker export 9d06f43b6871 > centos.tar

[root@arslinux-01 ~]# cat centos.tar |docker import - centos111

sha256:48f71221434353e6f02227794e1a95fcc8ae5f0dc9608142febc957dd124dba0


25.7 倉庫管理

  • docker start          啓動未啓動的容器(重啓docker服務後,容器會退出)

[root@arslinux-01 ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
6b975789ec24        centos6             "bash"              27 hours ago        Exited (137) 22 hours ago                       arsenal_pepe
d55e81eae6aa        centos              "/bin/bash"         28 hours ago        Exited (0) 28 hours ago                         condescending_varahamihira
02bccad25efb        centos              "/bin/bash"         28 hours ago        Exited (0) 28 hours ago                         heuristic_wu
ced4739b6204        centos6             "bash"              29 hours ago        Exited (137) 22 hours ago                       suspicious_perlman
77fbe7aeaee6        centos              "/bin/bash"         30 hours ago        Exited (137) 22 hours ago                       blissful_yonath
9d06f43b6871        centos              "/bin/bash"         3 days ago          Exited (137) 3 days ago                         zealous_mestorf
[root@arslinux-01 ~]# docker start 6b975789ec24
6b975789ec24
[root@arslinux-01 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
6b975789ec24        centos6             "bash"              27 hours ago        Up 1 second                             arsenal_pepe

1、利用 registry 創建私有倉庫

1)下載 registry 鏡像

[root@arslinux-01 ~]# docker pull registry

2)啓動 registry 容器

  • docker run -d -p 宿主機端口號:容器端口號 registry      啓動 registry 容器

[root@arslinux-01 ~]# docker run -d -p 5000:5000 registry
3f51ff27501d3f81a29996cc04b8a16ae9b85a9c5b03f4876d13f16fc53e2315
[root@arslinux-01 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
3f51ff27501d        registry            "/entrypoint.sh /etc…"   9 seconds ago       Up 7 seconds        0.0.0.0:5000->5000/tcp   quizzical_chandrasekhar
6b975789ec24        centos6             "bash"                   28 hours ago        Up 14 minutes                                arsenal_pepe

說明:以registry鏡像啓動容器,-p會把容器的端口映射到宿主機上,:左邊爲宿主機監聽端口,:右邊爲容器監聽端口

3)查看 registry 倉庫內容

  • curl 127.0.0.1:5000/v2/_catalog     查看 registry 倉庫內容

[root@arslinux-01 ~]# curl 127.0.0.1:5000/v2/_catalog
{"repositories":[]}

2、上傳鏡像到私有倉庫

1)標記鏡像

  • docker tag 要標記的鏡像 ip:端口/鏡像      標記tag,必須帶私有倉庫 ip:端口

[root@arslinux-01 ~]# docker tag centos6 192.168.194.130:5000/centos6
[root@arslinux-01 ~]# docker images
REPOSITORY                     TAG                 IMAGE ID            CREATED             SIZE
192.168.194.130:5000/centos6   latest              d0fc4ef11ea3        29 hours ago        512MB
centos6                        latest              d0fc4ef11ea3        29 hours ago        512MB
centos_with_net                latest              9c213599fd8f        30 hours ago        314MB
ubuntu                         latest              3556258649b2        2 weeks ago         64.2MB
arslinux                       latest              9f38484d220f        5 months ago        202MB
centos                         latest              9f38484d220f        5 months ago        202MB
test111                        190808              9f38484d220f        5 months ago        202MB
registry                       latest              f32a97de94e1        5 months ago        25.8MB

2)上傳鏡像

  • docker push ip:端口/鏡像      上傳鏡像到 registry

[root@arslinux-01 ~]# docker push 192.168.194.130:5000/centos6
The push refers to repository [192.168.194.130:5000/centos6]
Get https://192.168.194.130:5000/v2/: http: server gave HTTP response to HTTPS client

3)指定私有倉庫地址

以上報錯需要修改配置文件 /etc/docker/daemon.json,在 { } 中加一行內容(如果原本有內容需用逗號 , 隔開)

[root@arslinux-01 ~]# vim /etc/docker/daemon.json
{ "insecure-registries":["192.168.194.130:5000"] }

4)重啓 docker、鏡像

[root@arslinux-01 ~]# systemctl restart docker
[root@arslinux-01 ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                       PORTS               NAMES
3f51ff27501d        registry            "/entrypoint.sh /etc…"   31 minutes ago      Exited (2) 2 minutes ago                         quizzical_chandrasekhar
6b975789ec24        centos6             "bash"                   28 hours ago        Exited (137) 4 minutes ago                       arsenal_pepe
d55e81eae6aa        centos              "/bin/bash"              28 hours ago        Exited (0) 28 hours ago                          condescending_varahamihira
02bccad25efb        centos              "/bin/bash"              29 hours ago        Exited (0) 29 hours ago                          heuristic_wu
ced4739b6204        centos6             "bash"                   29 hours ago        Exited (137) 23 hours ago                        suspicious_perlman
77fbe7aeaee6        centos              "/bin/bash"              31 hours ago        Exited (137) 23 hours ago                        blissful_yonath
9d06f43b6871        centos              "/bin/bash"              4 days ago          Exited (137) 4 days ago                          zealous_mestorf
[root@arslinux-01 ~]# docker start 3f51ff27501d
3f51ff27501d
[root@arslinux-01 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
3f51ff27501d        registry            "/entrypoint.sh /etc…"   31 minutes ago      Up 3 seconds        0.0.0.0:5000->5000/tcp   quizzical_chandrasekhar

5)重新上傳

[root@arslinux-01 ~]# docker push 192.168.194.130:5000/centos6
The push refers to repository [192.168.194.130:5000/centos6]
0a2f11f7b1ef: Pushed
latest: digest: sha256:5c0d4992624653abc8764aec2409903be43105246fffd961fa69c3adbc78f0be size: 529

6)查看倉庫內容 curl

[root@arslinux-01 ~]# curl 192.168.194.130:5000/v2/_catalog
{"repositories":["centos6"]}

7)再用以上方法上傳 ubuntu

[root@arslinux-01 ~]# docker tag ubuntu 192.168.194.130:5000/ubuntu
[root@arslinux-01 ~]# docker images
REPOSITORY                     TAG                 IMAGE ID            CREATED             SIZE
192.168.194.130:5000/centos6   latest              d0fc4ef11ea3        29 hours ago        512MB
centos6                        latest              d0fc4ef11ea3        29 hours ago        512MB
centos_with_net                latest              9c213599fd8f        30 hours ago        314MB
192.168.194.130:5000/ubuntu    latest              3556258649b2        2 weeks ago         64.2MB
ubuntu                         latest              3556258649b2        2 weeks ago         64.2MB
centos                         latest              9f38484d220f        5 months ago        202MB
test111                        190808              9f38484d220f        5 months ago        202MB
arslinux                       latest              9f38484d220f        5 months ago        202MB
registry                       latest              f32a97de94e1        5 months ago        25.8MB
[root@arslinux-01 ~]# docker push 192.168.194.130:5000/ubuntu
The push refers to repository [192.168.194.130:5000/ubuntu]
b079b3fa8d1b: Pushed
a31dbd3063d7: Pushed
c56e09e1bd18: Pushed
543791078bdb: Pushed
latest: digest: sha256:d91842ef309155b85a9e5c59566719308fab816b40d376809c39cf1cf4de3c6a size: 1152
[root@arslinux-01 ~]# curl 192.168.194.130:5000/v2/_catalog
{"repositories":["centos6","ubuntu"]}


未完待續

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章