启动容器
启动容器有两种方式,一种是基于镜像新建一个容器并启动,另外一个是将在终止状态(stopd)的容器重新启动。
新建并启动
命令 docker run 或者docker container run(Docker 1.13+)
例如,下面输出一个 hello world 之后终止容器
[root@localhost ~]# docker container run ubuntu:14.04 /bin/echo 'hello world' hello world
[root@localhost ~]# docker container run -t -i ubuntu:14.04 /bin/bash root@bb39955f2bc5:/#
启动已终止容器
docker container start [names or container id]
后台运行
用 -d 参数运行容器
[root@localhost ~]# docker container run ubuntu:17.10 /bin/sh -c "while true; do echo hello world; sleep 1; done" hello world hello world ...
[root@localhost ~]# docker container run -d ubuntu:17.10 /bin/sh -c "while true; do echo hello world; sleep 1; done" 4ab0ae66c4d3479d03d1b40181dd82f2e30a17dd4cb7dd8fc9dda18dc23fd979 [root@localhost ~]#
[root@localhost ~]# docker container ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 4ab0ae66c4d3 ubuntu:17.10 "/bin/sh -c 'while..." 34 seconds ago Up 33 seconds nifty_thompson
[root@localhost ~]# docker container logs nifty_thompson hello world hello world hello world hello world [root@localhost ~]# 或者 [root@localhost ~]# docker container logs -f nifty_thompson hello world hello world ...持续输出
终止容器
docker container stop [container id or names]
[root@localhost ~]# docker container ls CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 4ab0ae66c4d3 ubuntu:17.10 "/bin/sh -c 'while..." 3 minutes ago Up 3 minutes nifty_thompson [root@localhost ~]# docker container stop nifty_thompson nifty_thompson [root@localhost ~]#
进入容器
使用 -d 参数时,容器启动后会进入后台
进入容器可以使用命令 docker attach [container id or names] 或 docker exec [container id or names] 两种方法
[root@localhost ~]# docker container run -dit ubuntu 154f174c96ee5629ad798c5031b2491628141ffe0c5be91a1de7d5412c57e7c8 [root@localhost ~]# docker container ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 154f174c96ee ubuntu "/bin/bash" 7 seconds ago Up 6 seconds affectionate_poincare [root@localhost ~]# docker container attach 154f root@154f174c96ee:/# root@154f174c96ee:/# exit exit [root@localhost ~]# docker container ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [root@localhost ~]#
docker attach 命令进入容器,如果从stdin中exit, 会导致容器的停止;用docker exec 进入容器,exit则不会停止
[root@localhost ~]# docker container run -dit ubuntu b5961768d580852a6f9c73c4ad81f1d85843f83bfb9ed04315be6f02afb92ab2 [root@localhost ~]# docker container ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b5961768d580 ubuntu "/bin/bash" 7 seconds ago Up 7 seconds kind_swirles [root@localhost ~]# docker exec -it b596 bash root@b5961768d580:/# exit exit [root@localhost ~]# docker container ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b5961768d580 ubuntu "/bin/bash" About a minute ago Up About a minute kind_swirles [root@localhost ~]#
导出和导入
docker export [docker container export]
docker import [docker image import]
docker export [cotainer id] > ubuntu.tar cat ubuntu.tar | docker import - test/ubuntu:v1.0
删除容器
docker rm [names or container id]
[root@localhost docker]# docker rm kind_swirles
kind_swirles
docker ps -a
清理所有终止状态的容器 docker container prune