创建容器
# docker run --name warp ubuntu
# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
75b5ae9f477a ubuntu "/bin/bash" 25 seconds ago Exited (0) 24 seconds ago warp
列出镜像和容器
# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu 22.04 58db3edaf2be 3 weeks ago 77.8MB
ubuntu latest 58db3edaf2be 3 weeks ago 77.8MB
列出正在运行的容器
# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
cd7b49132dab ubuntu "/bin/bash" 27 minutes ago Up About a minute 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp warp
# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
cd7b49132dab ubuntu "/bin/bash" 27 minutes ago Up About a minute 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp warp
# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
cd7b49132dab ubuntu "/bin/bash" 27 minutes ago Up 2 minutes 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp warp
删除镜像和容器
# docker image rm ubuntu:22.04
Untagged: ubuntu:22.04
# docker container rm warp
Error response from daemon: You cannot remove a running container cd7b49132dab2267b2e528cec3a9b1cc989fd1730a44ae328e60d478b3409174. Stop the container before attempting removal or force remove
无法删除正在运行的容器,-f强制删除
# docker container rm warp -f
warp
清理所有停止运行的容器
# docker container prune
WARNING! This will remove all stopped containers.
Are you sure you want to continue? [y/N] y
Total reclaimed space: 0B
分离与连接
使用以下命令创建容器后,容器将被启动并被分离,在后台运行bash,如果bash退出,容器也将退出
docker run --name warp -p 8080:8080 -it -d ubuntu
-i 让容器的标准输入保持打开
-t 让docker分配一个伪终端并绑定到容器的标准输入上
-d 分离到后台运行
使用 docker attach name
或 docker start -ai warp
即可进入容器,使用 ^P + ^Q 分离容器。