$ docker run -d ubuntu /bin/sh -c "while true; do echo hello world; sleep 1; done"
cb30b87566d0550ec5f1232d148c5ffed6546c347889e58a6405579f2af73f2a
使用 -d 参数启动后会返回一个唯一的 id。此时输出结果可以用docker logs [container ID or NAMES] 查看。如果不使用 -d 参数。输出的结果 (STDOUT) 会打印到宿主机上面
通过 docker container ls 命令来查看容器信息。
$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
cb30b87566d0 ubuntu "/bin/sh -c 'while t…" 2 minutes ago Up 2 minutes goofy_mcclintock
要获取容器的输出信息,可以通过 docker container logs 命令。
$ docker container logs goofy_mcclintock
hello world
hello world
hello world
......
注: 容器是否会长久运行,是和 docker run 指定的命令有关,和 -d 参数无关。
$ docker container rm awesome_payne
awesome_payne
如果要删除一个运行中的容器,可以添加 -f 参数。Docker 会发送SIGKILL 信号给容器。
清理所有处于终止状态的容器用 docker container ls -a 命令可以查看所有已经创建的包括终止状态的容器,如果数量太多要一个个删除可能会很麻烦,用下面的命令可以清理掉所有处于终止状态的容器。
$ docker container prune
WARNING! This will remove all stopped containers.
Are you sure you want to continue? [y/N] y
Deleted Containers:
545f8f6d19286efae28307d06ed1acc034d07f109e907c01892471a6f89e772d
cb30b87566d0550ec5f1232d148c5ffed6546c347889e58a6405579f2af73f2a
...... 导出和导入容器 导出容器
如果要导出本地某个容器,可以使用 docker export 命令。
$ docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
16168d4b66b1 ubuntu "/bin/bash" 18 minutes ago Up 18 minutes happy_bardeen
$ docker export 16168d4b66b1 > ubuntu.tar
这样将导出容器快照到本地文件。 导入容器快照
可以使用 docker import 从容器快照文件中再导入为镜像,例如
$ cat ubuntu.tar | docker import - test/ubuntu:v1.0
sha256:91b174fec9ed55d7ebc3d2556499713705f40713458e8594efa114f261d7369a
$ docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
test/ubuntu v1.0 91b174fec9ed 10 seconds ago 69.8MB
ubuntu latest 735f80812f90 3 weeks ago 83.5MB
此外,也可以通过指定 URL 或者某个目录来导入,例如
$ docker import http://example.com/exampleimage.tgz example/imagerepo