Home | 简体中文 | 繁体中文 | 杂文 | Search | ITEYE 博客 | OSChina 博客 | Facebook | Linkedin | 知乎专栏 | Email

161.3. 容器

161.3.1. 启动与终止容器

$ sudo docker run ubuntu:14.10 /bin/echo 'Hello world'
Hello world			
			

进入BASH

$ sudo docker run -t -i ubuntu:14.10 /bin/bash
root@f8c7b2afff14:/# 			
			

start / stop / restart

sudo docker start silly_bohr
silly_bohr

$ sudo docker stop silly_bohr
silly_bohr

$ sudo docker restart silly_bohr
silly_bohr
			

守护进程运行

$ sudo docker run -d ubuntu:14.10 /bin/sh -c "while true; do echo hello world; sleep 1; done"
4cdbb75eeabf3f1ea87bec91accdf5211639d0895e94ab94ffa1d55fb7f62e2a
			

通过 docker ps 命令来查看容器信息

$ sudo docker ps
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS              PORTS               NAMES
4cdbb75eeabf        ubuntu:14.10        "/bin/sh -c 'while t   30 seconds ago      Up 28 seconds                           drunk_rosalind 
			

要获取容器的输出信息,可以通过 docker logs 命令。

$ sudo docker logs insane_babbage			
			

注意:守护进程在后台运行,所以无输出,只能通过docker logs 命令查看

161.3.2. 进入容器

$ sudo docker run -idt ubuntu:14.10
793f9805620d7e10564e0778c388640cb73b6a1aec663bf468904d72a4f219f2

$ sudo docker ps 
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
793f9805620d        ubuntu:14.10        "/bin/bash"         5 seconds ago       Up 4 seconds                            mad_elion           

$ sudo docker attach mad_elion 
root@793f9805620d:/# ls
bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
			

161.3.3. 导出和导入容器

			
$ sudo docker export 7691a814370e > ubuntu.tar
						
			

			
$ cat ubuntu.tar | sudo docker import - test/ubuntu:v1.0
						
			

指定 URL 或者某个目录来导入,例如

$sudo docker import http://example.com/exampleimage.tgz example/imagerepo			
			

161.3.4. 删除容器

使用 docker rm 来删除一个处于终止状态的容器。

$ sudo docker ps -a
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS                      PORTS               NAMES
f8c7b2afff14        ubuntu:14.10        "/bin/bash"            14 minutes ago      Exited (0) 2 minutes ago                        agitated_fermat     
0abd2e5fc251        ubuntu:14.10        "/bin/echo 'Hello wo   15 minutes ago      Exited (0) 15 minutes ago                       clever_kowalevski 

$ sudo docker rm clever_kowalevski
clever_kowalevski

$ sudo docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES
f8c7b2afff14        ubuntu:14.10        "/bin/bash"         16 minutes ago      Exited (0) 5 minutes ago                       agitated_fermat     			
			
comments powered by Disqus