Docker运行容器前需要本地存在对应的镜像,如果镜像不存在本地,Docker会从镜像仓库下载(默认是Docker Hub)。
当利用docker run
来创建容器时,Docker在后台运行的标准操作包括:
- 检查本地是否存在指定的镜像,不存在就从公有仓库下载
- 利用镜像创建并启动一个容器
- 分配一个文件系统,并在只读的镜像层外面挂载一层可读写层
- 从宿主主机配置的网桥接口中桥接一个虚拟接口到容器中去
- 从地址池配置一个 ip 地址给容器
- 执行用户指定的应用程序
- 执行完毕后容器被终止
启动容器
启动容器有两种方式,一种是基于镜像新建一个容器并启动,另外一个是将在终止状态(stopped)的容器重新启动。
新建启动
所用的命令 docker run
。1
2docker@default:~$ docker container run centos /bin/echo 'hello world'
hello world
经常用的Options:-it –rm1
2
3docker@default:~$ docker run -it centos /bin/bash
[root@6d1e9498e298 /]# pwd
/
需要让Docker在后台运行而不是直接把执行命令的结果输出在当前宿主机下,可以通过添加-d
参数来实现。
终止的容器启动
直接将一个已经终止的容器启动运行, 命令docker container start
。1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19docker@default:~$ docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6d1e9498e298 centos "/bin/bash" 2 minutes ago Exited (130) 5 seconds ago hardcore_mclean
docker@default:~$ docker container start --help
Usage: docker container start [OPTIONS] CONTAINER [CONTAINER...]
Start one or more stopped containers
Options:
-a, --attach Attach STDOUT/STDERR and forward signals
--detach-keys string Override the key sequence for detaching a container
--help Print usage
-i, --interactive Attach container's STDIN
docker@default:~$ docker start 6d1e9498e298
6d1e9498e298
docker@default:~$ docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6d1e9498e298 centos "/bin/bash" 5 minutes ago Up 11 seconds hardcore_mclean
此外,docker restart
命令会将一个运行态的容器终止,然后再重新启动它。
终止容器
终止一个运行中的容器, 命令docker container stop
。1
2
3
4
5
6
7
8docker@default:~$ docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6d1e9498e298 centos "/bin/bash" 14 minutes ago Up 35 seconds hardcore_mclean
docker@default:~$ docker container stop 6d1e9498e298
6d1e9498e298
docker@default:~$ docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6d1e9498e298 centos "/bin/bash" 15 minutes ago Exited (137) 2 seconds ago hardcore_mclean
进入容器
docker container attach
命令或docker container exec
命令,推荐大家使用 docker exec 命令,因为docker container attach
命令exit,会导致容器的停止, 以docker container exec
命令进入就不会导致容器的停止。1
2
3
4
5
6
7
8
9
10
11
12
13
14
15docker@default:~$ docker container exec --help
Usage: docker container exec [OPTIONS] CONTAINER COMMAND [ARG...]
Run a command in a running container
Options:
-d, --detach Detached mode: run command in the background
--detach-keys string Override the key sequence for detaching a container
-e, --env list Set environment variables
--help Print usage
-i, --interactive Keep STDIN open even if not attached
--privileged Give extended privileges to the command
-t, --tty Allocate a pseudo-TTY
-u, --user string Username or UID (format: <name|uid>[:<group|gid>])
删除容器
可以使用docker container rm
来删除一个处于终止状态的容器。1
2
3
4
5
6
7
8
9
10
11docker@default:~$ docker container rm --help
Usage: docker container rm [OPTIONS] CONTAINER [CONTAINER...]
Remove one or more containers
Options:
-f, --force Force the removal of a running container (uses SIGKILL)
--help Print usage
-l, --link Remove the specified link
-v, --volumes Remove the volumes associated with the container
- 清理所有处于终止的容器
docker container prune
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16docker@default:~$ docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2db2042590a4 hello-world "/hello" About a minute ago Exited (0) About a minute ago admiring_volhard
6d1e9498e298 centos "/bin/bash" 20 hours ago Up 9 minutes hardcore_mclean
f14e316a8af9 centos "/bin/echo 'hello ..." 20 hours ago Exited (0) 20 hours ago blissful_swartz
docker@default:~$ docker container prune
WARNING! This will remove all stopped containers.
Are you sure you want to continue? [y/N] y
Deleted Containers:
2db2042590a4486e9a5a9c095d2bc06303f662fdd2fe077f3974d5032cf8a34c
f14e316a8af9643b350c233c54270d72c05abaf2ff654735234f26dc5fc5fdf8
Total reclaimed space: 0B
docker@default:~$ docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6d1e9498e298 centos "/bin/bash" 20 hours ago Up 10 minutes hardcore_mclean