1. 程式人生 > >docker容器的常用操作

docker容器的常用操作

 

建立容器

 

[[email protected] ~]# docker create --name test_create -ti ubuntu
1d13990eea0d3f282a0f7d5589f40e49394d8fa7a766c95cc9b95ebf17deeb7f
[[email protected] ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                 CREATED             STATUS                    PORTS               NAMES
1d13990eea0d        ubuntu              
"/bin/bash" 12 seconds ago Created test_create ff2f56ca3a73 d3ae5b7a2ce8 "bash" 4 hours ago Exited (0) 4 hours ago tender_ardinghelli e2ee7db10156 ubuntu "bash" 5
hours ago Exited (0) 5 hours ago objective_booth 51e2940a5804 ubuntu "echo 'HI you hao a'" 5 hours ago Exited (0) 5 hours ago musing_hawking 9d8caf15e8f6 hello-world "/hello" 23 hours ago Exited (0
) 23 hours ago cranky_stonebraker [[email protected] ~]#

 

 

 

 

[[email protected] ~]# docker run -ti -d --name test_network ubuntu bash
bc45e45234057e781c4b0b0c3c802d03f382fb2e59549433cb0889f1c5afa224
[[email protected] ~]# docker exec -ti test_network bash
[email protected]:/# ifconfig
bash: ifconfig: command not found
[email protected]:/# cat /etc/hosts
127.0.0.1    localhost
::1    localhost ip6-localhost ip6-loopback
fe00::0    ip6-localnet
ff00::0    ip6-mcastprefix
ff02::1    ip6-allnodes
ff02::2    ip6-allrouters
172.17.0.2    bc45e4523405
[email protected]:/# ps -ef
UID         PID   PPID  C STIME TTY          TIME CMD
root          1      0  0 13:49 ?        00:00:00 bash
root          9      0  0 13:49 ?        00:00:00 bash
root         19      9  0 13:51 ?        00:00:00 ps -ef
[email protected]:/# 

 

 

[[email protected] ~]# docker run -ti --name test_finish ubuntu echo "hi sali"
hi sali
[[email protected] ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                 CREATED             STATUS                     PORTS               NAMES
1477720733ed        ubuntu              "echo 'hi sali'"        3 minutes ago       Exited (0) 3 minutes ago                       test_finish
bc45e4523405        ubuntu              "bash"                  8 minutes ago       Up 8 minutes                                   test_network
1d13990eea0d        ubuntu              "/bin/bash"             33 minutes ago      Created                                        test_create
ff2f56ca3a73        d3ae5b7a2ce8        "bash"                  5 hours ago         Exited (0) 5 hours ago                         tender_ardinghelli
e2ee7db10156        ubuntu              "bash"                  6 hours ago         Exited (0) 6 hours ago                         objective_booth
51e2940a5804        ubuntu              "echo 'HI you hao a'"   6 hours ago         Exited (0) 6 hours ago                         musing_hawking
9d8caf15e8f6        hello-world         "/hello"                23 hours ago        Exited (0) 23 hours ago                        cranky_stonebraker
[[email protected] ~]# 

 

 

 

 

[[email protected] ~]# docker run -t -i ubuntu /bin/bash
[email protected]:/# ls
bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
[email protected]:/# pwd
/
[email protected]:/# exit
exit
[[email protected] ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                 CREATED             STATUS                     PORTS               NAMES
3b72a81646ce        ubuntu              "/bin/bash"             19 seconds ago      Exited (0) 7 seconds ago                       amazing_bartik
1477720733ed        ubuntu              "echo 'hi sali'"        8 minutes ago       Exited (0) 8 minutes ago                       test_finish
bc45e4523405        ubuntu              "bash"                  13 minutes ago      Up 13 minutes                                  test_network
1d13990eea0d        ubuntu              "/bin/bash"             38 minutes ago      Created                                        test_create
ff2f56ca3a73        d3ae5b7a2ce8        "bash"                  5 hours ago         Exited (0) 5 hours ago                         tender_ardinghelli
e2ee7db10156        ubuntu              "bash"                  6 hours ago         Exited (0) 6 hours ago                         objective_booth
51e2940a5804        ubuntu              "echo 'HI you hao a'"   6 hours ago         Exited (0) 6 hours ago                         musing_hawking
9d8caf15e8f6        hello-world         "/hello"                23 hours ago        Exited (0) 23 hours ago                        cranky_stonebraker
[[email protected] ~]# 

  

 

[[email protected] ~]# docker run -d --name test_daemon ubuntu /bin/sh -c "while true;do echo hello guet;sleep 1;done"
866ce2b92ac56730ba7625da5bd47a21f8b4f4341c105654a42ef56a09e36c0a
[[email protected] ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
866ce2b92ac5        ubuntu              "/bin/sh -c 'while..."   7 seconds ago       Up 6 seconds                            test_daemon
bc45e4523405        ubuntu              "bash"                   18 minutes ago      Up 18 minutes                           test_network

 

 

 

 終止容器

 

 

 

[[email protected] ~]# docker stop test_daemon 
test_daemon
[[email protected] ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                        PORTS               NAMES
866ce2b92ac5        ubuntu              "/bin/sh -c 'while..."   15 minutes ago      Exited (137) 11 seconds ago                       test_daemon
3b72a81646ce        ubuntu              "/bin/bash"              20 minutes ago      Exited (0) 20 minutes ago                         amazing_bartik
1477720733ed        ubuntu              "echo 'hi sali'"         28 minutes ago      Exited (0) 28 minutes ago                         test_finish
bc45e4523405        ubuntu              "bash"                   33 minutes ago      Up 33 minutes                                     test_network
1d13990eea0d        ubuntu              "/bin/bash"              About an hour ago   Created                                           test_create
ff2f56ca3a73        d3ae5b7a2ce8        "bash"                   5 hours ago         Exited (0) 5 hours ago                            tender_ardinghelli
e2ee7db10156        ubuntu              "bash"                   6 hours ago         Exited (0) 6 hours ago                            objective_booth
51e2940a5804        ubuntu              "echo 'HI you hao a'"    6 hours ago         Exited (0) 6 hours ago                            musing_hawking
9d8caf15e8f6        hello-world         "/hello"                 24 hours ago        Exited (0) 24 hours ago                           cranky_stonebraker

 

 

 進入容器

 

 

[[email protected] ~]# docker run -d --name test_ti ubuntu /bin/sh -c "while true;do echo hello guet;sleep 1;done"
c1839dfd85031e7dfa786f236701b523c5334b7b6c3d0b4eeb97ad3419dd266d
[[email protected] ~]# docker exec -i -t test_ti bash
[email protected]:/# ps -ef
UID         PID   PPID  C STIME TTY          TIME CMD
root          1      0  0 14:50 ?        00:00:00 /bin/sh -c while true;do echo hello guet;sleep 1;done
root         28      0  0 14:50 ?        00:00:00 bash
root         41      1  0 14:50 ?        00:00:00 sleep 1
root         42     28  0 14:50 ?        00:00:00 ps -ef
[email protected]:/# exit
exit
[[email protected] ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
c1839dfd8503        ubuntu              "/bin/sh -c 'while..."   38 seconds ago      Up 37 seconds                           test_ti
bc45e4523405        ubuntu              "bash"                   About an hour ago   Up About an hour                        test_network
[[email protected] ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                         PORTS               NAMES
c1839dfd8503        ubuntu              "/bin/sh -c 'while..."   44 seconds ago      Up 43 seconds                                      test_ti
866ce2b92ac5        ubuntu              "/bin/sh -c 'while..."   43 minutes ago      Exited (137) 28 minutes ago                        test_daemon
3b72a81646ce        ubuntu              "/bin/bash"              About an hour ago   Exited (0) About an hour ago                       amazing_bartik
1477720733ed        ubuntu              "echo 'hi sali'"         About an hour ago   Exited (0) About an hour ago                       test_finish
bc45e4523405        ubuntu              "bash"                   About an hour ago   Up About an hour                                   test_network
1d13990eea0d        ubuntu              "/bin/bash"              About an hour ago   Created                                            test_create
ff2f56ca3a73        d3ae5b7a2ce8        "bash"                   6 hours ago         Exited (0) 6 hours ago                             tender_ardinghelli
e2ee7db10156        ubuntu              "bash"                   7 hours ago         Exited (0) 6 hours ago                             objective_booth
51e2940a5804        ubuntu              "echo 'HI you hao a'"    7 hours ago         Exited (0) 7 hours ago                             musing_hawking
9d8caf15e8f6        hello-world         "/hello"                 24 hours ago        Exited (0) 24 hours ago                            cranky_stonebraker
[[email protected] ~]#

 

 

 刪除容器

 

 

[[email protected] ~]# docker rm test_ti
Error response from daemon: You cannot remove a running container c1839dfd85031e7dfa786f236701b523c5334b7b6c3d0b4eeb97ad3419dd266d. 
Stop the container before attempting removal or force remove [[email protected]
~]# docker stop test_ti test_ti [[email protected] ~]# docker rm test_ti test_ti [[email protected] ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 866ce2b92ac5 ubuntu "/bin/sh -c 'while..." About an hour ago Exited (137) 33 minutes ago test_daemon 3b72a81646ce ubuntu "/bin/bash" About an hour ago Exited (0) About an hour ago amazing_bartik 1477720733ed ubuntu "echo 'hi sali'" About an hour ago Exited (0) About an hour ago test_finish bc45e4523405 ubuntu "bash" About an hour ago Up About an hour test_network 1d13990eea0d ubuntu "/bin/bash" 2 hours ago Created test_create ff2f56ca3a73 d3ae5b7a2ce8 "bash" 6 hours ago Exited (0) 6 hours ago tender_ardinghelli e2ee7db10156 ubuntu "bash" 7 hours ago Exited (0) 7 hours ago objective_booth 51e2940a5804 ubuntu "echo 'HI you hao a'" 7 hours ago Exited (0) 7 hours ago musing_hawking 9d8caf15e8f6 hello-world "/hello" 24 hours ago Exited (0) 24 hours ago cranky_stonebraker [[email protected] ~]#

 

 

[email protected] ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
bc45e4523405        ubuntu              "bash"              About an hour ago   Up About an hour                        test_network
[[email protected] ~]# docker rm -f test_network
test_network
[[email protected] ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[[email protected] ~]# 

 

 

 匯入和匯出容器

 

 

 

[[email protected] ~]# docker run --name test_export -ti ubuntu bash
[email protected]:/# echo "hello world hadoop docker" > export.txt
[email protected]:/# ls
bin  boot  dev  etc  export.txt  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
[email protected]:/# cat export.txt 
hello world hadoop docker
[email protected]:/# exit
exit
[[email protected] ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                         PORTS               NAMES
04b7794db11c        ubuntu              "bash"                   54 seconds ago      Exited (0) 6 seconds ago                           te_export
866ce2b92ac5        ubuntu              "/bin/sh -c 'while..."   About an hour ago   Exited (137) 42 minutes ago                        test_daemon
3b72a81646ce        ubuntu              "/bin/bash"              About an hour ago   Exited (0) About an hour ago                       amazing_bartik
1477720733ed        ubuntu              "echo 'hi sali'"         About an hour ago   Exited (0) About an hour ago                       test_finish
1d13990eea0d        ubuntu              "/bin/bash"              2 hours ago         Created                                            test_create
ff2f56ca3a73        d3ae5b7a2ce8        "bash"                   6 hours ago         Exited (0) 6 hours ago                             tender_ardinghelli
e2ee7db10156        ubuntu              "bash"                   7 hours ago         Exited (0) 7 hours ago                             objective_booth
51e2940a5804        ubuntu              "echo 'HI you hao a'"    7 hours ago         Exited (0) 7 hours ago                             musing_hawking
9d8caf15e8f6        hello-world         "/hello"                 24 hours ago        Exited (0) 24 hours ago                            cranky_stonebraker
[[email protected] ~]# docker export test_export > export.tar
Error response from daemon: No such container: test_export
[[email protected] ~]# docker export te_export > export.tar
[[email protected] ~]# ls
anaconda-ks.cfg  export.tar  initial-setup-ks.cfg  test.tar
[[email protected] ~]#

 

 

[[email protected] ~]# cat export.tar | docker import - dajiangtai/testimport:latest
sha256:6f35c73d51efe8cb0385d08d37b9f02f8eed4d03d8e215b6e7a3cedb2af3e7ec
[[email protected] ~]# docker images
REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
dajiangtai/testimport   latest              6f35c73d51ef        9 seconds ago       69.8MB
810905729/test          latest              d3ae5b7a2ce8        6 hours ago         85.8MB
gong/test               latest              d3ae5b7a2ce8        6 hours ago         85.8MB
gongstudy/test2         latest              d3ae5b7a2ce8        6 hours ago         85.8MB
root/test               latest              d3ae5b7a2ce8        6 hours ago         85.8MB
ubuntu                  latest              ea4c82dcd15a        2 weeks ago         85.8MB
hello-world             latest              4ab4c602aa5e        8 weeks ago         1.84kB
[[email protected] ~]# 

 

 

 

[[email protected] ~]# docker run -ti dajiangtai/testimport bash
[email protected]:/# ls
bin  boot  dev  etc  export.txt  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
[email protected]:/# cat export.txt 
hello world hadoop docker
[email protected]:/# exit
exit
[[email protected] ~]#