目录
容器(Containers)
每一个Docker容器都是独立和安全的应用平台,它和文件夹类似,一个Docker容器包含了某个应用运行所需要的所有环境。每一个Docker容器都是Docker镜像创建的,Docker 容器可以运行、开始、停止、移动和删除。
1. 查看容器信息
1.1 容器基础信息
语法结构:
docker ps
[root@localhost ~]# docker ps #查看正在运行的容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@localhost ~]# docker ps -a #查看所有容器
[root@localhost ~]# docker ps -a -q #仅仅查看所有容器的ID
[root@localhost ~]# docker ps -l #输出最近一次启动的容器
输出结果说明:
- CONTAINER:容器ID
- IMAGE:容器使用的镜像
- COMMAND:启动容器时运行的命令
- CREATED:容器被创建的时间
- STATUS:容器7种状态之一
- Created:已经被创建
- Paused:暂停
- Exited:停止
- Dead:死亡
- Running/Up:运行中
- Restarting:重启
- Removing:迁移中
- PORTS:容器端口和连接类型(TCP/UDP)
- NAMES:容器名称,不指定则自动生成
1.2 容器底层信息
返回JSON文件记录容器配置和状态信息。
语法结构:
docker inspect 容器名/容器ID
[root@localhost ~]# docker inspect ubuntu-v1|grep "IPAddress" #获取容器IP
"SecondaryIPAddresses": null,
"IPAddress": "172.17.0.3",
"IPAddress": "172.17.0.3",
2. 运行容器
语法结构:
docker run ...
2.1 运行Hello World
使用ubuntu:20.04镜像运行一个容器,并在容器内执行/bin/echo "Hello world",结果输出Hello world:
[root@localhost ~]# docker run ubuntu:20.04 /bin/echo "Hello world"
Unable to find image 'ubuntu:20.04' locally
20.04: Pulling from library/ubuntu
Digest: sha256:626ffe58f6e7566e00254b638eb7e0f3b11d4da9675088f4781a50ae288f3322
Status: Downloaded newer image for ubuntu:20.04
Hello world
命令参数解释:
- ubuntu:20.4:指定运行的镜像,如果本地没有该镜像将在Docker官方镜像仓库下载公共镜像
- /bin/echo:在启动的容器中执行Hello world
2.2 运行交互式容器
运行并进入容器。
[root@localhost ~]# docker run -it ubuntu:20.04 /bin/sh #指定/bin/sh登陆shell进入容器
# ls #查看容器中/下的文件
bin boot dev etc home lib lib32 lib64 libx32 media mnt opt proc root run sbin srv sys tmp usr var
# exit #退出容器
[root@localhost ~]# docker run -it ubuntu:20.04 /bin/bash #指定/bin/bash登陆shell进入容器
root@6220df97dbf0:/# exit
[root@localhost ~]# docker run -it ubuntu:20.04 #不指定登陆shell进入容器,默认使用/bin/bash
root@cdb5c1955e18:/# exit
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
cdb5c1955e18 ubuntu:20.04 "bash" 32 seconds ago Exited (127) 11 seconds ago intelligent_ishizaka
6220df97dbf0 ubuntu:20.04 "/bin/bash" 8 minutes ago Exited (0) 8 minutes ago stoic_haibt
78901734a00b ubuntu:20.04 "/bin/sh" 9 minutes ago Exited (130) 8 minutes ago optimistic_dirac
8d6d2390255a ubuntu:20.04 "/bin/echo 'Hello wo…" 30 minutes ago Exited (0) 30 minutes ago inspiring_allen
命令参数解释:
- -it:提供交互式伪输入终端
退出容器:
- 运行exit命令
- 使用CTRL+D
2.3 后台运行容器
运行容器不会在终端打印命令提示符,而是在后端运行,可使用docker ps查看容器运行状态。
1.后台运行未定义名称的容器:
[root@localhost ~]# docker run -itd ubuntu /bin/bash #后台运行容器
2a6cd654177f31012f79a72e1a864d0794184aec8db9df83c40f97712eedf95e
[root@localhost ~]# docker ps #查看容器运行
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2a6cd654177f ubuntu "/bin/bash" 12 seconds ago Up 10 seconds compassionate_black
命令参数解释:
- -d:指定容器在后台运行
2.后台运行容器名为ubuntu-v1的容器:
[root@localhost ~]# docker run --name ubuntu-v1 -itd ubuntu /bin/sh
573e981db760c1a96e232b521936bb26e2d8c49550cbab8ab640e056776db3f3
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
573e981db760 ubuntu "/bin/sh" 18 seconds ago Up 17 seconds ubuntu-v1
2a6cd654177f ubuntu "/bin/bash" 8 minutes ago Up 8 minutes compassionate_black
命令参数解释:
- --name:指定容器名称
3. 停止容器
停止正在运行的容器。
语法结构:
docker stop 容器名/容器ID
[root@localhost ~]# docker stop ubuntu-v1
ubuntu-v1
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
573e981db760 ubuntu "/bin/sh" 22 minutes ago Exited (137) 17 seconds ago ubuntu-v1
4. 启动容器
启动已经停止的容器。
语法结构:
docker start 容器名/容器ID
[root@localhost ~]# docker start ubuntu-v1
ubuntu-v1
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
573e981db760 ubuntu "/bin/sh" 27 minutes ago Up 4 seconds ubuntu-v1
5. 重启容器
停止并重新启动容器。
语法结构:
docker restart 容器名/容器ID
[root@localhost ~]# docker restart 573e981db760
573e981db760
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
573e981db760 ubuntu "/bin/sh" 29 minutes ago Up 8 seconds ubuntu-v1
6. 进入容器
进入正在后台运行的容器。
语法结构:
docker exec -it 容器名/容器ID 指定shell
[root@localhost ~]# docker exec -it ubuntu-v1 /bin/bash
root@573e981db760:/# exit
命令参数解释:
- exec:进入容器,退出后不会停止容器运行
7. 删除容器
语法结构:
docker rm 容器名/容器ID
7.1 删除单个容器
[root@localhost ~]# docker ps #查看正在运行的容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
573e981db760 ubuntu "/bin/sh" 52 minutes ago Up 23 minutes ubuntu-v1
2a6cd654177f ubuntu "/bin/bash" About an hour ago Up About an hour compassionate_black
[root@localhost ~]# docker rm ubuntu-v1 #rm直接删除正在运行的容器失败
Error response from daemon: You cannot remove a running container 573e981db760c1a96e232b521936bb26e2d8c49550cbab8ab640e056776db3f3. Stop the container before attempting removal or force remove
[root@localhost ~]# docker rm -f ubuntu-v1 #添加-f参数强制删除正在运行的容器
ubuntu-v1
[root@localhost ~]# docker rm 2a6cd654177f #删除正在运行的容器失败
Error response from daemon: You cannot remove a running container 2a6cd654177f31012f79a72e1a864d0794184aec8db9df83c40f97712eedf95e. Stop the container before attempting removal or force remove
[root@localhost ~]# docker stop 2a6cd654177f #先停止容器
2a6cd654177f
[root@localhost ~]# docker rm 2a6cd654177f #再使用rm删除容器
2a6cd654177f
命令参数解释:
- rm:只能删除停止运行的容器
- -f:强制删除正在运行的容器
7.2 删除所有容器
[root@localhost ~]# docker ps -a #查看所有容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
cdb5c1955e18 ubuntu:20.04 "bash" 2 days ago Exited (127) 2 days ago intelligent_ishizaka
6220df97dbf0 ubuntu:20.04 "/bin/bash" 2 days ago Exited (0) 2 days ago stoic_haibt
78901734a00b ubuntu:20.04 "/bin/sh" 2 days ago Exited (137) 2 days ago optimistic_dirac
8d6d2390255a ubuntu:20.04 "/bin/echo 'Hello wo…" 2 days ago Exited (0) 2 days ago inspiring_allen
[root@localhost ~]# docker rm $(docker ps -a -q) #先获取所有容器的ID,再删除所有容器
cdb5c1955e18
6220df97dbf0
78901734a00b
8d6d2390255a
7.3 删除停止状态的容器
删除所有处于Exited状态的容器。
语法结构:
docker container prune
[root@localhost ~]# docker ps -a #有两个处于停止状态的容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3432dcbe7ac6 ubuntu "bash" 45 seconds ago Up 44 seconds epic_feynman
11b6793c0cc7 ubuntu "bash" About a minute ago Exited (0) 8 seconds ago exciting_poincare
109a53d0ca0d ubuntu "bash" About a minute ago Exited (0) 33 seconds ago elegant_lumiere
[root@localhost ~]# docker container prune
WARNING! This will remove all stopped containers.
Are you sure you want to continue? [y/N] y
Deleted Containers:
11b6793c0cc7d1d8927c49b0379ba5fbaac08f2dfccb760b9ef1dffef0f7032b
109a53d0ca0dd5091c44fae83fed11fe87a1976edced48ed2a3ba4c6a3d27590
Total reclaimed space: 0B
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3432dcbe7ac6 ubuntu "bash" About a minute ago Up About a minute epic_feynman
8. 导入和导出容器
8.1 导出容器
将正在运行的容器导出为tar包的镜像文件。
语法结构:
docker export 容器名/容器ID > 命名.tar
[root@localhost ~]# docker ps -a #查看本地是否存在容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@localhost ~]# docker run -itd --name ubuntu-v1 ubuntu #运行容器ubuntu-v1
2a345f1b4e7dbca05d20e115a0b47513cec01c12c51ca4a15db6e4d99c155a07
[root@localhost ~]# docker export ubuntu-v1 > ubuntu-v1.tar #导出ubuntu-v1镜像到当前目录,并命名为ubuntu-v1.tar
[root@localhost ~]# ls #查看当前目录导出成功
anaconda-ks.cfg ubuntu-v1.tar
命令参数解释:
- export:相当于容器快照,容器快照文件将丢失所有历史记录和元数据信息
8.2 导入容器
将容器快照导入到新的镜像中。
语法结构:
cat 容器快照 |docker import - 新镜像名
[root@localhost ~]# cat ubuntu-v1.tar |docker import - ubuntu:v2
sha256:b0b841662b3989cb447c9a1917334b1eb8f1afc54df5e3ea2367e1e7427fad7e
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu v2 b0b841662b39 47 seconds ago 72.8MB