docker ps 命令可以列出正在运行的容器,加 -a 参数可以列出所有的容器,包括停止运行的容器。
1 2 3 4
$ docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a6548f35ae74 ubuntu "/bin/bash" 8 minutes ago Exited (0) 3 seconds ago goofy_shirley 7b7d1d2c4fd4 ubuntu "/bin/bash" 33 minutes ago Exited (0) 32 minutes ago naughty_turing
$ docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 081d0ff60f8b ubuntu "/bin/bash" 17 seconds ago Exited (0) 12 seconds ago new_container
容器的命名唯一,试图创建两个名字相同的容器会报错。
重启容器
重新启动已停止的容器:
1 2 3 4 5 6
$ docker start test test
$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a6548f35ae74 ubuntu "/bin/bash" About an hour ago Up 5 seconds test
$ docker run --name daemon_dave -d ubuntu /bin/sh -c "while true; do echo hello world; sleep 1; done" 87b0002a9aff201d54511bba95a5fc6c7f7772f40a4a384ff82c5300b607605d
-d 参数将创建一个后台运行的容器。
1 2 3
$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 87b0002a9aff ubuntu "/bin/sh -c 'while tr" About a minute ago Up About a minute daemon_dave
通过 docker logs 命令可以获取容器的日志:
1 2 3 4
$ docker logs daemon_dave hello world hello world hello world
监控并格式化输出日志:
1 2 3 4 5 6
$ docker logs -f -t daemon_dave 2016-09-30T03:55:26.662176379Z hello world 2016-09-30T03:55:27.663102063Z hello world 2016-09-30T03:55:28.664941891Z hello world 2016-09-30T03:55:29.666235163Z hello world ...