Docker提供强大的命令行工具docker来管理你的容器,我们来看如何用它来管理容器,先查看容器管理帮助:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
[root@node1 ~]# docker container --help

Usage: docker container COMMAND

Manage containers

Options:
--help Print usage

Commands:
attach Attach to a running container
commit Create a new image from a container's changes
cp Copy files/folders between a container and the local filesystem
create Create a new container
diff Inspect changes to files or directories on a container's filesystem
exec Run a command in a running container
export Export a container's filesystem as a tar archive
inspect Display detailed information on one or more containers
kill Kill one or more running containers
logs Fetch the logs of a container
ls List containers
pause Pause all processes within one or more containers
port List port mappings or a specific mapping for the container
prune Remove all stopped containers
rename Rename a container
restart Restart one or more containers
rm Remove one or more containers
run Run a command in a new container
start Start one or more stopped containers
stats Display a live stream of container(s) resource usage statistics
stop Stop one or more running containers
top Display the running processes of a container
unpause Unpause all processes within one or more containers
update Update configuration of one or more containers
wait Block until one or more containers stop, then print their exit codes

Run 'docker container COMMAND --help' for more information on a command.

docker为了便捷操作和兼容以前版本的命令,docker container命令的大部分参数在以前是直接不需要container关键字的,现在依然有效,所以上述命令我们可以直接活力container关键字。参数作用见后面的详细描述,参数后面可接的子参数,可以通过docker container COMMAND --help来查询,下面我们只讲常用操作。

容器管理

1. 创建容器

拉取镜像(可选,若镜像不存在,docker run时也会自动拉取),此处以centos镜像为例

1
[root@node1 ~]# docker pull centos:latest

创建容器(交互式)

1
2
[root@node1 ~]# docker run -it centos:latest /bin/bash
[root@8e1ffd77d3ba /]#
  • -i:允许你对容器内的标准输入 (STDIN) 进行交互
  • -t:在新容器内指定一个tty终端
  • centos:latest:镜像名
  • /bin/bash:交互命令

上面即创建(运行)了一个centos的容器,运行命令为/bin/bash,此时已通过交互参数进入到容器中。退出可使用exit或Ctrl+d。

创建容器(后台式)

1
2
3
[root@node1 ~]# docker run -d --name test centos:latest tailf /var/log/lastlog
2886e49b88098325c98c7e2edf28aa6bf767795fd1a2dd411f5f0aa9d7333983
[root@node1 ~]#
  • -d:以后台服务(daemon)方式创建容器
  • –name:容器名

类似后台服务,你需要给容器一个可以不停执行的命令,此时容器会在后台运行,如果命令结束,容器也会退出并停止。你可以通过查看命令来找到你创建的容器。

2. 查看容器

查看正在运行的容器

1
2
3
[root@node1 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2886e49b8809 centos:latest "tailf /var/log/last…" About a minute ago Up About a minute test

查看所有容器

1
2
3
4
[root@node1 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8667cfc10391 centos:latest "tailf /var/log/mess" 4 seconds ago Exited (1) 3 seconds ago sleepy_poincare
2886e49b8809 centos:latest "tailf /var/log/last…" 2 minutes ago Up 2 minutes test

所有容器包含所有状态的容器,包括创建、运行、停止等。

查看容器日志

1
[root@node1 ~]# docker logs test

3. 停止容器

1
2
3
4
5
6
[root@node1 ~]# docker stop test
test
[root@node1 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8667cfc10391 centos:latest "tailf /var/log/mess" 2 minutes ago Exited (1) 2 minutes ago sleepy_poincare
2886e49b8809 centos:latest "tailf /var/log/last…" 4 minutes ago Exited (137) 32 seconds ago test

后面接容器名或容器ID都可以

4. 启动/重启容器

  • 启动
1
2
3
4
5
[root@node1 ~]# docker start test
test
[root@node1 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2886e49b8809 centos:latest "tailf /var/log/last…" 5 minutes ago Up 3 seconds test
  • 重启
1
2
3
4
5
[root@node1 ~]# docker restart test
test
[root@node1 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2886e49b8809 centos:latest "tailf /var/log/last…" 6 minutes ago Up 5 seconds test

5. 删除容器

docker默认只能删除停止状态的容器

1
2
3
4
5
6
7
8
[root@node1 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8667cfc10391 centos:latest "tailf /var/log/mess" 6 minutes ago Exited (1) 6 minutes ago sleepy_poincare
2886e49b8809 centos:latest "tailf /var/log/last…" 8 minutes ago Up 2 minutes test
[root@node1 ~]# docker rm sleepy_poincare
sleepy_poincare
[root@node1 ~]# docker rm test
Error response from daemon: You cannot remove a running container 2886e49b88098325c98c7e2edf28aa6bf767795fd1a2dd411f5f0aa9d7333983. Stop the container before attempting removal or force remove

可以先停止容器,再删除。

如果需要强制删除运行中的容器,直接加参数’-f’或’–force’即可

1
2
3
4
[root@node1 ~]# docker rm -f test
test
[root@centos-vm7 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

6. 其它常用操作

执行命令

  • 非交互式
1
2
3
4
5
6
7
8
[root@node1 ~]# docker exec test cat /etc/hosts
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.18.0.11 2886e49b8809
  • 交互式
1
2
[root@node1 ~]# docker exec -it test /bin/bash
2886e49b8809#

查看容器详细参数和信息

1
[root@node1 ~]# docker inspect test

会以json格式输出所有与容器相关的参数,因为过长此处不列。

查看容器内进程占用

1
[root@node1 ~]# docker top test

和top命令类似,输出进程详细信息

文件拷贝

在宿主机和容器之间拷贝文件或目录,以下列出两种方向的拷贝。

1
2
[root@node1 ~]# docker cp test:/etc/hosts /root/
[root@node1 ~]# docker cp /etc/hosts test:/root/

查看docker状态信息

1
[root@node1 ~]# docker info

如果命令输出正确,则可判定docker服务已正常启动。