1. 程式人生 > >Docker入門二

Docker入門二

-a light contain 為我 命令執行 之前 com creat bash

容器管理

技術分享圖片

1.docker create創建一個容器,但容器並沒啟動,就和我們創建虛擬機一樣,創建了虛擬機後沒啟動

[root@centos-02 ~]# docker create -it centos6 bash
WARNING: IPv4 forwarding is disabled. Networking will not work.
558e31f7b0fb941ca4ee8c1c2b42553b06ac79c0613984b7ad8b9b4ba97f61fd
[root@centos-02 ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
558e31f7b0fb        centos6             "bash"              9 seconds ago       Created
hopeful_murdock 94b9eab05296 centos6 "bash" About an hour ago Up About an hour elegant_minsky 815adfd9da61 centos "/bin/bash" 3 days ago Up 3 days distracted_mahavira [root@centos-02 ~]#

2.啟動容器

[root@centos-02 ~]# docker start 558e31f7b0fb
558e31f7b0fb
[root@centos-02 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
558e31f7b0fb        centos6             "bash"              2 minutes ago       Up
8 seconds hopeful_murdock 94b9eab05296 centos6 "bash" 2 hours ago Up About an hour elegant_minsky 815adfd9da61 centos "/bin/bash" 3 days ago Up 3 days distracted_mahavira [root@centos-02 ~]#

3.不加-d運行容器

[root@centos-02 ~]# docker run -it centos bash
WARNING: IPv4 forwarding is disabled. Networking will not work.
[root@f4eba170402c /]# ls
bin  etc   lib    media  opt   root  sbin  sys  usr
dev  home  lib64  mnt    proc  run   srv   tmp  var

4.我們用ctrl+d退出,然後查看容器發現沒有f4eba170402c,因為我們退出了之前的bash,這是因為我們退出了沒有加-d,加上-a參數查看狀態為Exited,

[root@f4eba170402c /]# exit
[root@centos-02 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
558e31f7b0fb        centos6             "bash"              10 minutes ago      Up 7 minutes                            hopeful_murdock
94b9eab05296        centos6             "bash"              2 hours ago         Up 2 hours                              elegant_minsky
815adfd9da61        centos              "/bin/bash"         3 days ago          Up 3 days                               distracted_mahavira
[root@centos-02 ~]# 
[root@centos-02 ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES
f4eba170402c        centos              "bash"              3 minutes ago       Exited (0) 2 minutes ago                       cocky_bell
558e31f7b0fb        centos6             "bash"              12 minutes ago      Up 9 minutes                                   hopeful_murdock
94b9eab05296        centos6             "bash"              2 hours ago         Up 2 hours                                     elegant_minsky
815adfd9da61        centos              "/bin/bash"         3 days ago          Up 3 days                                      distracted_mahavira
[root@centos-02 ~]# 

5.給容器起個名字

[root@centos-02 ~]# docker run -itd --name centos6_1 centos6 bash
WARNING: IPv4 forwarding is disabled. Networking will not work.
5e32153dbbd6dea32b62291f9aa484b1d82c84d5edf9899ab72a8335f61e16ac
[root@centos-02 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
5e32153dbbd6        centos6             "bash"              15 seconds ago      Up 11 seconds                           centos6_1
558e31f7b0fb        centos6             "bash"              17 minutes ago      Up 14 minutes                           hopeful_murdock
94b9eab05296        centos6             "bash"              2 hours ago         Up 2 hours                              elegant_minsky
815adfd9da61        centos              "/bin/bash"         3 days ago          Up 3 days                               distracted_mahavira
[root@centos-02 ~]# 

6.可以直接用名字進入容器

[root@centos-02 ~]# docker exec -it centos6_1 bash
[root@5e32153dbbd6 /]# 

7.容器執行完直接刪除、命令執行完容器就退出,執行完不留任何痕跡。

[root@centos-02 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
5e32153dbbd6        centos6             "bash"              5 minutes ago       Up 5 minutes                            centos6_1
558e31f7b0fb        centos6             "bash"              22 minutes ago      Up 19 minutes                           hopeful_murdock
94b9eab05296        centos6             "bash"              2 hours ago         Up 2 hours                              elegant_minsky
815adfd9da61        centos              "/bin/bash"         3 days ago          Up 3 days                               distracted_mahavira
[root@centos-02 ~]# docker run --rm -it centos bash -c "sleep 10"
WARNING: IPv4 forwarding is disabled. Networking will not work.
[root@centos-02 ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
5e32153dbbd6        centos6             "bash"              7 minutes ago       Up 6 minutes                                    centos6_1
f4eba170402c        centos              "bash"              15 minutes ago      Exited (0) 13 minutes ago                       cocky_bell
558e31f7b0fb        centos6             "bash"              23 minutes ago      Up 21 minutes                                   hopeful_murdock
94b9eab05296        centos6             "bash"              2 hours ago         Up 2 hours                                      elegant_minsky
815adfd9da61        centos              "/bin/bash"         3 days ago          Up 3 days                                       distracted_mahavira
[root@centos-02 ~]# 

技術分享圖片

1.docker logs查看容器運行歷史信息

[root@centos-02 ~]# docker run -itd centos bash -c "echo 123"
WARNING: IPv4 forwarding is disabled. Networking will not work.
59e0b06bcfb4e407ba29719e0bd805c8f52948429ecfbf94c8616ea5090dcd37
[root@centos-02 ~]# docker logs 59e0b0
123
[root@centos-02 ~]# 

2.刪除容器 rm,刪除一個啟動的容器加-f參數

[root@centos-02 ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS                      PORTS               NAMES
59e0b06bcfb4        centos              "bash -c ‘echo 123‘"   2 minutes ago       Exited (0) 2 minutes ago                        xenodochial_hoover
5e32153dbbd6        centos6             "bash"                 18 minutes ago      Up 18 minutes                                   centos6_1
f4eba170402c        centos              "bash"                 26 minutes ago      Exited (0) 24 minutes ago                       cocky_bell
558e31f7b0fb        centos6             "bash"                 34 minutes ago      Up 32 minutes                                   hopeful_murdock
94b9eab05296        centos6             "bash"                 2 hours ago         Up 2 hours                                      elegant_minsky
815adfd9da61        centos              "/bin/bash"            3 days ago          Up 3 days                                       distracted_mahavira
[root@centos-02 ~]# docker rm 59e0b06bcfb4
59e0b06bcfb4
[root@centos-02 ~]# docker rm f4eba170402c
f4eba170402c
[root@centos-02 ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
5e32153dbbd6        centos6             "bash"              20 minutes ago      Up 20 minutes                           centos6_1
558e31f7b0fb        centos6             "bash"              37 minutes ago      Up 34 minutes                           hopeful_murdock
94b9eab05296        centos6             "bash"              2 hours ago         Up 2 hours                              elegant_minsky
815adfd9da61        centos              "/bin/bash"         3 days ago          Up 3 days                               distracted_mahavira
[root@centos-02 ~]#  
[root@centos-02 ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
5e32153dbbd6        centos6             "bash"              21 minutes ago      Up 21 minutes                           centos6_1
558e31f7b0fb        centos6             "bash"              38 minutes ago      Up 35 minutes                           hopeful_murdock
94b9eab05296        centos6             "bash"              2 hours ago         Up 2 hours                              elegant_minsky
815adfd9da61        centos              "/bin/bash"         3 days ago          Up 3 days                               distracted_mahavira
[root@centos-02 ~]# docker rm 558e31f7b0fb
Error response from daemon: You cannot remove a running container 558e31f7b0fb941ca4ee8c1c2b42553b06ac79c0613984b7ad8b9b4ba97f61fd. Stop the container before attempting removal or 
force remove [root@centos-02 ~]# docker rm -f 558e31f7b0fb 558e31f7b0fb [root@centos-02 ~]#
[root@centos-02 ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
5e32153dbbd6        centos6             "bash"              22 minutes ago      Up 22 minutes                           centos6_1
94b9eab05296        centos6             "bash"              2 hours ago         Up 2 hours                              elegant_minsky
815adfd9da61        centos              "/bin/bash"         3 days ago          Up 3 days                               distracted_mahavira
[root@centos-02 ~]# 

倉庫管理

技術分享圖片

1.用registry鏡像搭建私有倉庫

  

  

  

  

  

  

  

  

  

Docker入門二