docker/docker-镜像容器管理.md
2025-04-04 17:31:40 +08:00

308 lines
7.6 KiB
Markdown
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<h2><center>Docker 镜像容器管理</center></h2>
------
## 一:镜像管理
### 1. 搜索镜像
这种方法只能用于官方镜像库
```bash
[root@docker ~]# docker search centos
```
按星级搜索镜像,例如查找 star 数至少为 100 的镜像,默认不加 s 选项找出所有相关 ubuntu 镜像
```bash
[root@docker ~]# docker search ubuntu -f stars=100
```
### 2. 拉取镜像
```bash
[root@docker ~]# docker pull centos
```
### 3. 查看本地镜像
```bash
[root@docker ~]# docker images
```
### 4. 查看镜像详情
```bash
[root@docker ~]# docker image inspect 镜像id
```
只查看所有镜像的id
```bash
[root@docker ~]# docker images -q
```
查看镜像制作的过程相当于Dockerfile
```bash
[root@docker ~]# docker history centos:latest
IMAGE CREATED CREATED BY SIZE COMMENT
5d0da3dc9764 3 years ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B
<missing> 3 years ago /bin/sh -c #(nop) LABEL org.label-schema.sc… 0B
<missing> 3 years ago /bin/sh -c #(nop) ADD file:805cb5e15fb6e0bb0… 231MB
```
### 5. 删除镜像
删除一个或多个多个之间用空格隔开可以使用镜像名称或id
```bash
[root@docker ~]# docker rmi redis:latest
```
**强制删除**
如果镜像正在被使用,可用--force强制删除
```bash
[root@docker ~]# docker rmi mysql:5.7 --force
```
**删除所有镜像**
```bash
[root@docker ~]# docker rmi $(docker images -q)
```
### 6. 给镜像打个 tag
```bash
[root@docker ~]# docker tag centos:latest centos:v7
```
## 二:容器管理
### 1. 创建新容器但不启动
```bash
[root@docker ~]# docker create --name="centos-v7" -it centos:latest /bin/bash
```
### 2. 创建并运行一个新容器
```bash
[root@docker ~]# docker run -it -d --name="mysql-v5.7" --restart=always mysql:5.7 /bin/bash
```
语法格式:
```shell
docker run [选项] <镜像名> [命令]
选项:
-v文件映射格式为主机目录容器目录
-d: 后台运行容器并返回容器ID
-i: 以交互模式运行容器,通常与 -t 同时使用
-p: 端口映射,格式为:主机(宿主)端口:容器端口
-P: 自动将容器所有暴露的端口映射到宿主机随机端口
-t: 为容器重新分配一个伪输入终端,通常与 -i 同时使用
-u: 以指定用户身份运行容器
-h: "mars": 指定容器的hostname
-e: username="ritchie": 设置环境变量
--name="nginx-lb": 为容器指定一个名称;
--dns 8.8.8.8: 指定容器使用的DNS服务器默认和宿主一致
--dns-search example.com: 指定容器DNS搜索域名默认和宿主一致
--restart=always: 容器随docker engine自启动因为重启docker时默认容器都会被关闭,也适用于create子命令
--cpuset-cpus="0-2" or --cpuset-cpus="0,1,2": 绑定容器到指定CPU运行
--privileged 以特权模式运行
```
### 3. 查看容器
只查看运行状态的容器
```bash
[root@docker ~]# docker ps
```
查看所有容器
```bash
[root@docker ~]# docker ps -a
```
只查看容器`id`
```bash
[root@docker ~]# docker ps -a -q
```
列出最近一次启动的容器
```bash
[root@docker ~]# docker ps -l
```
**查看容器详细信息**
用于查看容器的配置信息,包含容器名、环境变量、运行命令、主机配置、网络配置和数据卷配置等。
```bash
[root@docker ~]# docker inspect 9777
```
比如容器里在安装ip或ifconfig命令之前查看网卡IP显示容器IP地址和端口号如果输出是空的说明没有配置IP地址不同的Docker容器可以通过此IP地址互相访问
```bash
[root@docker ~]# docker inspect --format='{{.NetworkSettings.IPAddress}}' 9777
```
### 4. 启动容器
```bash
[root@docker ~]# docker start 容器名称
重启
[root@docker ~]# docker restart 容器名称
```
### 5. 关闭容器
```bash
[root@docker ~]# docker stop 容器名称
[root@docker ~]# docker kill 容器名称
关闭所有running状态的容器
[root@docker ~]# docker kill $(docker ps -q)
```
### 6. 删除容器
```bash
[root@docker ~]# docker rm 容器id或名称
要删除一个运行中的容器,添加 -f 参数
根据状态删除所有容器
[root@docker ~]# docker rm $(docker ps -qf status=exited)
```
### 7. 暂停、恢复容器
```bash
暂停容器
[root@docker ~]# docker pause name
恢复容器
[root@docker ~]# docker unpause name
```
### 8. 修改容器名称
```bash
[root@docker ~]# docker rename mytest test
```
### 9. 动态显示容器的资源消耗情况
```bash
[root@docker ~]# docker stats 容器名称
```
### 10. 输出容器端口与宿主机端口的映射情况
```bash
[root@docker ~]# docker port 容器名称
```
### 11. 连接容器
**attach**
```bash
[root@docker ~]# docker attach 容器id
前提是容器创建时必须指定了交互shell
```
**exec**
通过exec命令可以创建两种任务后台型任务和交互型任务
交互型任务
```bash
[root@docker ~]# docker exec -it 容器id /bin/bash
```
后台型任务
```bash
[root@docker ~]# docker exec 容器id touch /testfile
```
### 12. 断开与容器的连接且关闭容器
```bash
[root@96d1f135e795 /]# exit
快捷键ctrl+d
如果只想断开和容器的连接而不关闭容器可使用快捷键ctrl+p+q
```
### 13. 监控容器运行
**logs**
可以通过使用docker logs命令来查看容器的运行日志其中--tail选项可以指定查看最后几条日志而-t选项则可以对日志条目附加时间戳。使用-f选项可以跟踪日志的输出直到手动停止。
```bash
[root@docker ~]# docker logs App_Container //不同终端操作
[root@docker ~]# docker logs -f App_Container
[root@docker ~]# docker run -itd --name mytest docker.io/centos /bin/sh -c "while true; do echo hello world; sleep 2; done"
37738fe3d6f9ef26152cb25018df9528a89e7a07355493020e72f147a291cd17
[root@docker ~]# docker logs mytest
hello world
hello world
docker run -itd --name mytest 298ec /bin/sh -c "while true; do echo hello world; sleep 2; done"
```
**top**
显示一个运行的容器里面的进程信息
```bash
[root@docker ~]# docker top mysql-v5.7
```
**events**
实时输出Docker服务器端的事件包括容器的创建启动关闭等
```bash
[root@docker ~]# docker start mysql-v5.7
[root@docker ~]# docker events //不同终端操作
2025-03-31T20:10:17.222402344+08:00 network connect e83d539b24e4feb53840b16cc3213ccb74fc660de2540d3ce60036df2f4177cf (container=9777c67416eecb90140a02ddacf7c1c8e5f6514553580f4a10ab7e2946cb82c3, name=bridge, type=bridge)
2025-03-31T20:10:17.223307361+08:00 volume mount 4de49ba26655f7c8eed55cf8b539ddc409b784f831d1f642e554bf2b7d31b581 (container=9777c67416eecb90140a02ddacf7c1c8e5f6514553580f4a10ab7e2946cb82c3, destination=/var/lib/mysql, driver=local, propagation=, read/write=true)
2025-03-31T20:10:17.370632114+08:00 container start 9777c67416eecb90140a02ddacf7c1c8e5f6514553580f4a10ab7e2946cb82c3 (image=mysql:5.7, name=mysql-v5.7)
```
**waitX**
执行此命令后,该命令会"hang"在当前终端,直到容器停止,此时,会打印出容器的退出码
```bash
[root@docker ~]# docker wait 容器id
```
**diff**
查看容器内发生改变的文件以centos-v7容器为例
```bash
[root@96d1f135e795 /]# touch c.txt
[root@96d1f135e795 /]# exit
[root@docker ~]# docker diff centos-v7
C /root
A /root/.bash_history
A /c.txt
```