@zhaikun
2016-12-28T10:16:28.000000Z
字数 3623
阅读 1238
docker
启动容器有两种方式,一种是基于镜像新建一个容器并启动,另外一个是将在终止状态(stopped)的容器重新启动。
因为 Docker 的容器实在太轻量级了,很多时候用户都是随时删除和新创建容器。
所需要的命令主要为 docker run
。
例如,下面的命令输出一个 “Hello World”,之后终止容器。
[root@zzk ~]# docker run -it centos /bin/echo "Hello world"
Hello world
[root@zzk ~]#
这跟在本地直接执行 /bin/echo 'hello world'
几乎感觉不出任何区别。
下面的命令则启动一个 bash 终端,允许用户进行交互。
[root@zzk ~]# docker run -it centos bash
[root@ddc24a6d00f0 /]#
其中,-t 选项让Docker分配一个伪终端(pseudo-tty)并绑定到容器的标准输入上, -i 则让容器的标准输入保持打开。
当利用 docker run
来创建容器时,Docker 在后台运行的标准操作包括:
可以利用 docker ps -a
命令查看当前系统中容器的列表。
[root@zzk ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ddc24a6d00f0 centos "bash" 5 minutes ago Exited (0) 5 seconds ago modest_gates
7b71848a072a centos "/bin/echo 'Hello wor" 7 minutes ago Exited (0) 7 minutes ago big_bell
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
edbe3f8b74d1 centos "/bin/bash" 5 hours ago Exited (137) About an hour ago big_gates
2d8e610dea5c centos "bash" 2 days ago Exited (137) About an hour ago berserk_payne
46bb9ad81a01 bash "bash" 2 days ago Exited (0) 2 days ago goofy_albattani
ed0595a5ff0e bash "bash" 2 days ago Exited (0) 2 days ago stupefied_albattani
2004577f8837 bash "bash" 3 days ago Exited (0) 3 days ago mad_keller
ef263c4b2603 bash "bash" 3 days ago Exited (0) 3 days ago tiny_keller
6e9df078978c bash "bash" 3 days ago Exited (127) 3 days ago grave_roentgen
0bd749bde8cc centos "/bin/bas" 3 days ago Created thirsty_nobel
[root@zzk ~]#
利用docker start(restart、stop)
启动,重启,停止容器
根据CONTAINER ID 重新启动
[root@zzk ~]# docker start ddc24a6d00f0
ddc24a6d00f0
[root@zzk ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ddc24a6d00f0 centos "bash" 8 minutes ago Up 5 seconds modest_gates
7b71848a072a centos "/bin/echo 'Hello wor" 10 minutes ago Exited (0) 10 minutes ago big_bell
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
edbe3f8b74d1 centos "/bin/bash" 5 hours ago Exited (137) About an hour ago big_gates
2d8e610dea5c centos "bash" 2 days ago Exited (137) About an hour ago berserk_payne
46bb9ad81a01 bash "bash" 2 days ago Exited (0) 2 days ago goofy_albattani
ed0595a5ff0e bash "bash" 2 days ago Exited (0) 2 days ago stupefied_albattani
2004577f8837 bash "bash" 3 days ago Exited (0) 3 days ago mad_keller
ef263c4b2603 bash "bash" 3 days ago Exited (0) 3 days ago tiny_keller
6e9df078978c bash "bash" 3 days ago Exited (127) 3 days ago grave_roentgen
0bd749bde8cc centos "/bin/bas" 3 days ago Created thirsty_nobel
[root@zzk ~]#
重新附着在容器上(首先要启动该容器,再用attach重新附着该容器)
[root@zzk ~]# docker start ddc24a6d00f0
ddc24a6d00f0
[root@zzk ~]# docker attach modest_gates
[root@ddc24a6d00f0 /]#
[root@zzk ~]# docker top webserver
UID PID PPID C STIME TTY TIME CMD
root 48238 48026 0 14:57 ? 00:00:00 nginx: master process nginx -g daemon off;
104 48251 48238 0 14:57 ? 00:00:00 nginx: worker process
[root@zzk ~]#
除docker ps
命令获取容器信息之外,我们还可以使用docke inspect
来获取更多的容器信息,如下:
[root@zzk ~]# docker inspect webserver
[
{
"Id": "c4cc44e8870fae11943237e2eb7e41458dbc7593c300df2bc84d210f4f786d11",
"Created": "2016-12-26T05:36:28.405796558Z",
"Path": "nginx",
"Args": [
"-g",
"daemon off;"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 48238,
"ExitCode": 0,
"Error": "",
"StartedAt": "2016-12-26T06:57:41.916606911Z",
"FinishedAt": "2016-12-26T06:10:38.660640812Z"
},
"Image": "sha256:c8c29d842c09d6c61f537843808e01c0af4079e9e74079616f57dfcfa91d4e25",
"ResolvConfPath": "/var/lib/docker/containers/c4cc44e8870fae11943237e2eb7e41458dbc7593c300df2bc84d210f4f786d11/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/c4cc44e8870fae11943237e2eb7e41458dbc7593c300df2bc84d210f4f786d11/hostname",
"HostsPath": "/var/lib/docker/containers/c4cc44e8870fae11943237e2eb7e41458dbc7593c300df2bc84d210f4f786d11/hosts",