docker容器相关命令

一 创建容器
可以使用docker create命令新建一个容器。
[root@localhost ~]# docker create -it ubuntu:latest
5097a7a94472e22bcc38f0d2fe4a1fcf2ab49b047a261f9f62da4ac5a89c27b9
[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                     PORTS               NAMES
5097a7a94472        ubuntu:latest       "/bin/bash"              16 seconds ago      Created                                        dreamy_euclid
e422f73a5651        ubuntu:15.10        "/bin/bash"              2 hours ago         Exited (0) 2 hours ago                         fervent_poincare
bf67651b183f        ubuntu:latest       "/bin/bash"              3 hours ago         Exited (127) 3 hours ago                       fervent_volhard
e26a0172a0aa        ubuntu:15.10        "/bin/bash"              3 hours ago         Exited (0) 3 hours ago                         desperate_aryabhata
fc7112c18128        ubuntu:15.10        "/bin/sh -c 'while tr"   4 hours ago         Exited (137) 4 hours ago                       infallible_kilby
8b58c26d093d        ubuntu:15.10        "/bin/bash"              5 hours ago         Exited (0) 4 hours ago                         stoic_yalow
6df454abad9a        ubuntu:15.10        "/bin/echo 'Hello wor"   5 hours ago         Exited (0) 5 hours ago                         clever_varahamihira
70574cff1a52        hello-world         "/hello"                 5 hours ago         Exited (0) 5 hours ago                         stupefied_panini
使用docker create命令新建的容器处于停止状态,可以使用docker start命令来启动它。
[root@localhost ~]# docker start 5097a7a94472
5097a7a94472
[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                     PORTS               NAMES
5097a7a94472        ubuntu:latest       "/bin/bash"              5 minutes ago       Up 42 seconds                                  dreamy_euclid
e422f73a5651        ubuntu:15.10        "/bin/bash"              2 hours ago         Exited (0) 2 hours ago                         fervent_poincare
bf67651b183f        ubuntu:latest       "/bin/bash"              3 hours ago         Exited (127) 3 hours ago                       fervent_volhard
e26a0172a0aa        ubuntu:15.10        "/bin/bash"              3 hours ago         Exited (0) 3 hours ago                         desperate_aryabhata
fc7112c18128        ubuntu:15.10        "/bin/sh -c 'while tr"   5 hours ago         Exited (137) 4 hours ago                       infallible_kilby
8b58c26d093d        ubuntu:15.10        "/bin/bash"              5 hours ago         Exited (0) 5 hours ago                         stoic_yalow
6df454abad9a        ubuntu:15.10        "/bin/echo 'Hello wor"   5 hours ago         Exited (0) 5 hours ago                         clever_varahamihira
70574cff1a52        hello-world         "/hello"                 5 hours ago         Exited (0) 5 hours ago                         stupefied_panini
 
二 新建并启动容器
下面的命令输出一个“Hello World”,之后容器自动终止。
[root@localhost ~]# docker run ubuntu /bin/echo 'Hello world'
Hello world
利用docker run来创建并启动容器时,Docker在后台运行的标准操作包括:
1、检查本地是否存在指定的镜像,不存在就从共有仓库下载。
2、利用镜像创建并启动一个容器。
3、分配一个文件系统,并在只读的进行层外面挂载一层可读可写。
4、从宿主主机配置的网桥接口中桥接一个虚拟接口到容器中去。
5、从地址池配置一个IP地址给容器
6、执行完用户指定的应用程序。
7、执行完毕后容器被终止。
在容器内用ps命令查看进程,可以看到,只运行了bash应用,并没有运行其他不需要的进程。
用户可以按Ctrl+d或输入exit命令来退出容器。
root@93a1e1c730f0:/# ps
  PID TTY          TIME CMD
    1 ?        00:00:00 bash
   10 ?        00:00:00 ps
root@93a1e1c730f0:/# exit
exit
 
三 容器的停止和启动
1、docker stop
[root@localhost ~]# docker run -d ubuntu /bin/sh -c "while true;do echo hello world;sleep 1;done"
7c60a8165ee1f13c2397c13b399ed6560833cb3db35cc06851f0b81dd4b32a58
[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
7c60a8165ee1        ubuntu              "/bin/sh -c 'while tr"   29 seconds ago      Up 26 seconds                           cranky_shirley
[root@localhost ~]# docker stop 7c6
7c6
[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
2、docker start
处于终止的容器,可以通过docker start命令来重新启动
[root@localhost ~]# docker start 7c6
7c6
[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
7c60a8165ee1        ubuntu              "/bin/sh -c 'while tr"   4 minutes ago       Up 4 seconds                            cranky_shirley
3、docker restart
[root@localhost ~]# docker restart 7c6
7c6
[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
7c60a8165ee1        ubuntu              "/bin/sh -c 'while tr"   4 minutes ago       Up 2 seconds                            cranky_shirley
[root@localhost ~]# docker stop 7c6
7c6
 
四 进入容器
1、attach命令
attach命令是Docker自带的命令,如果想进入容器操作,可执行此命令。
[root@localhost ~]# docker run -idt ubuntu
f9db2f923a8cf9587c1769d847677310d726f8c82a2e89579adbf20735f2c343
[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
f9db2f923a8c        ubuntu              "/bin/bash"         12 seconds ago      Up 9 seconds                            small_wing
[root@localhost ~]# docker attach small_wing
root@f9db2f923a8c:/# 
2、exec命令
[root@localhost ~]# docker run -idt ubuntu
8b6655b34fbb926f3713a519fd764c665d16e71bac76996a5c11f0da536dc342
[root@localhost ~]# docker exec -ti 8b66 /bin/bash
root@8b6655b34fbb:/# 
 
五 删除容器
1、可以使用docker rm命令删除处于终止状态的容器。
[root@localhost ~]# docker ps  -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                        PORTS               NAMES
cf6b48960740        ubuntu              "/bin/sh -c 'while tr"   8 seconds ago       Up 5 seconds                                      peaceful_mahavira
8b6655b34fbb        ubuntu              "/bin/bash"              5 minutes ago       Up 5 minutes                                      admiring_aryabhata
f9db2f923a8c        ubuntu              "/bin/bash"              12 minutes ago      Exited (0) 5 minutes ago                          small_wing
7c60a8165ee1        ubuntu              "/bin/sh -c 'while tr"   21 minutes ago      Exited (137) 16 minutes ago                       cranky_shirley
93a1e1c730f0        ubuntu:15.10        "/bin/bash"              31 minutes ago      Exited (0) 29 minutes ago                         sick_curran
[root@localhost ~]# docker rm f9d
f9d
[root@localhost ~]# docker ps  -a
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS                        PORTS               NAMES
cf6b48960740        ubuntu              "/bin/sh -c 'while tr"   About a minute ago   Up About a minute                                 peaceful_mahavira
8b6655b34fbb        ubuntu              "/bin/bash"              6 minutes ago        Up 6 minutes                                      admiring_aryabhata
7c60a8165ee1        ubuntu              "/bin/sh -c 'while tr"   22 minutes ago       Exited (137) 17 minutes ago                       cranky_shirley
93a1e1c730f0        ubuntu:15.10        "/bin/bash"              32 minutes ago       Exited (0) 30 minutes ago                         sick_curran
2、如果要删除一个运行中的容器,可以添加-f 参数。
[root@localhost ~]# docker rm -f cf6
[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                        PORTS               NAMES
8b6655b34fbb        ubuntu              "/bin/bash"              8 minutes ago       Up 8 minutes                                      admiring_aryabhata
7c60a8165ee1        ubuntu              "/bin/sh -c 'while tr"   24 minutes ago      Exited (137) 19 minutes ago                       cranky_shirley
93a1e1c730f0        ubuntu:15.10        "/bin/bash"              34 minutes ago      Exited (0) 32 minutes ago                         sick_curran
 
六 导出和导入容器
1、导出容器
导出容器是指导出一个已经创建的容器到一个文件,不管此时这个容器是否处于运行状态,可以使用docker export命令,该命令格式为docker export CONTAINER。
导出容器到文件test.tar中
[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                         PORTS               NAMES
a993263fcaa1        ubuntu              "-t -i /bin/bash"        53 minutes ago      Created                                            grave_hodgkin
9ec4ef73572a        ubuntu              "-i -t /bin/bash"        53 minutes ago      Created                                            elegant_sammet
05e8791de1c7        ubuntu              "-it /bin/bash"          54 minutes ago      Created                                            furious_leavitt
5097a7a94472        ubuntu:latest       "/bin/bash"              About an hour ago   Exited (0) About an hour ago                       dreamy_euclid
e422f73a5651        ubuntu:15.10        "/bin/bash"              3 hours ago         Exited (0) 3 hours ago                             fervent_poincare
fc7112c18128        ubuntu:15.10        "/bin/sh -c 'while tr"   6 hours ago         Exited (137) 6 hours ago                           infallible_kilby
[root@localhost ~]# docker export fc > test.tar
2、导入容器
导出的文件又可以使用docker import命令导入,成为镜像。
[root@localhost ~]# docker images
REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
docker.io/httpd         2.2                 3624a4b77da8        2 weeks ago         169.9 MB
docker.io/ubuntu        latest              0ef2e08ed3fa        2 weeks ago         130 MB
docker.io/hello-world   latest              48b5124b2768        9 weeks ago         1.84 kB
docker.io/ubuntu        15.10               9b9cb95443b5        7 months ago        137.2 MB
[root@localhost ~]# cat test1.tar |docker import - test/while
sha256:cf1473016855cd216787c3b86cc7c84b8130e6025dbf6a27414c3e43fc5c2d7c
[root@localhost ~]# docker images
REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
test/while              latest              cf1473016855        43 seconds ago      118.8 MB
docker.io/httpd         2.2                 3624a4b77da8        2 weeks ago         169.9 MB
docker.io/ubuntu        latest              0ef2e08ed3fa        2 weeks ago         130 MB
docker.io/hello-world   latest              48b5124b2768        9 weeks ago         1.84 kB
docker.io/ubuntu        15.10               9b9cb95443b5        7 months ago        137.2 MB

猜你喜欢

转载自cakin24.iteye.com/blog/2372335