docker容器的常用操作

创建容器

[root@mydocker ~]# docker create --name test_create -ti ubuntu
1d13990eea0d3f282a0f7d5589f40e49394d8fa7a766c95cc9b95ebf17deeb7f
[root@mydocker ~]# 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
[root@mydocker ~]# 

 

[root@mydocker ~]# docker run -ti -d --name test_network ubuntu bash
bc45e45234057e781c4b0b0c3c802d03f382fb2e59549433cb0889f1c5afa224
[root@mydocker ~]# docker exec -ti test_network bash
root@bc45e4523405:/# ifconfig
bash: ifconfig: command not found
root@bc45e4523405:/# 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
root@bc45e4523405:/# 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
root@bc45e4523405:/# 

[root@mydocker ~]# docker run -ti --name test_finish ubuntu echo "hi sali"
hi sali
[root@mydocker ~]# 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
[root@mydocker ~]# 

 

[root@mydocker ~]# docker run -t -i ubuntu /bin/bash
root@3b72a81646ce:/# ls
bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
root@3b72a81646ce:/# pwd
/
root@3b72a81646ce:/# exit
exit
[root@mydocker ~]# 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
[root@mydocker ~]# 

  

[root@mydocker ~]# docker run -d --name test_daemon ubuntu /bin/sh -c "while true;do echo hello guet;sleep 1;done"
866ce2b92ac56730ba7625da5bd47a21f8b4f4341c105654a42ef56a09e36c0a
[root@mydocker ~]# 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

 终止容器

 

 

[root@mydocker ~]# docker stop test_daemon 
test_daemon
[root@mydocker ~]# 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

 进入容器

 

[root@mydocker ~]# docker run -d --name test_ti ubuntu /bin/sh -c "while true;do echo hello guet;sleep 1;done"
c1839dfd85031e7dfa786f236701b523c5334b7b6c3d0b4eeb97ad3419dd266d
[root@mydocker ~]# docker exec -i -t test_ti bash
root@c1839dfd8503:/# 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
root@c1839dfd8503:/# exit
exit
[root@mydocker ~]# 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
[root@mydocker ~]# 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
[root@mydocker ~]#

 删除容器

 

[root@mydocker ~]# docker rm test_ti
Error response from daemon: You cannot remove a running container c1839dfd85031e7dfa786f236701b523c5334b7b6c3d0b4eeb97ad3419dd266d. 
Stop the container before attempting removal or force remove [root@mydocker
~]# docker stop test_ti test_ti [root@mydocker ~]# docker rm test_ti test_ti [root@mydocker ~]# 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 [root@mydocker ~]#

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

 导入和导出容器

 

 

[root@mydocker ~]# docker run --name test_export -ti ubuntu bash
root@04b7794db11c:/# echo "hello world hadoop docker" > export.txt
root@04b7794db11c:/# ls
bin  boot  dev  etc  export.txt  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
root@04b7794db11c:/# cat export.txt 
hello world hadoop docker
root@04b7794db11c:/# exit
exit
[root@mydocker ~]# 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
[root@mydocker ~]# docker export test_export > export.tar
Error response from daemon: No such container: test_export
[root@mydocker ~]# docker export te_export > export.tar
[root@mydocker ~]# ls
anaconda-ks.cfg  export.tar  initial-setup-ks.cfg  test.tar
[root@mydocker ~]#

[root@mydocker ~]# cat export.tar | docker import - dajiangtai/testimport:latest
sha256:6f35c73d51efe8cb0385d08d37b9f02f8eed4d03d8e215b6e7a3cedb2af3e7ec
[root@mydocker ~]# 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
[root@mydocker ~]# 

 

[root@mydocker ~]# docker run -ti dajiangtai/testimport bash
root@891713e46dec:/# ls
bin  boot  dev  etc  export.txt  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
root@891713e46dec:/# cat export.txt 
hello world hadoop docker
root@891713e46dec:/# exit
exit
[root@mydocker ~]# 
原文地址:https://www.cnblogs.com/braveym/p/9906557.html