docker常用命令
docker常用命令
帮助命令
docker version # 显示docker的版本信息
docker info # 显示docker的系统信息,包括镜像和容器的数量
docker 命令 --help # 帮助命令
镜像命令
docker images 查看所有本地的主机上的镜像
[root@VM-4-6-centos /]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest d1165f221234 4 months ago 13.3kB
# REPOSITORY 镜像的仓库源 TAG 镜像的标签 IMAGE ID 镜像的ID CREATED 镜像的创建时间 SIZE 镜像的大小
# 可选项
-a, --all 列出所有镜像
-q, --quiet 只显示镜像的id
docker search 搜索镜像
[root@VM-4-6-centos /]# docker search mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 11175 [OK]
mariadb MariaDB Server is a high performing open sou… 4243 [OK]
# 可选项,通过收藏过滤
--filter=STARS=3000 # 搜索出来的镜像就是STARS大于3000的
[root@VM-4-6-centos /]# docker search mysql --filter=STARS=3000
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 11175 [OK]
mariadb MariaDB Server is a high performing open sou… 4243 [OK]
[root@VM-4-6-centos /]# docker search mysql --filter=STARS=5000
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 11175 [OK]
docker pull 下载命令
# 下载镜像 docker pull 镜像名[:tag]
[root@VM-4-6-centos /]# docker pull mysql
Using default tag: latest # 如果不写 tag,默认就是 latest
latest: Pulling from library/mysql
33847f680f63: Pull complete # 分层下载,docker images的核心 联合文件系统
5cb67864e624: Pull complete
1a2b594783f5: Pull complete
b30e406dd925: Pull complete
48901e306e4c: Pull complete
603d2b7147fd: Pull complete
802aa684c1c4: Pull complete
715d3c143a06: Pull complete
6978e1b7a511: Pull complete
f0d78b0ac1be: Pull complete
35a94d251ed1: Pull complete
36f75719b1a9: Pull complete
Digest: sha256:8b928a5117cf5c2238c7a09cd28c2e801ac98f91c3f8203a8938ae51f14700fd # 签名
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest # 真实地址
# 等价于它
docker pull mysql
docker pull docker.io/library/mysql:latest
# 指定版本下载
[root@VM-4-6-centos ~]# docker pull mysql:5.7
5.7: Pulling from library/mysql
33847f680f63: Already exists
5cb67864e624: Already exists
1a2b594783f5: Already exists
b30e406dd925: Already exists
48901e306e4c: Already exists
603d2b7147fd: Already exists
802aa684c1c4: Already exists
5b5a19178915: Pull complete
f9ce7411c6e4: Pull complete
f51f6977d9b2: Pull complete
aeb6b16ce012: Pull complete
Digest: sha256:be70d18aedc37927293e7947c8de41ae6490ecd4c79df1db40d1b5b5af7d9596
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7
docker rmi 删除镜像
[root@VM-4-6-centos /]# docker rmi -f 镜像id # 删除指定的镜像
[root@VM-4-6-centos /]# docker rmi -f 镜像id 镜像id 容器id 镜像id 镜像id # 删除多个镜像
[root@VM-4-6-centos /]# docker rmi -f $(docker images -aq) # 删除全部的镜像
修改镜像名称或者版本
docker tag 镜像id 新的名字:[tag]
容器命令
说明:有了镜像才可以创建容器,linux,下载centos镜像测试
docker pull centos
新建容器并启动
docker run [可选参数] image
# 参数说明
--name="name" 容器名字 tomcat01 tomcat02,用来区分容器
-d 后台方式运行
-it 使用交互方式运行,进入容器查看内容
-p 指定容器的端口 -p 8080:8080
-p ip:主机端口:容器端口
-p 主机端口:容器端口 (常用)
-p 容器端口
-P 随机指定端口
# 测试,启动并进入容器
[root@VM-4-6-centos ~]# docker run -it centos /bin/bash
[root@319ba74df193 /]# ls # 查看容器内部的centos,基础版本,很多命令都不完善
bin etc lib lost+found mnt proc run srv tmp var
dev home lib64 media opt root sbin sys usr
# 从容器中退回主机
[root@c9c13400bd05 /]# exit
exit
[root@VM-4-6-centos /]# ls
bin data etc lib lost+found mnt proc run srv tmp var
boot dev home lib64 media opt root sbin sys usr
列出所有的运行的容器
# docker ps 命令 列出当前正在运行的容器
-a # 列出当前正在运行的容器+带出历史运行过的容器
-n=? # 显示最近创建的容器
-q # 只显示容器的编号
[root@VM-4-6-centos /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@VM-4-6-centos /]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c9c13400bd05 centos "/bin/bash" 2 minutes ago Exited (0) 2 minutes ago sharp_ritchie
319ba74df193 centos "/bin/bash" 5 minutes ago Exited (0) 2 minutes ago interesting_northcutt
287ed6ff0d7b centos "/bin/bash" 5 minutes ago Exited (130) 5 minutes ago elated_knuth
5a21fc1cdeba d1165f221234 "/hello" 2 hours ago Exited (0) 2 hours ago cranky_allen
退出容器
exit # 直接容器停止并退出
ctrl + p + q # 容器不停止退出
删除容器
docker rm 容器id # 删除指定容器,不能删除正在运行的容器,如果强制删除 rm -f
docker rm -f $(docker ps -aq) # 删除所有容器
docker ps -a -q|xargs docker rm # 删除所有容器
启动和停止容器的操作
docker start 容器id # 启动容器
docker restart 容器id # 重启容器
docker stop 容器id # 停止当前正在运行的容器
docker kill 容器id # 强制停止容器
服务器磁盘空间满了
启动容器报:Error response from daemon: Cannot restart container 26bc4ef925f3: mkdir /var/lib/docker/overlay2.../merged: no space left on device
这是启动容器的时候磁盘空间满了,无法创建文件
解决:
查看磁盘占用情况:df -h
切换到 / 目录,查看目录下文件大小:du -sh *
查找占用空间大的目录,通过逐层定位,最后会找到具体的文件
除了磁盘空间除了文件占用之外,还有一种情况,当磁盘空间满了之后,我们无法查找到大文件,此时可能是文件可能已经被删掉,但有进程依然在使用它。在进程运行期间,Linux 不会释放该文件的存储空间。此时看到磁盘空间仍是100%
使用 lsof | grep deleted
查找到占用的进程,直接停止进程或者kill掉就可以释放空间(注:如果不是生产环境,重启操作系统,空间也会释放)
常用其他命令
后台启动容器
# 命令 docker run -d 镜像名
[root@VM-4-6-centos /]# docker run -d centos
5b64efa61bce56ddf35258137355dc014974bf8b13ebfefe486fbd00865411a0
[root@VM-4-6-centos /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
# 问题:docker ps,发现centos停止了
# 常见的坑,docker容器使用后台运行,就必须要有一个前台进程,docker发现没有应用,就会自动停止
# nginx,容器启动后,发现自己没有提供服务,就会立刻停止,就是没有程序了
查看日志
docker logs [OPTIONS] CONTAINER
Options:
- –details 显示更多的信息
- -f, --follow 跟踪实时日志
- –since string 显示自某个timestamp之后的日志,或相对时间,如42m(即42分钟)
- –tail string 从日志末尾显示多少行日志, 默认是all
- -t, --timestamps 显示时间戳
- –until string 显示自某个timestamp之前的日志,或相对时间,如42m(即42分钟)
docker logs -f -t --tail 10 容器id,没有日志
# 自己编写一段shell脚本
[root@VM-4-6-centos /]# docker run -d centos /bin/sh -c "while true;do echo "zhangsan";sleep 1;done"
[root@VM-4-6-centos /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bb91853ee9c1 centos "/bin/sh -c 'while t…" 7 seconds ago Up 6 seconds awesome_meninsky
# 显示日志
-tf # 显示日志
--tail number # 要显示日志的条数
[root@VM-4-6-centos /]# docker logs -f -t --tail 10 bb91853ee9c1
2021-07-28T13:19:06.960355824Z zhangsan
2021-07-28T13:19:07.962189112Z zhangsan
2021-07-28T13:19:08.964093434Z zhangsan
2021-07-28T13:19:09.965842471Z zhangsan
查看指定时间后的日志,只显示最后100行:
docker logs -f -t --since=“2018-02-08” --tail=100 CONTAINER_ID
查看最近30分钟的日志:
docker logs --since 30m CONTAINER_ID
查看某时间之后的日志:
docker logs -t --since=“2018-02-08T13:23:37” CONTAINER_ID
查看某时间段日志:
docker logs -t --since=“2020-04-01T08:25:00” --until “2020-04-01T09:30:59” CONTAINER_ID
查看容器中进程信息
# 命令 docker top 容器id
[root@VM-4-6-centos /]# docker top bb91853ee9c1
UID PID PPID C STIME TTY TIME CMD
root 10728 10709 0 21:18 ? 00:00:00 /bin/sh -c while true;do echo zhangsan;sleep 1;done
root 12497 10728 0 21:25 ? 00:00:00 /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 1
查看容器的元数据
# 命令
docker inspect 容器id
# 测试
[root@VM-4-6-centos /]# docker inspect bb91853ee9c1
[
{
"Id": "bb91853ee9c1536a4065793123029c0f93968987567b921dddd6b55632c8f67e",
"Created": "2021-07-28T13:18:33.597838995Z",
"Path": "/bin/sh",
"Args": [
"-c",
"while true;do echo zhangsan;sleep 1;done"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 10728,
"ExitCode": 0,
"Error": "",
"StartedAt": "2021-07-28T13:18:33.899057962Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:300e315adb2f96afe5f0b2780b87f28ae95231fe3bdd1e16b9ba606307728f55",
"ResolvConfPath": "/var/lib/docker/containers/bb91853ee9c1536a4065793123029c0f93968987567b921dddd6b55632c8f67e/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/bb91853ee9c1536a4065793123029c0f93968987567b921dddd6b55632c8f67e/hostname",
"HostsPath": "/var/lib/docker/containers/bb91853ee9c1536a4065793123029c0f93968987567b921dddd6b55632c8f67e/hosts",
"LogPath": "/var/lib/docker/containers/bb91853ee9c1536a4065793123029c0f93968987567b921dddd6b55632c8f67e/bb91853ee9c1536a4065793123029c0f93968987567b921dddd6b55632c8f67e-json.log",
"Name": "/awesome_meninsky",
"RestartCount": 0,
"Driver": "overlay2",
"Platform": "linux",
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile": "",
"ExecIDs": null,
"HostConfig": {
"Binds": null,
"ContainerIDFile": "",
"LogConfig": {
"Type": "json-file",
"Config": {}
},
"NetworkMode": "default",
"PortBindings": {},
"RestartPolicy": {
"Name": "no",
"MaximumRetryCount": 0
},
"AutoRemove": false,
"VolumeDriver": "",
"VolumesFrom": null,
"CapAdd": null,
"CapDrop": null,
"CgroupnsMode": "host",
"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",
"ConsoleSize": [
0,
0
],
"Isolation": "",
"CpuShares": 0,
"Memory": 0,
"NanoCpus": 0,
"CgroupParent": "",
"BlkioWeight": 0,
"BlkioWeightDevice": [],
"BlkioDeviceReadBps": null,
"BlkioDeviceWriteBps": null,
"BlkioDeviceReadIOps": null,
"BlkioDeviceWriteIOps": null,
"CpuPeriod": 0,
"CpuQuota": 0,
"CpuRealtimePeriod": 0,
"CpuRealtimeRuntime": 0,
"CpusetCpus": "",
"CpusetMems": "",
"Devices": [],
"DeviceCgroupRules": null,
"DeviceRequests": null,
"KernelMemory": 0,
"KernelMemoryTCP": 0,
"MemoryReservation": 0,
"MemorySwap": 0,
"MemorySwappiness": null,
"OomKillDisable": false,
"PidsLimit": null,
"Ulimits": null,
"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"
],
"ReadonlyPaths": [
"/proc/bus",
"/proc/fs",
"/proc/irq",
"/proc/sys",
"/proc/sysrq-trigger"
]
},
"GraphDriver": {
"Data": {
"LowerDir": "/var/lib/docker/overlay2/53c225498a4bef1a95a626a9801716000c0695307c988acbb37807e88fd25c6a-init/diff:/var/lib/docker/overlay2/8d1a1f15308f8d3ab404ab30732941f026136bfaf9acc83b16a760301d7e9b27/diff",
"MergedDir": "/var/lib/docker/overlay2/53c225498a4bef1a95a626a9801716000c0695307c988acbb37807e88fd25c6a/merged",
"UpperDir": "/var/lib/docker/overlay2/53c225498a4bef1a95a626a9801716000c0695307c988acbb37807e88fd25c6a/diff",
"WorkDir": "/var/lib/docker/overlay2/53c225498a4bef1a95a626a9801716000c0695307c988acbb37807e88fd25c6a/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "bb91853ee9c1",
"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 zhangsan;sleep 1;done"
],
"Image": "centos",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"org.label-schema.build-date": "20201204",
"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": "22830b8a98c93831de600aa8e182dbeb6dfc4a5457fa9c53d72f344583e8983a",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/22830b8a98c9",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "e85f2f3886719833f64ae6b16bd0b9138604cbdedba564909e94590eb952afd3",
"Gateway": "172.17.0.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"MacAddress": "02:42:ac:11:00:02",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "d9c87f94c09bbedf3de2c96d5d42d7b6358e58a4c8eb85372f96c85568278667",
"EndpointID": "e85f2f3886719833f64ae6b16bd0b9138604cbdedba564909e94590eb952afd3",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:02",
"DriverOpts": null
}
}
}
}
]
进入当前正在运行的容器
# 通常容器都是使用后台方式运行的,需要进入容器,修改一些配置
# 命令
docker exec -it 容器id /bin/bash
# 测试
[root@VM-4-6-centos /]# docker exec -it bb91853ee9c1 /bin/bash
[root@bb91853ee9c1 /]# ls
bin etc lib lost+found mnt proc run srv tmp var
dev home lib64 media opt root sbin sys usr
[root@bb91853ee9c1 /]# exit
# 方式二
docker attach 容器id
# 测试
[root@VM-4-6-centos /]# docker attach bb91853ee9c1
正在执行当前的代码...
# docker exec 进入容器后开启一个新的终端,可以在里面操作(常用)
# docker attach 进入容器正在执行的终端,不会启动新的进程
从容器内拷贝文件到主机上
docker cp 容器id:容器内路径 目的路径
# 测试
# 进入docker容器内部
[root@VM-4-6-centos /]# docker exec -it e290c6f892a1 /bin/bash
[root@e290c6f892a1 /]# cd /home
# 新建文件
[root@e290c6f892a1 home]# touch test.java
[root@e290c6f892a1 home]# ls
test.java
[root@e290c6f892a1 home]# exit
exit
# 将文件拷贝到主机上
[root@VM-4-6-centos /]# docker cp e290c6f892a1:/home/test.java /home
[root@VM-4-6-centos /]# cd home
[root@VM-4-6-centos home]# ls
lighthouse test.java
# 拷贝是一个手动的过程,未来使用 -v 卷技术,可以实现自动同步,主机目录 容器目录连通
测试
# 第一步:搜索镜像
[root@VM-4-6-centos /]# docker search nginx
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
nginx Official build of Nginx. 15277 [OK]
jwilder/nginx-proxy Automated Nginx reverse proxy for docker con… 2056 [OK]
richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable of… 815 [OK]
jc21/nginx-proxy-manager Docker container for managing Nginx proxy ho… 228
# 第二步:下载镜像 pull
[root@VM-4-6-centos /]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
33847f680f63: Pull complete
dbb907d5159d: Pull complete
8a268f30c42a: Pull complete
b10cf527a02d: Pull complete
c90b090c213b: Pull complete
1f41b2f2bf94: Pull complete
Digest: sha256:8f335768880da6baf72b70c701002b45f4932acae8d574dedfddaf967fc3ac90
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
# 第三步:运行镜像
[root@VM-4-6-centos /]# docker run -d --name nginx01 -p 3344:80 nginx # 将nginx以后台的方式运行别名为nginx01,主机的3344端口与容器的80端口进行映射
0a98b790df5a7458beda860a66bbc0be8fcacf3deacbb504eaf808a0a94aecbb
[root@VM-4-6-centos /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0a98b790df5a nginx "/docker-entrypoint.…" 7 seconds ago Up 6 seconds 0.0.0.0:3344->80/tcp, :::3344->80/tcp nginx01
e290c6f892a1 300e315adb2f "/bin/bash" 8 days ago Up 8 days quizzical_antonelli
# 第四步:发送请求测试
[root@VM-4-6-centos /]# curl localhost:3344
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
# 第五步:进入容器
[root@VM-4-6-centos /]# docker exec -it nginx01 /bin/bash
root@0a98b790df5a:/# whereis nginx
nginx: /usr/sbin/nginx /usr/lib/nginx /etc/nginx /usr/share/nginx
root@0a98b790df5a:/# cd /etc/nginx
root@0a98b790df5a:/etc/nginx# ls
conf.d fastcgi_params mime.types modules nginx.conf scgi_params uwsgi_params
# 官方使用tomcat
docker run -it --rm tomcat:9.0 # docker run -it --rm,一般用来测试,用完删除
# 之前启动都是在后台,停止容器之后,容器还是可以查到
# 下载tomcat
docker pull tomcat
# 启动运行
docker run -d -p 3355:8080 --name tomcat01 tomcat
# 测试访问有问题,404
# 进入容器
[root@VM-4-6-centos /]# docker exec -it tomcat01 /bin/bash
root@b73c69ffd8a6:/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
# 发现问题:1、linux命令少了 2、webapps没有内容 阿里云镜像的原因,默认是最小的镜像,所有不必要的都剔除
# 保证最小可运行
root@b73c69ffd8a6:/usr/local/tomcat# ll
bash: ll: command not found
root@b73c69ffd8a6:/usr/local/tomcat# cd webapps
root@b73c69ffd8a6:/usr/local/tomcat/webapps# ls
root@b73c69ffd8a6:/usr/local/tomcat/webapps#
# 将 webapps.dist 里的文件拷贝到 webapps目录中 即可解决,webapps.dist目录中是本来webapps的项目
root@b73c69ffd8a6:/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@b73c69ffd8a6:/usr/local/tomcat# cp -r webapps.dist/* webapps # 拷贝
root@b73c69ffd8a6:/usr/local/tomcat# cd webapps
root@b73c69ffd8a6:/usr/local/tomcat/webapps# ls
ROOT docs examples host-manager manager
部署 es
# es 暴露的端口很多
# es 十分耗内存
# es 的数据一般需要放置到安全目录 挂载
# --net somenetwork 网咯配置
# 启动 elasticsearch
docker run -d --name elasticsearch -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" elasticsearch:7.6.2
# 启动了,linux 就卡住了,docker stats 查看 cpu 的状态
# 解决:增加内存限制,修改配置文件 -e 环境配置修改
docker run -d --name elasticsearch -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" -e ES_JAVA_OPTS="-Xms64m -Xmx512m" elasticsearch:7.6.2
可视化
portainer(先用这个)
docker图形化界面管理工具,提供后台面板供我们操作
docker run -d -p 8088:9000 \
--restart=always -v /var/run/docker.sock:/var/run/docker.sock --privileged=true portainer/portainer
设置用户名和密码
选择本地的
进入后的面板
Rancher(CI/CD再用)
commit镜像
docker镜像都是只读的,当容器启动时,一个新的可写层加载到镜像的顶部
这一层就是通常说的容器层,容器之下叫镜像层
# docker commit 提交容器称为新的副本
docker commit -m="提交的描述信息" -a="作者" 容器id 新的镜像名:[tag]
# 测试
[root@VM-4-6-centos /]# docker commit -a="zhangsan" -m="add webapps apps" 9a8ebc7d56e7 tomcat01:1.0
sha256:9088587ab82b295470f18ecf572ba15528198264af00d197db2155de1583a923
[root@VM-4-6-centos /]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
tomcat01 1.0 9088587ab82b 9 seconds ago 673MB
tomcat latest 710ec5c56683 26 hours ago 668MB
nginx latest 08b152afcfae 2 weeks ago 133MB
portainer/portainer latest 580c0e4e98b0 4 months ago 79.1MB
centos latest 300e315adb2f 8 months ago 209MB
elasticsearch 7.6.2 f29a1ee41030 16 months ago 791MB
将操作过的容器通过commit提交为一个新的镜像,以后就可以使用新的镜像
容器数据卷
如果数据都在容器中,容器删除,数据就会丢失!需求:数据可以持久化
MySql,容器删了,数据丢失!需求:MySql数据可以存储在本地
容器之间可以有一个数据共享技术!docker容器中产生的数据,同步在本地!
这就是卷技术,目录的挂载,将容器中的目录挂载到linux上面!
方式一:直接使用命令挂载
docker run -it -v 主机目录:容器内目录
# 测试
[root@VM-4-6-centos /]# docker run -it -v /home/test:/home centos /bin/bash
# docker inspect 容器id 查看数据
[root@VM-4-6-centos home]# docker inspect 6e5e298c5611
"Mounts": [
{
"Type": "bind",
"Source": "/home/test", # 主机内的地址
"Destination": "/home", # 容器内的地址
"Mode": "",
"RW": true,
"Propagation": "rprivate"
}
],
挂载成功
好处:修改文件只需要在本地修改,容器内会自动同步
MySql数据持久化问题
# 获取镜像
docker pull mysql:5.7
# 运行容器,需要做数据挂载! 安装启动 mysql ,需要配置密码,需要注意
# 官方命令 docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag
# 运行mysql
# -d 后台运行
# -p 端口映射
# -v 数据卷挂载
# -e 环境配置
# --name 容器名字
[root@VM-4-6-centos home]# docker run -d -p 3306:3306 -v /home/mysql/conf:/etc/mysql/conf.d -v /home/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=root --name=mysql01 mysql:5.7
挂载成功后,删除容器,本地的数据没有丢失,实现了容器数据持久化功能!
具名与匿名挂载
# 匿名挂载 -v 容器内路径
docker run -d -P --name nginx01 -v /etc/nginx nginx
# 查看所有的volume的情况
[root@VM-4-6-centos /]# docker volume ls
DRIVER VOLUME NAME
local 0877e091f1d763ac0aaaa73adc1ffb37b17f5f1807b4fb3bb5c23be5b72737a0
# 这种是匿名挂载,在 -v 只写了容器的路径,没有写容器外的路径
# 具名挂载
docker run -d -P --name nginx02 -v nginx02:/etc/nginx nginx
[root@VM-4-6-centos /]# docker run -d -P --name nginx02 -v nginx02:/etc/nginx nginx
953500116bcdabd69cd16ad81610873feb21b151bc04124a26b99baedf98f256
[root@VM-4-6-centos /]# docker volume ls
DRIVER VOLUME NAME
local nginx02
# 通过 -v 卷名:容器内路径
# 查看这个卷
[root@VM-4-6-centos /]# docker volume inspect nginx02
[
{
"CreatedAt": "2021-08-08T14:55:17+08:00",
"Driver": "local",
"Labels": null,
"Mountpoint": "/var/lib/docker/volumes/nginx02/_data", # volume 卷在主机映射的路径
"Name": "nginx02",
"Options": null,
"Scope": "local"
}
]
所有的docker容器内的卷,没有指定目录的情况下都是在/var/lib/docker/volumes/xxxx/_data
通过具名挂载可以方便找到挂载的卷,大部分情况使用的具名挂载
# 如何确定是具名挂载还是匿名挂载,还是指定路径挂载
-v 容器内路径 # 匿名挂载
-v 卷名:容器内路径 # 具名挂载
-v /主机路径:容器内路径 # 指定路径挂载
# 通过 -v 容器内路径:ro rw 改变读写权限
ro readonly # 只读
rw readwrite # 可读可写
# 设置了容器的权限,容器对挂载出来的内容就限定了
docker run -d -P --name nginx02 -v nginx02:/etc/nginx:ro nginx
docker run -d -P --name nginx02 -v nginx02:/etc/nginx:rw nginx
# ro 说明这个路径这个路径只能主机操作,容器内部是无法操作的
方式二:dockerfile
dockerfile就是用来构建docker镜像的构建文件,命令脚本
通过脚本生成镜像,镜像是一层层的,脚本是一个一个的命令,每个命令都是一层
# 创建 dockerfile-cmd 文件,名字随意
# 内容 指令(大写) 参数 每个命令就是镜像的一层
FROM centos
VOLUME ["volume01","volume02"]
CMD echo "------end------"
CMD /bin/bash
# 测试
[root@VM-4-6-centos ~]# docker build -f /home/dockerfile-test/dockerfile-cmd -t zhangsan/centos:1.0 .
Sending build context to Docker daemon 29.74kB
Step 1/4 : FROM centos
---> 300e315adb2f
Step 2/4 : VOLUME ["volume01","volume02"]
---> Running in c976c3bc1860
Removing intermediate container c976c3bc1860
---> b5bc0e020c5a
Step 3/4 : CMD echo "------end------"
---> Running in 759a6e1030f3
Removing intermediate container 759a6e1030f3
---> 15fee29c0a33
Step 4/4 : CMD /bin/bash
---> Running in 886be2f26f5b
Removing intermediate container 886be2f26f5b
---> 3c4440cd3d9f
Successfully built 3c4440cd3d9f
Successfully tagged zhangsan/centos:1.0
[root@VM-4-6-centos ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
zhangsan/centos 1.0 3c4440cd3d9f 10 seconds ago 209MB
这个卷和外部一定有一个同步的目录
查看卷挂载的目录 docker inspect 容器id
构建镜像的时候没有挂载卷,需要手动挂载 -v 卷名:容器内路径
# 通过 --volumrs-from 容器id(名称)实现容器间的数据共享
[root@VM-4-6-centos /]# docker run -it --name centos03 --volumes-from centos01 zhangsan/centos:1.0 /bin/bash
删除其中的任意数据卷共享的容器容器,存在数据卷共享的容器不受影响
多个mysql实现数据共享
# 两个mysql容器实现了数据同步
[root@VM-4-6-centos home]# docker run -d -p 3306:3306 -v /home/mysql/conf:/etc/mysql/conf.d -v /home/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=root --name=mysql01 mysql:5.7
[root@VM-4-6-centos home]# docker run -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=root --name=mysql02 --volumes-from mysql01 mysql:5.7
查看本地镜像变更历史
docker history 镜像id
进入容器vi/vim未安装
进入运行的容器中后,使用 vim 命令打开某个文件出现
解决:
apt-get update
apt-get install vim
查看tomcat容器logs
docker logs -f -t --since="2021-03-24" --tail=10 tomcat01
--since : 此参数指定了输出日志开始日期,即只输出指定日期(yyyy-MM-ddTHH:mm:ss.SSS 或者 yyyy-MM-dd)
之后的日志。
-f : 查看实时日志
-t : 查看日志产生的日期
-tail=10 : 查看最后的10条日志。
tomcat01: 容器名称
模糊搜素日志
docker logs -f tomcat01 | grep "2021-03-24"
# docker logs -f 容器id
docker logs -f b63b21196391