多数命令都有新旧两种格式,命令向后兼容,两种格式都能使用。
docker version
client 和 server 版本最好一致
server 也叫 engine,在 mac 或 linux 上也叫 daemon
docker info
显示 docker engin 的绝大多数配置
docker
查看所有 docker 命令
docker container run ...........
docker run ...........
(old way)
启动一个新的容器
docker container ls
docker ps
(old way)
列出正在运行的容器
docker container stop container_id
docker stop container_id
(old way)
停止容器进程,并不删除
container_id
不必写全,只写前几位独一无二能让docker 识别是哪一个容器就行。
docker container ls -a
列出全部正在运行和已经停止的容器
run
vs. start
docker container run ...........
启动一个新容器
docker container start ...........
启动一个存在的已经停止的容器
--name
指定容器名称,不指定则由docker 随机生成。```bash
docker container run --publish 80:80 --detach --name webserver nginx
```
此命令启动一个名称为 `webserver` 的 nginx 容器,
docker container logs container_id_or_name
docker logs container_id_or_name
(old way)
显示特定容器的 log
docker container top container_id_or_name
显示容器内的进程
docker container --help
查看可以应用于容器的命令
C:\Users\yanchun>docker container --help
Usage: docker container COMMAND
Manage containers
Commands:
attach Attach local standard input, output, and error streams 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 container rm id_1 id_2 id_3 .....
docker rm id_1 id_2 id_3 .....
(old way)
删除一个或多个容器,但是,出于安全的考虑,不允许删除正在运行的容器,所以下面的命令,有一个容器由于正在运行,删除出错:
C:\Users\yanchun>docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b7bea902ff8f nginx "/docker-entrypoint.…" 21 minutes ago Up 21 minutes 0.0.0.0:80->80/tcp webserver
946f7e643cdf nginx "/docker-entrypoint.…" About an hour ago Exited (0) About an hour ago agitated_benz
d7d47384102d nginx "/docker-entrypoint.…" 2 hours ago Exited (0) About an hour ago sharp_kirch
1ad86b3be23e redis "docker-entrypoint.s…" 6 days ago Exited (255) 4 hours ago 0.0.0.0:6379->6379/tcp redis-go-chat
C:\Users\yanchun>docker container rm b7 94 d7 1a
94
d7
1a
Error response from daemon: You cannot remove a running container b7bea902ff8fca1ea8059f1841160637a6acf9536be563e9118eaab2dd10929d. Stop the container before attempting removal or force remove
C:\Users\yanchun>docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b7bea902ff8f nginx "/docker-entrypoint.…" 22 minutes ago Up 22 minutes 0.0.0.0:80->80/tcp webserver
C:\Users\yanchun>
要删除这个正在运行的容器,可以分两步,先用停止,再删除。
但是也可以使用 -f
选项强制一步删除:
C:\Users\yanchun>docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b7bea902ff8f nginx "/docker-entrypoint.…" 25 minutes ago Up 25 minutes 0.0.0.0:80->80/tcp webserver
C:\Users\yanchun>docker container rm -f b7
b7
C:\Users\yanchun>docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
C:\Users\yanchun>