文章目录
- Docker
- Ubuntu 下 docker 安装
- 安装docker
- 运行docker
- Docker的常用命令
- 帮助命令
- 镜像命令
- 容器命令
- 其他常用命令
- 小结
- 分层理解
- 一、Docker镜像的分层结构
- 二、UnionFS与镜像分层
- 三、镜像层的具体内容
- 四、镜像分层的好处
- 五、容器层与镜像层的关系
- 如何提交一个自己的镜像
Docker
Ubuntu 下 docker 安装
安装docker
1.创建目录
mkdir -m 0775 -p /usr/share/keyrings/
2.下载GPG密钥
curl -fsSL https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
3.添加Docker的APT源
echo "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
4.更新APT包索引并安装Docker
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin -y
运行docker
#配置加载
sudo systemctl daemon-reload
#启动服务
sudo systemctl start docker
#开启启动
sudo systemctl enable docker
#查看服务状态
sudo systemctl status docker
配置镜像加速器
vim /etc/docker/daemon.json
源
{
"registry-mirrors": [
"https://docker.anyhub.us.kg",
"https://dockerhub.jobcher.com",
"https://dockerhub.icu",
"https://docker.m.daocloud.io",
"https://dockerhub.timeweb.cloud",
"https://huecker.io"
]
}
sudo systemctl daemon-reload
sudo systemctl restart docker
Docker的常用命令
帮助命令
docker version # 显示docker版本
docker info # 显示系统信息
docker 命令 --help # 用法帮助文档
帮助文档的地址 :https://docs.docker.com/enige/reference/commandline/
地址:docker | Docker Docs
镜像命令
docker images 查看本地镜像
root@iZbp140vcie2ktlx79vrqjZ:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 c20987f18b13 2 years ago 448MB
centos latest 5d0da3dc9764 3 years ago 231MB
REPOSITORY #镜像的仓库源
TAG #镜像的标签
IMAGE ID #镜像的ID
CREATED #镜像的创建时间
SIZE #镜像的大小
可选项
-a, --all Show all images (default hides intermediate images)
-q, --quiet Only show image IDs
docker 搜索命令
docker search
root@iZbp140vcie2ktlx79vrqjZ:~# docker search --help
Usage: docker search [OPTIONS] TERM
Search Docker Hub for images
Options:
-f, --filter filter Filter output based on conditions provided
--format string Pretty-print search using a Go template
--limit int Max number of search results
--no-trunc Don't truncate output
docker search mysql --filter=STARS=3000 # 搜索出来的镜像就是stars大于3000的
docker 下载命令
docker pull
root@iZbp140vcie2ktlx79vrqjZ:~# docker pull redis
Using default tag: latest # 如果不写tag,版本默认最新版
latest: Pulling from library/redis
a2abf6c4d29d: Pull complete #分层下载,docker images的核心 联合文件系统
c7a4e4382001: Pull complete
4044b9ba67c9: Pull complete
c8388a79482f: Pull complete
413c8bb60be2: Pull complete
1abfd3011519: Pull complete
Digest: sha256:db485f2e245b5b3329fdc7eff4eb00f913e09d8feb9ca720788059fdc2ed8339
Status: Downloaded newer image for redis:latest
docker.io/library/redis:latest # docker.io :真实地址
# 等价
docker pull redis
docker.io/library/redis:latest
#指定版本下载
docker pull mysql:5,7
root@iZbp140vcie2ktlx79vrqjZ:~# docker pull mysql:5.6
5.6: Pulling from library/mysql
35b2232c987e: Pull complete
fc55c00e48f2: Pull complete
0030405130e3: Pull complete
e1fef7f6a8d1: Pull complete
1c76272398bb: Pull complete
f57e698171b6: Pull complete
f5b825b269c0: Pull complete
dcb0af686073: Pull complete
27bbfeb886d1: Pull complete
6f70cc868145: Pull complete
1f6637f4600d: Pull complete
Digest: sha256:20575ecebe6216036d25dab5903808211f1e9ba63dc7825ac20cb975e34cfcae
Status: Downloaded newer image for mysql:5.6
docker.io/library/mysql:5.6
删除镜像 docker rmi
# docker rmi 镜像ID/名称
docker rmi -f dd3b2a5dcb48
docker rmi -f mysql:5.6
# 删除全部镜像
docker rmi -f $(docker images -aq)
容器命令
我们有了镜像才可以创建容器
新建容器并启动
root@iZbp140vcie2ktlx79vrqjZ:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
redis latest 7614ae9453d1 2 years ago 113MB
centos latest 5d0da3dc9764 3 years ago 231MB
root@iZbp140vcie2ktlx79vrqjZ:~# docker run redis
1:C 05 Nov 2024 00:46:04.586 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1:C 05 Nov 2024 00:46:04.586 # Redis version=6.2.6, bits=64, commit=00000000, modified=0, pid=1, just started
1:C 05 Nov 2024 00:46:04.586 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
1:M 05 Nov 2024 00:46:04.587 * monotonic clock: POSIX clock_gettime
1:M 05 Nov 2024 00:46:04.588 * Running mode=standalone, port=6379.
1:M 05 Nov 2024 00:46:04.588 # Server initialized
1:M 05 Nov 2024 00:46:04.588 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
1:M 05 Nov 2024 00:46:04.589 * Ready to accept connections
docker pull 容器名字
--name="容器名字",tomcat01,tomcat02 区分容器
-d 以后台的形式运行
-it 以交互的方式运行,进入容器查看内容
-p 指定端口
-p ip:主机端口:容器端口
-p 主机端口:容器端口 (主机端口映射到容器端口)
-p 容器端口
容器端口
-P 随机指定端口
容器内的centos(套娃既视感)
root@iZbp140vcie2ktlx79vrqjZ:~# docker run -it centos /bin/bash
[root@dd0c70aba58c /]# ls
bin etc lib lost+found mnt proc run srv tmp var
dev home lib64 media opt root sbin sys usr
exit #退出容器的命令,从容器中退出主机
列出当前运行的容器
docker ps
docker ps 命令
# 列出当前运行的容器
-a # 列出当前运行的容器 + 历史运行的容器
-n=? #列出个数
-q #只显示容器的编号
root@iZbp140vcie2ktlx79vrqjZ:~# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
dd0c70aba58c centos "/bin/bash" 5 minutes ago Exited (0) 4 minutes ago wonderful_agnesi
4760b2201ace redis "docker-entrypoint.s…" 13 minutes ago Exited (0) 12 minutes ago tender_hypatia
e637b243775a centos "/bin/bash" 13 hours ago Exited (127) 12 hours ago funny_jackson
ad558bd7f1fa centos "/bin/bash" 13 hours ago Exited (0) 13 hours ago admiring_fermat
a72cd00164e3 centos "/bin/bash" 13 hours ago Exited (0) 13 hours ago kind_noyce
281bc0a9d807 d2c94e258dcb "/hello" 14 hours ago Exited (0) 14 hours ago jolly_kepler
5039a56b7a49 d2c94e258dcb "/hello" 22 hours ago Exited (0) 22 hours ago objective_hermann
0aee46418f40 d2c94e258dcb "/hello" 22 hours ago Exited (0) 22 hours ago gallant_fermi
# 只显示容器编号
root@iZbp140vcie2ktlx79vrqjZ:~# docker ps -aq
dd0c70aba58c
4760b2201ace
e637b243775a
ad558bd7f1fa
a72cd00164e3
281bc0a9d807
5039a56b7a49
0aee46418f40
容器不停止退出:ctrl + P + Q
删除容器 docker rm -f
docker rm 容器id #删除指定容器 (不能删除正在运行的容器)
docker rm -f $(docker ps -aq) # 删除全部容器
root@iZbp140vcie2ktlx79vrqjZ:~# docker rm dd0c70aba58c
dd0c70aba58c
启动和停止容器的操作
docker start 容器id #启动容器
docker restar 容器id # 重启容器
docker stop 容器id #停止容器
docker kill 容器id #强制停止容器
其他常用命令
后台启动命令
# 命令 docker run -d 镜像名
root@iZbp140vcie2ktlx79vrqjZ:~# docker run -d centos
052ef64a9737c23aba69ca01abbd4e3c67d54d1e055531d978d30d6778156a92
root@iZbp140vcie2ktlx79vrqjZ:~# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
root@iZbp140vcie2ktlx79vrqjZ:~#
# 问题docker ps,发现centos停止了
# 常见的坑,docker容器使用后台运行,就必须要有一个前台进程,docker发现没有应用,就会自动停止
# nginx 容器启动后,发现自己没有提高服务,就会立刻停止,就是没有程序了
查看日志
docker logs -f -t --tail 指定条数 容器
root@iZbp140vcie2ktlx79vrqjZ:~# docker run -d centos /bin/sh -c "while true;do echo ecut;sleep 1;done"
ed84ca14e11fb8cd21e042b414a0fb225677108e8497feb8acf57fc08f897da7
root@iZbp140vcie2ktlx79vrqjZ:~# docker ps
CONTAINER ID IMAGE
ed84ca14e11f centos
# 显示日志
-tf #显示全部f(format)
--tail 数字 # 显示日志条数
root@iZbp140vcie2ktlx79vrqjZ:~# docker logs -f -t --tail 10 ed84ca14e11f
2024-11-05T01:35:39.271655955Z ecut
2024-11-05T01:35:40.273898199Z ecut
2024-11-05T01:35:41.275942654Z ecut
2024-11-05T01:35:42.278454940Z ecut
2024-11-05T01:35:43.280607210Z ecut
2024-11-05T01:35:44.282823147Z ecut
2024-11-05T01:35:45.284783537Z ecut
2024-11-05T01:35:46.287112461Z ecut
2024-11-05T01:35:47.289384937Z ecut
2024-11-05T01:35:48.291505003Z ecut
2024-11-05T01:35:49.293457407Z ecut
2024-11-05T01:35:50.295750937Z ecut
2024-11-05T01:35:51.298013209Z ecut
2024-11-05T01:35:52.300721630Z ecut
2024-11-05T01:35:53.303064907Z ecut
...
查看容器的进程信息
docker top 容器id
root@iZbp140vcie2ktlx79vrqjZ:~# docker top 05c98eee32c3
UID PID PPID C STIME TTY TIME CMD
root 27748 27722 0 09:40 ? 00:00:00 /bin/sh -c while true;do echo ecut;sleep 1;done
root 27795 27748 0 09:40 ? 00:00:00 /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 1
查看镜像的元数据
docker inspect
root@iZbp140vcie2ktlx79vrqjZ:~# docker inspect 05c98eee32c3
[
{
"Id": "05c98eee32c3ed99ab348ae7cd0504dd7b3ff7b6bbbb38d792723d5288785a75",
"Created": "2024-11-05T01:40:38.577546516Z",
"Path": "/bin/sh",
"Args": [
"-c",
"while true;do echo ecut;sleep 1;done"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 27748,
"ExitCode": 0,
"Error": "",
"StartedAt": "2024-11-05T01:40:38.660305814Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6",
"ResolvConfPath": "/var/lib/docker/containers/05c98eee32c3ed99ab348ae7cd0504dd7b3ff7b6bbbb38d792723d5288785a75/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/05c98eee32c3ed99ab348ae7cd0504dd7b3ff7b6bbbb38d792723d5288785a75/hostname",
"HostsPath": "/var/lib/docker/containers/05c98eee32c3ed99ab348ae7cd0504dd7b3ff7b6bbbb38d792723d5288785a75/hosts",
"LogPath": "/var/lib/docker/containers/05c98eee32c3ed99ab348ae7cd0504dd7b3ff7b6bbbb38d792723d5288785a75/05c98eee32c3ed99ab348ae7cd0504dd7b3ff7b6bbbb38d792723d5288785a75-json.log",
"Name": "/affectionate_lehmann",
"RestartCount": 0,
"Driver": "overlay2",
"Platform": "linux",
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile": "docker-default",
"ExecIDs": null,
"HostConfig": {
"Binds": null,
"ContainerIDFile": "",
"LogConfig": {
"Type": "json-file",
"Config": {}
},
"NetworkMode": "bridge",
"PortBindings": {},
"RestartPolicy": {
"Name": "no",
"MaximumRetryCount": 0
},
"AutoRemove": false,
"VolumeDriver": "",
"VolumesFrom": null,
"ConsoleSize": [
23,
104
],
"CapAdd": null,
"CapDrop": null,
"CgroupnsMode": "private",
"Dns": [],
"DnsOptions": [],
"DnsSearch": [],
"ExtraHosts": null,
"GroupAdd": null,
"IpcMode": "private",
"Cgroup": "",
"Links": null,
"OomScoreAdj": 0,
"PidMode": "",
"Privileged": false,
"PublishAllPorts": false,
"ReadonlyRootfs": false,
"SecurityOpt": null,
"UTSMode": "",
"UsernsMode": "",
"ShmSize": 67108864,
"Runtime": "runc",
"Isolation": "",
"CpuShares": 0,
"Memory": 0,
"NanoCpus": 0,
"CgroupParent": "",
"BlkioWeight": 0,
"BlkioWeightDevice": [],
"BlkioDeviceReadBps": [],
"BlkioDeviceWriteBps": [],
"BlkioDeviceReadIOps": [],
"BlkioDeviceWriteIOps": [],
"CpuPeriod": 0,
"CpuQuota": 0,
"CpuRealtimePeriod": 0,
"CpuRealtimeRuntime": 0,
"CpusetCpus": "",
"CpusetMems": "",
"Devices": [],
"DeviceCgroupRules": null,
"DeviceRequests": null,
"MemoryReservation": 0,
"MemorySwap": 0,
"MemorySwappiness": null,
"OomKillDisable": null,
"PidsLimit": null,
"Ulimits": [],
"CpuCount": 0,
"CpuPercent": 0,
"IOMaximumIOps": 0,
"IOMaximumBandwidth": 0,
"MaskedPaths": [
"/proc/asound",
"/proc/acpi",
"/proc/kcore",
"/proc/keys",
"/proc/latency_stats",
"/proc/timer_list",
"/proc/timer_stats",
"/proc/sched_debug",
"/proc/scsi",
"/sys/firmware",
"/sys/devices/virtual/powercap"
],
"ReadonlyPaths": [
"/proc/bus",
"/proc/fs",
"/proc/irq",
"/proc/sys",
"/proc/sysrq-trigger"
]
},
"GraphDriver": {
"Data": {
"LowerDir": "/var/lib/docker/overlay2/1b665e9648f17dc59bbfd26209c7ceb84bfc061bcf91b04e6ae0c8c8448094cb-init/diff:/var/lib/docker/overlay2/941d269f72c89e4a6e5b6557d5a115c8f19d7ce4a4d540ff854cd5faf46650cf/diff",
"MergedDir": "/var/lib/docker/overlay2/1b665e9648f17dc59bbfd26209c7ceb84bfc061bcf91b04e6ae0c8c8448094cb/merged",
"UpperDir": "/var/lib/docker/overlay2/1b665e9648f17dc59bbfd26209c7ceb84bfc061bcf91b04e6ae0c8c8448094cb/diff",
"WorkDir": "/var/lib/docker/overlay2/1b665e9648f17dc59bbfd26209c7ceb84bfc061bcf91b04e6ae0c8c8448094cb/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "05c98eee32c3",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"Cmd": [
"/bin/sh",
"-c",
"while true;do echo ecut;sleep 1;done"
],
"Image": "centos",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"org.label-schema.build-date": "20210915",
"org.label-schema.license": "GPLv2",
"org.label-schema.name": "CentOS Base Image",
"org.label-schema.schema-version": "1.0",
"org.label-schema.vendor": "CentOS"
}
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "c37483b300bf910b98c17cb6b64b38d9f27c4707b786b5be85256bb8bc250566",
"SandboxKey": "/var/run/docker/netns/c37483b300bf",
"Ports": {},
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "276eb9c5fb2e1c801b14cd34f5c4a18e6d4c4cc165e6f928b9c6f8308cbdca45",
"Gateway": "172.18.0.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "172.18.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"MacAddress": "02:42:ac:12:00:02",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"MacAddress": "02:42:ac:12:00:02",
"DriverOpts": null,
"NetworkID": "54ae5e16c1c069d6a5d4cd84168d9ec00ff7ef2ea005177e4029ba846e3f4308",
"EndpointID": "276eb9c5fb2e1c801b14cd34f5c4a18e6d4c4cc165e6f928b9c6f8308cbdca45",
"Gateway": "172.18.0.1",
"IPAddress": "172.18.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"DNSNames": null
}
}
}
}
]
进入当前正在运行的容器
# docker exec -it 容器id
root@iZbp140vcie2ktlx79vrqjZ:~# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
05c98eee32c3 centos "/bin/sh -c 'while t…" 19 minutes ago Up 19 minutes affectionate_lehmann
root@iZbp140vcie2ktlx79vrqjZ:~# docker exec -it 05c98eee32c3 /bin/bash
[root@05c98eee32c3 /]# ls
bin etc lib lost+found mnt proc run srv tmp var
dev home lib64 media opt root sbin sys usr
[root@05c98eee32c3 /]# ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 01:40 ? 00:00:00 /bin/sh -c while true;do echo ecut;sleep 1;done
root 1244 0 0 02:00 pts/0 00:00:00 /bin/bash
root 1274 1 0 02:01 ? 00:00:00 /usr/bin/coreutils --coreutils-prog-shebang=sleep /u
root 1275 1244 0 02:01 pts/0 00:00:00 ps -ef
#方式二
docker attach 容器id
# 进入的是正在运行的命令行
root@iZbp140vcie2ktlx79vrqjZ:~# docker attach 05c98eee32c3
ecut
ecut
ecut
ecut
ecut
ecut
# docker exec -it 容器id 进入容器开启一个新的终端,可以在里面操作(常用)
# docker attach 容器id 进入容器正在运行的终端,不会启动新的进程
从容器内拷贝到主机
docker cp
root@iZbp140vcie2ktlx79vrqjZ:~# docker run -it centos /bin/sh
sh-4.4#
sh-4.4# root@iZbp140vcie2ktlx79vrqjZ:~#
root@iZbp140vcie2ktlx79vrqjZ:~# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
979c62023860 centos "/bin/sh" 12 seconds ago Up 12 seconds laughing_goldwasser
# 进入容器内部
root@iZbp140vcie2ktlx79vrqjZ:~# docker attach 979c62023860
sh-4.4# ls
bin etc lib lost+found mnt proc run srv tmp var
dev home lib64 media opt root sbin sys usr
sh-4.4# cd home
sh-4.4# ls
新建一个容器
sh-4.4# touch test.java
sh-4.4# ls
test.java
# 退出容器
sh-4.4# exit
exit
# 将容器内的text.java 拷贝到指定
root@iZbp140vcie2ktlx79vrqjZ:~# docker cp 979c62023860:/home/test.java /
Successfully copied 1.54kB to /
# 这里的拷贝是一个手动过程
小结
命令:https://www.runoob.com/docker/docker-command-manual.html
分层理解
一、Docker镜像的分层结构
Docker镜像由多个只读的层(layer)组成,这些层按照从下到上的顺序堆叠在一起,形成一个完整的镜像。每一层都是基于下面一层的变化,只包含与前一层的差异部分。这种分层结构使得Docker镜像在构建、传输和存储时都非常高效。
二、UnionFS与镜像分层
UnionFS(联合文件系统)是Docker镜像分层的基础。UnionFS支持将多个只读文件系统挂载到同一个虚拟文件系统中,并且可以将这些文件系统的内容合并显示,但实际上并不复制内容,从而节省了存储空间。在Docker中,每个镜像层都是一个只读的文件系统,它们通过UnionFS被组合成一个统一的虚拟文件系统,供容器运行时使用。
三、镜像层的具体内容
- 基础镜像层:Docker镜像的第一层通常是基础镜像层,它包含了一个最小化的操作系统环境,如Alpine Linux、Ubuntu等。这个基础镜像提供了运行应用程序所需的最基本的文件和工具。
- 依赖层:在基础镜像之上,可以添加应用程序的依赖项和运行时环境。这些依赖项可能包括软件包、库文件等,它们被组织在不同的层中。
- 应用程序层:最后,可以添加应用程序的实际代码和资源文件。这些文件构成了镜像的最上层,包含了应用程序运行所需的所有内容。
四、镜像分层的好处
- 轻量级:由于镜像层是只读的,并且只存储差异部分,因此Docker镜像通常比传统的虚拟机镜像要小得多。
- 高效性:分层结构使得Docker镜像的构建、传输和存储都非常高效。当构建新镜像时,如果所需的层已经存在,Docker会直接使用现有的层,而不是重新创建。这大大加快了镜像的构建速度,并减少了网络传输的数据量。
- 可复用性:多个镜像可以共享相同的基础层和依赖层,从而节省存储空间。这种复用性使得Docker镜像在持续集成、持续部署和分布式系统部署中非常有用。
- 灵活性:Docker镜像的分层结构使得镜像的修改和扩展变得非常灵活。用户可以通过修改Dockerfile来添加新的层,从而构建出满足特定需求的新镜像。
五、容器层与镜像层的关系
当基于Docker镜像启动容器时,Docker会在镜像的最顶层添加一个可写的容器层。这个容器层允许容器在运行时对文件系统进行写操作,如创建新文件、修改现有文件等。所有对容器的修改都只会发生在容器层中,而不会影响底层的镜像层。这种设计保证了镜像的不可变性,使得容器可以轻松地从一个环境迁移到另一个环境,而无需担心环境差异导致的问题。
如何提交一个自己的镜像
操作测试
# 启动一个默认的tomcat
docker run -it -p 8080:8080 tomcat
# 发现默认的tomcat下的webapps没有应用(镜像的原因)
root@iZbp140vcie2ktlx79vrqjZ:~# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
62bac25773cc tomcat "catalina.sh run" 16 seconds ago Up 15 seconds 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp friendly_sanderson
root@iZbp140vcie2ktlx79vrqjZ:~# docker exec -it 62bac25773cc /bin/bash
root@62bac25773cc:/usr/local/tomcat# ls
BUILDING.txt LICENSE README.md RUNNING.txt conf logs temp webapps.dist
CONTRIBUTING.md NOTICE RELEASE-NOTES bin lib native-jni-lib webapps work
root@62bac25773cc:/usr/local/tomcat# cd webapps
root@62bac25773cc:/usr/local/tomcat/webapps# ls
root@62bac25773cc:/usr/local/tomcat/webapps#
# 现在自己拷贝进去自己的文件
root@62bac25773cc:/usr/local/tomcat/webapps# cd ..
root@62bac25773cc:/usr/local/tomcat# cp -r webapps.dist/* webapps
root@62bac25773cc:/usr/local/tomcat# cd webapps
root@62bac25773cc:/usr/local/tomcat/webapps# ls
ROOT docs examples host-manager manager
root@62bac25773cc:/usr/local/tomcat/webapps#
# 提交自己的镜像
root@iZbp140vcie2ktlx79vrqjZ:~# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
62bac25773cc tomcat "catalina.sh run" 10 minutes ago Up 10 minutes 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp friendly_sanderson
# 将我们修改过的镜像 通过 commit 提交
root@iZbp140vcie2ktlx79vrqjZ:~# docker commit -a="Kc" -m="cp webapps.dist to webapps" 62bac25773cc mycat
sha256:5a0c6c6d18fa007982c917725b8317dfae1d499977e00f16d8e697e942cbfb28
# mycat就是自己提交的镜像
root@iZbp140vcie2ktlx79vrqjZ:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mycat latest 5a0c6c6d18fa About a minute ago 684MB
nginx latest 605c77e624dd 2 years ago 141MB
tomcat 9.0 b8e65a4d736d 2 years ago 680MB
tomcat latest fb5657adc892 2 years ago 680MB
redis latest 7614ae9453d1 2 years ago 113MB
centos latest 5d0da3dc9764 3 years ago 231MB