[关闭]
@zhaikun 2016-12-28T10:16:28.000000Z 字数 3623 阅读 1238

docker 容器启动

docker


启动容器

启动容器有两种方式,一种是基于镜像新建一个容器并启动,另外一个是将在终止状态(stopped)的容器重新启动。

因为 Docker 的容器实在太轻量级了,很多时候用户都是随时删除和新创建容器。

新建并启动

所需要的命令主要为 docker run
例如,下面的命令输出一个 “Hello World”,之后终止容器。

  1. [root@zzk ~]# docker run -it centos /bin/echo "Hello world"
  2. Hello world
  3. [root@zzk ~]#

这跟在本地直接执行 /bin/echo 'hello world' 几乎感觉不出任何区别。

下面的命令则启动一个 bash 终端,允许用户进行交互。

  1. [root@zzk ~]# docker run -it centos bash
  2. [root@ddc24a6d00f0 /]#

其中,-t 选项让Docker分配一个伪终端(pseudo-tty)并绑定到容器的标准输入上, -i 则让容器的标准输入保持打开。

当利用 docker run 来创建容器时,Docker 在后台运行的标准操作包括:

启动已终止容器

可以利用 docker ps -a 命令查看当前系统中容器的列表。

  1. [root@zzk ~]# docker ps -a
  2. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  3. ddc24a6d00f0 centos "bash" 5 minutes ago Exited (0) 5 seconds ago modest_gates
  4. 7b71848a072a centos "/bin/echo 'Hello wor" 7 minutes ago Exited (0) 7 minutes ago big_bell
  5. c4cc44e8870f nginx:1.9 "nginx -g 'daemon off" About an hour ago Up 15 minutes 0.0.0.0:80->80/tcp, 443/tcp webserver
  6. edbe3f8b74d1 centos "/bin/bash" 5 hours ago Exited (137) About an hour ago big_gates
  7. 2d8e610dea5c centos "bash" 2 days ago Exited (137) About an hour ago berserk_payne
  8. 46bb9ad81a01 bash "bash" 2 days ago Exited (0) 2 days ago goofy_albattani
  9. ed0595a5ff0e bash "bash" 2 days ago Exited (0) 2 days ago stupefied_albattani
  10. 2004577f8837 bash "bash" 3 days ago Exited (0) 3 days ago mad_keller
  11. ef263c4b2603 bash "bash" 3 days ago Exited (0) 3 days ago tiny_keller
  12. 6e9df078978c bash "bash" 3 days ago Exited (127) 3 days ago grave_roentgen
  13. 0bd749bde8cc centos "/bin/bas" 3 days ago Created thirsty_nobel
  14. [root@zzk ~]#

利用docker start(restart、stop)启动,重启,停止容器
根据CONTAINER ID 重新启动

  1. [root@zzk ~]# docker start ddc24a6d00f0
  2. ddc24a6d00f0
  3. [root@zzk ~]# docker ps -a
  4. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  5. ddc24a6d00f0 centos "bash" 8 minutes ago Up 5 seconds modest_gates
  6. 7b71848a072a centos "/bin/echo 'Hello wor" 10 minutes ago Exited (0) 10 minutes ago big_bell
  7. c4cc44e8870f nginx:1.9 "nginx -g 'daemon off" About an hour ago Up 18 minutes 0.0.0.0:80->80/tcp, 443/tcp webserver
  8. edbe3f8b74d1 centos "/bin/bash" 5 hours ago Exited (137) About an hour ago big_gates
  9. 2d8e610dea5c centos "bash" 2 days ago Exited (137) About an hour ago berserk_payne
  10. 46bb9ad81a01 bash "bash" 2 days ago Exited (0) 2 days ago goofy_albattani
  11. ed0595a5ff0e bash "bash" 2 days ago Exited (0) 2 days ago stupefied_albattani
  12. 2004577f8837 bash "bash" 3 days ago Exited (0) 3 days ago mad_keller
  13. ef263c4b2603 bash "bash" 3 days ago Exited (0) 3 days ago tiny_keller
  14. 6e9df078978c bash "bash" 3 days ago Exited (127) 3 days ago grave_roentgen
  15. 0bd749bde8cc centos "/bin/bas" 3 days ago Created thirsty_nobel
  16. [root@zzk ~]#

重新附着在容器上(首先要启动该容器,再用attach重新附着该容器)

  1. [root@zzk ~]# docker start ddc24a6d00f0
  2. ddc24a6d00f0
  3. [root@zzk ~]# docker attach modest_gates
  4. [root@ddc24a6d00f0 /]#

查看容器内的进程

  1. [root@zzk ~]# docker top webserver
  2. UID PID PPID C STIME TTY TIME CMD
  3. root 48238 48026 0 14:57 ? 00:00:00 nginx: master process nginx -g daemon off;
  4. 104 48251 48238 0 14:57 ? 00:00:00 nginx: worker process
  5. [root@zzk ~]#

深入容器

docker ps 命令获取容器信息之外,我们还可以使用docke inspect来获取更多的容器信息,如下:

  1. [root@zzk ~]# docker inspect webserver
  2. [
  3. {
  4. "Id": "c4cc44e8870fae11943237e2eb7e41458dbc7593c300df2bc84d210f4f786d11",
  5. "Created": "2016-12-26T05:36:28.405796558Z",
  6. "Path": "nginx",
  7. "Args": [
  8. "-g",
  9. "daemon off;"
  10. ],
  11. "State": {
  12. "Status": "running",
  13. "Running": true,
  14. "Paused": false,
  15. "Restarting": false,
  16. "OOMKilled": false,
  17. "Dead": false,
  18. "Pid": 48238,
  19. "ExitCode": 0,
  20. "Error": "",
  21. "StartedAt": "2016-12-26T06:57:41.916606911Z",
  22. "FinishedAt": "2016-12-26T06:10:38.660640812Z"
  23. },
  24. "Image": "sha256:c8c29d842c09d6c61f537843808e01c0af4079e9e74079616f57dfcfa91d4e25",
  25. "ResolvConfPath": "/var/lib/docker/containers/c4cc44e8870fae11943237e2eb7e41458dbc7593c300df2bc84d210f4f786d11/resolv.conf",
  26. "HostnamePath": "/var/lib/docker/containers/c4cc44e8870fae11943237e2eb7e41458dbc7593c300df2bc84d210f4f786d11/hostname",
  27. "HostsPath": "/var/lib/docker/containers/c4cc44e8870fae11943237e2eb7e41458dbc7593c300df2bc84d210f4f786d11/hosts",
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注