docker學習筆記(二):鏡像及容器

1啓動這個容器:            docker    run  -itd      centos docker

     查看這個容器:            docker   psubuntu

[root@localhost_001 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
2e1518f22125        centos              "/bin/bash"         9 seconds ago       Up 5 seconds                            upbeat_lichterman
[root@localhost_001 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
2e1518f22125        centos              "/bin/bash"         14 seconds ago      Up 10 seconds                           upbeat_lichterman

2、如何進入到一個容器下:      docker   exec         容器ID (CONTAINER ID)     bashcentos

exec  後面能夠更  容器 ID(前六位)   ,         也能夠是  容器的 NAMESbash

[root@localhost_001 ~]# docker exec -it 2e1518 bash
[root@2e1518f22125 /]# ls
anaconda-post.log  dev  home  lib64  mnt  proc  run   srv  tmp  var
bin                etc  lib   media  opt  root  sbin  sys  usr
[root@2e1518f22125 /]# df -h 
Filesystem      Size  Used Avail Use% Mounted on
overlay          36G  4.3G   32G  12% /
tmpfs            64M     0   64M   0% /dev
tmpfs           489M     0  489M   0% /sys/fs/cgroup
/dev/sda3        36G  4.3G   32G  12% /etc/hosts
shm              64M     0   64M   0% /dev/shm
tmpfs           489M     0  489M   0% /proc/asound
tmpfs           489M     0  489M   0% /proc/acpi
tmpfs           489M     0  489M   0% /proc/scsi
tmpfs           489M     0  489M   0% /sys/firmware
[root@2e1518f22125 /]# free 
              total        used        free      shared  buff/cache   available
Mem:         999700      636412       96948        1772      266340      176564
Swap:       4194300        7648     4186652

註釋:容器就是一個閹割版的操做系統,大約只有100M: 能夠看到磁盤和內存都和宿主機的大小是同樣的;而且可使用的命令只有600多個;app

以下:在容器是不能查看IP地址的,全部須要安裝 net-tools 這個包;post

ot@2e1518f22125 /]# ifconfig 
bash: ifconfig: command not found
[root@2e1518f22125 /]# yum install -y net-tools
[root@2e1518f22125 /]# 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 2446  bytes 9684204 (9.2 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 2072  bytes 115550 (112.8 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

註釋:能夠看到IP地址變成172.17.0.2,這是docker生成的一個網段,相似於vmware的net8 NAT模式,每開啓一個docker就會生成一個新的網卡生成;spa

而後此時查看宿主機也有這幾個網段了;操作系統

[root@localhost_001 ~]# ifconfig
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:5bff:fe9e:626f  prefixlen 64  scopeid 0x20<link>
        ether 02:42:5b:9e:62:6f  txqueuelen 0  (Ethernet)
        RX packets 2100  bytes 88502 (86.4 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 2462  bytes 9685740 (9.2 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 03d

3、如今能夠把這個容器的變動保存到鏡像裏去,作成一個新的鏡像了;好比:在這個容器裏安裝 net-tools 命令,下次能夠直接使用這個容器,這樣能夠查看ifconig這個命令,而就不須要再從新安裝了;rest

docker commit -m "install net-tools" -a "yuanhh" 2e1518f22125 centos_with_net
 -m   "後面跟加的一些改動信息"        -a   "表示那個做者改動的"

[root@localhost_001 ~]# docker commit -m "install net-tools" -a "yuanhh" 2e1518f22125 centos_with_net
sha256:e9fff0b4ad1ad2fba2c142532ebc085e04105b56e707ee343f5b2029b88328d8
[root@localhost_001 ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos_with_net     latest              e9fff0b4ad1a        15 seconds ago      280MB
ubuntu              latest              94e814e2efa8        39 hours ago        88.9MB
centos              latest              1e1148e4cc2c        3 months ago        202MB
fenye               latest              1e1148e4cc2c        3 months ago        202MB
yuanhh              190312              1e1148e4cc2c        3 months ago        202MB

4、能夠運行上面生成的那個鏡像試試;    docker   run  -itd    centos_with_net

並進入這個容器:       docker exec -it dreamy_aryabhata bash

[root@localhost_001 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
288405f3fde6        centos_with_net     "/bin/bash"         12 seconds ago      Up 8 seconds                            dreamy_aryabhata
2e1518f22125        centos              "/bin/bash"         20 minutes ago      Up 20 minutes                           upbeat_lichterman

[root@localhost_001 ~]# docker exec -it dreamy_aryabhata bash
[root@288405f3fde6 /]# 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 648 (648.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

註釋:發現能夠直接運行這個ifconfig 這個命令,由於這個容器是以前的容器保存成鏡像的,而且還發現和以前的那個容器是在一個網段的; 172.17.0.3

五、docker使用模板建立鏡像:

一、首先去官網openvz.org 下載一個模板:  https://download.openvz.org/template/precreated/centos-6-x86-minimal.tar.gz

[root@localhost_001 ~]# wget https://download.openvz.org/template/precreated/centos-6-x86-minimal.tar.gz
--2019-03-14 10:10:27--  https://download.openvz.org/template/precreated/centos-6-x86-minimal.tar.gz
正在鏈接 download.openvz.org (download.openvz.org)|185.231.241.69|:443... 已鏈接。
已發出 HTTP 請求,正在等待迴應... 200 OK
長度:210291971 (201M) [application/x-gzip]
正在保存至: 「centos-6-x86-minimal.tar.gz」

二、導入該鏡像:    cat   centos-6-x86-minimal.tar.gz |docker   import   -  centos 

[root@localhost_001 ~]# cat centos-6-x86-minimal.tar.gz |docker import - centos6
sha256:afa1f6db949f4ad7ae513ac5de1c9803355fa35ce8420fb3d67fb90d8fe3e48d

3、查看這個鏡像:   docker    images                   |  第一個就是;

[root@localhost_001 ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos6             latest              afa1f6db949f        15 minutes ago      512MB
centos_with_net     latest              e9fff0b4ad1a        11 hours ago        280MB
ubuntu              latest              94e814e2efa8        2 days ago          88.9MB
centos              latest              1e1148e4cc2c        3 months ago        202MB
fenye               latest              1e1148e4cc2c        3 months ago        202MB
yuanhh              190312              1e1148e4cc2c        3 months ago        202MB

啓動並進入到這個容器:   docker  run  -itd   centos   bash                  |            docker     ps

[root@localhost_001 ~]# docker run -itd centos6 bash         #啓動這個容器;
b340e953ed68ca4461a52ea061310bb1f7a5a0773437e79acad158556d13dd4a
[root@localhost_001 ~]# docker ps                 #查看當前運行的容器;
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
b340e953ed68        centos6             "bash"              12 seconds ago      Up 6 seconds                            peaceful_herschel
[root@localhost_001 ~]# docker exec -it b340e95 bash        #進入這個容器;
[root@b340e953ed68 /]# ifconfig                             #查看這個網卡;
eth0      Link encap:Ethernet  HWaddr 02:42:AC:11:00:02  
          inet addr:172.17.0.2  Bcast:172.17.255.255  Mask:255.255.0.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:16 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:1296 (1.2 KiB)  TX bytes:0 (0.0 b)
[root@b340e953ed68 /]# cat /etc/issue          #查看系統版本
CentOS release 6.8 (Final) 
Kernel \r on an \m

註釋:並且和宿主機使用的是同一個內核;  只是主機名不一樣而已;

4、把現有鏡像導出爲一個文件:             docker save -o yuanhh_centos_with(新文件名)     centos_with_net(舊文件名)

[root@localhost_001 ~]# docker save -o yuanhh_centos_with  centos_with_net
[root@localhost_001 ~]# ls
yuanhh_centos_with

註釋: 咱們還可使用這個文件恢復本地鏡像;

docker    load    --input     yuanhh_centos_with    或者       docker     load     <  yuanhh_centos_with

[root@localhost_001 ~]# docker rmi -f centos_with_net       #刪除這個鏡像, -f 表示強制刪除
Untagged: centos_with_net:latest
Deleted: sha256:e9fff0b4ad1ad2fba2c142532ebc085e04105b56e707ee343f5b2029b88328d8
[root@localhost_001 ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos6             latest              afa1f6db949f        33 minutes ago      512MB
ubuntu              latest              94e814e2efa8        2 days ago          88.9MB
centos              latest              1e1148e4cc2c        3 months ago        202MB
fenye               latest              1e1148e4cc2c        3 months ago        202MB
yuanhh              190312              1e1148e4cc2c        3 months ago        202MB
[root@localhost_001 ~]# docker load < yuanhh_centos_with       #恢復;
Loaded image: centos_with_net:latest
[root@localhost_001 ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos6             latest              afa1f6db949f        35 minutes ago      512MB
centos_with_net     latest              e9fff0b4ad1a        12 hours ago        280MB

其中如上導出鏡像導入鏡像命令:    save      load  是一對;

而後導入鏡像和導出鏡像命令:        export  和   import  是一對;

5、固然也能夠把本身的鏡像導入傳到 dockerhub官網上去,可是前提須要註冊一個用戶才能夠;

docker  push   image      name

6、容器管理;     docker  create -it  鏡像名稱                                         docker    start|stop|restart       容器ID

建立一個容器,可是該容器並無啓動:         docker   create -it    centos6   bash           用  docker   ps   -a    才能查看到;

啓動一個容器:    docker    start|stop|restart       容器ID(CONTAINER ID)                     啓動|關閉|重啓

[root@localhost_001 ~]# docker create -it centos bash          #建立一個容器
1c9e512ff5a1e8f4d506963ddeb97a93681de97cae4c7d2b02b9cb3784c2c967
[root@localhost_001 ~]# docker ps -a            #查看這個容器; 狀態顯示 created 狀態
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                       PORTS               NAMES
1c9e512ff5a1        centos              "bash"              4 seconds ago       Created                                          silly_knuth
8a021ac19c69        fenye               "/bin/bash"         2 minutes ago       Up 2 minutes                                     kind_jones

註釋:發現以上頁面顯示 created  啓動狀態;           啓動成功後用  docker   ps   就能夠查看到了;

docker    start   1c9e512ff5a1(容器ID)

[root@localhost_001 ~]# docker start 1c9e512ff5a1
1c9e512ff5a1
[root@localhost_001 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
1c9e512ff5a1        centos              "bash"              6 minutes ago       Up 2 seconds                            silly_knuth
8a021ac19c69        fenye               "/bin/bash"         9 minutes ago       Up 8 minutes                            kind_jones
b340e953ed68        centos6             "bash"              11 hours ago        Up 3 minutes                            peaceful_herschel

註釋:其實這樣子:   docker   run    -tid    鏡像名稱    就等同因而   先 create  再  start

7、直接進入這個容器操做:  docker run -it centos bash       

能夠容許一些命令,而後使用 exit  和 ctrl+d 退出後,這個容器也會中止;  由於沒有加 -d  ,   -d 則表示在後臺運行這個容器;

[root@localhost_001 ~]# docker run -it fenye bash
[root@3d6aa960c09a /]# ifconig 
bash: ifconig: command not found
[root@3d6aa960c09a /]# yum install -y net-tools > /dev/null
[root@3d6aa960c09a /]# exit            #退出
exit
[root@localhost_001 ~]# docker ps        #退出後發現這個容器也沒有在運行了;
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
e10f1c3ae75e        centos              "bash"              8 minutes ago       Up 8 minutes                            zealous_galois

註釋:  固然也能夠只讓容器在後臺運行:   docker   run    -d    centos 

8、 固然也能夠只讓容器在後臺運行:   docker   run    -d    centos

好比:能夠是用  -c 選項讓docker 運行一個命令,而後在退出;  docker run -d fenye bash -c "while :;do echo "123";sleep 2;done"

[root@localhost_001 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
4b299a44f97d        fenye               "bash -c 'while :;do…"   41 seconds ago      Up 37 seconds                           eager_chatelet
e10f1c3ae75e        centos              "bash"                   13 minutes ago      Up 13 minutes                           zealous_galois
[root@localhost_001 ~]# 
[root@localhost_001 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS              PORTS               NAMES
4b299a44f97d        fenye               "bash -c 'while :;do…"   About a minute ago   Up About a minute                       eager_chatelet
#註釋:每隔兩秒打印 一次123就能夠;

9、給容器自定義名稱:   如上圖最右側的 NAME 參數;    docker   rum  -itd   --name  ubuntu_test    ubuntu   bash

[root@localhost_001 ~]# docker run -itd --name ubuntu_test ubuntu bash
894dc0c4a29c22a66db30a5ce4c23b558520602b524b5450ad9dfbbef2aab16a
[root@localhost_001 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
894dc0c4a29c        ubuntu              "bash"              17 seconds ago      Up 14 seconds                           ubuntu_test
e10f1c3ae75e        centos              "bash"              20 minutes ago      Up 19 minutes                           zealous_galois

而後進入這個容器時,就能夠加 NAME  就能夠了;   docker    exec -it    ubuntu_test

固然也能夠加 容器 ID 進入:  docker    exec   -it    894dc0c4a29c

[root@localhost_001 ~]# docker exec -it ubuntu_test bash
root@894dc0c4a29c:/# ls     
bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var

10、也可讓容器 執行完某個命令後直接刪除;     docker    run  --rm  -it    fenye   bash    -c   "sleep  60"

[root@localhost_001 ~]# docker run  --rm  -itd fenye  bash -c "sleep 60"
4570ff07657196736ba8246e08c8a6c76278a4b6edf9d8d3479c7360a2163c4a
[root@localhost_001 ~]# 
[root@localhost_001 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS              PORTS               NAMES
4570ff076571        fenye               "bash -c 'sleep 60'"   3 seconds ago       Up 2 seconds                            eager_wilbur
894dc0c4a29c        ubuntu              "bash"                 8 minutes ago       Up 8 minutes                            ubuntu_test
[root@localhost_001 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
894dc0c4a29c        ubuntu              "bash"              10 minutes ago      Up 10 minutes                           ubuntu_test

11、獲取容器運行的歷史信息:            docker    logs   容器ID

[root@localhost_001 ~]# docker run -itd  fenye  bash -c "echo yuanhh"
463522739e5ece95445b7ce85d3e112fa3cb99fb34a519fc33c98ee087bf3c89
[root@localhost_001 ~]# 
[root@localhost_001 ~]# docker logs 46352273
yuanhh

註釋docker   attach    能夠進入一個後臺運行的容器, 好比:  docker      attach   aa964fd01624(容器ID)     可是 attach 這個命令並很差用,若是使用這個進入後臺容器,再退出exit,而後就會直接退出這個容器了;         也可使用   ctrl+q  ,臨時退出這個容器,不會退出後臺程序;

[root@localhost_001 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
aa964fd01624        fenye               "bash"              5 seconds ago       Up 3 seconds                            gallant_cray
894dc0c4a29c        ubuntu              "bash"              20 minutes ago      Up 20 minutes                           ubuntu_test
[root@localhost_001 ~]# docker attach  aa964fd01624
[root@aa964fd01624 /]# exit
exit
[root@localhost_001 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
894dc0c4a29c        ubuntu              "bash"              25 minutes ago      Up 25 minutes                           ubuntu_test

固然,還有一個辦法,   docker   exec  -itd   容器ID(CONTAINEA ID)  bash   這樣就算是用exit退出後,容器仍然會在後臺運行;

十二、刪除容器:      docker     rm    容器ID(CONTAINEA ID)   這樣能夠把容器刪除掉,若是在運行着的容器,則須要加上 -f 選項;

[root@localhost_001 ~]# docker rm  288405f3fde6
288405f3fde6
[root@localhost_001 ~]# docker rm  8a021ac19c69
8a021ac19c69

[root@localhost_001 ~]# docker rm  -f e10f1c3ae75e 
e10f1c3ae75e
[root@localhost_001 ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
894dc0c4a29c        ubuntu              "bash"              33 minutes ago      Up 33 minutes                           ubuntu_test
1c9e512ff5a1        centos              "bash"              About an hour ago   Up About an hour                        silly_knuth

13、導出容器:                 export      容器ID   >  file.tar                    容器導出以後能夠遷移到其它機器去使用的;

[root@localhost_001 ~]# docker export 894dc0c4a29c > file.tar

[root@localhost_001 ~]# 
[root@localhost_001 ~]# ls -ld file.tar 
-rw-r--r-- 1 root root 72308736 3月  14 22:53 file.tar

導入容器並生成鏡像:       cat  file.tar   |docker  import    - yuanhh_test            導入容器並生成 yuanhh_test鏡像;

[root@localhost_001 ~]# cat file.tar |docker import - yuanhh_test 
sha256:56830bb9c4023bc2bd691fe4f5cce59fcb47d0ce81043d49f3f516c1e330fe75
[root@localhost_001 ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
yuanhh_test         latest              56830bb9c402        7 seconds ago       69.8MB
centos6             latest              afa1f6db949f        12 hours ago        512MB
相關文章
相關標籤/搜索