帮助命令
docker version # 显示docker的版本信息
docker info # 显示docker的系统信息,包括镜像和容器
docker 命令 --help # 帮助命令
帮助文档地址:https://docs.docker.com/reference/
Hub地址:https://hub.docker.com/_/mysql?tab=description
镜像命令
1、docker images查看所有本地的主机上的镜像
[root@iZwz9ga2spyeb5u9tdq8t5Z /]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest feb5d9fea6a5 3 months ago 13.3kB
# 解释
REPOSITORY 镜像的仓库源
TAG 镜像的标签
IMAGE ID 镜像的id
CREATED 镜像的创建时间
SIZE 镜像的大小
# 可选项
-a, --all 列出所有的镜像
-q, --quiet 只显示镜像的id
2、docker search 搜索镜像
[root@iZwz9ga2spyeb5u9tdq8t5Z /]# docker search mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 11880 [OK]
mariadb MariaDB Server is a high performing open sou… 4541 [OK]
mysql/mysql-server Optimized MySQL Server Docker images. Create… 889 [OK]
percona Percona Server is a fork of the MySQL relati… 566 [OK]
phpmyadmin phpMyAdmin - A web interface for MySQL and M… 407 [OK]
# 可选项,通过搜索来过滤
-f=STARS=10000 # 搜索出来的镜像是STARS大于10000的
[root@iZwz9ga2spyeb5u9tdq8t5Z /]# docker search mysql --filter=STARs=10000
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 11880 [OK]
[root@iZwz9ga2spyeb5u9tdq8t5Z /]# docker search mysql -f=STARs=10000
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 11880 [OK]
3、docker pull 下载镜像
# 下载镜像 docker pull 镜像名字[:tag]
[root@iZwz9ga2spyeb5u9tdq8t5Z /]# docker pull mysql
Using default tag: latest # 如果不写tag,默认是latest
latest: Pulling from library/mysql
72a69066d2fe: Pull complete # 分层下载,docker iamge的核心,联合文件系统
93619dbc5b36: Pull complete
99da31dd6142: Pull complete
626033c43d70: Pull complete
37d5d7efb64e: Pull complete
ac563158d721: Pull complete
d2ba16033dad: Pull complete
688ba7d5c01a: Pull complete
00e060b6d11d: Pull complete
1c04857f594f: Pull complete
4d7cfa90e6ea: Pull complete
e0431212d27d: Pull complete
Digest: sha256:e9027fe4d91c0153429607251656806cc784e914937271037f7738bd5b8e7709 # 签名
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest # 真实地址
docker pull mysql
docker docker.io/library/mysql:latest # 这两个等价
# 指定版本下载,要去看看hub中是否支持该版本:https://hub.docker.com/_/mysql?tab=description
[root@iZwz9ga2spyeb5u9tdq8t5Z /]# docker pull mysql:5.7
5.7: Pulling from library/mysql
72a69066d2fe: Already exists # 分层下载时,可使用之前已拥有的部分层级,大大的减少了内存的消耗,即联合文件系统思想
93619dbc5b36: Already exists
99da31dd6142: Already exists
626033c43d70: Already exists
37d5d7efb64e: Already exists
ac563158d721: Already exists
d2ba16033dad: Already exists
0ceb82207cd7: Pull complete
37f2405cae96: Pull complete
e2482e017e53: Pull complete
70deed891d42: Pull complete
Digest: sha256:f2ad209efe9c67104167fc609cca6973c8422939491c9345270175a300419f94
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7
# 当前镜像信息
[root@iZwz9ga2spyeb5u9tdq8t5Z /]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 c20987f18b13 8 days ago 448MB
mysql latest 3218b38490ce 8 days ago 516MB
hello-world latest feb5d9fea6a5 3 months ago 13.3kB
4、docker rmi删除镜像
# 删除指定的镜像 docker rmi -f 镜像id
[root@iZwz9ga2spyeb5u9tdq8t5Z /]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 c20987f18b13 8 days ago 448MB
mysql latest 3218b38490ce 8 days ago 516MB
hello-world latest feb5d9fea6a5 3 months ago 13.3kB
[root@iZwz9ga2spyeb5u9tdq8t5Z /]# docker rmi -f c20987f18b13
# 删除所有镜像 docker rmi -f $(docker images -aq)
# 删除多个镜像 docker rmi -f 镜像id 镜像id 镜像id
容器命令
说明:我们有了镜像才可以创建容器,Linux,下载一个centos镜像来测试学习
docker pull centos
1、新建容器并启动
docker run [可选参数] image
# 常用参数说明
--name="Name" 容器名字 tomcat01 tomcat01,用来区分容器
-d 后台方式运行
-it 使用交互方式运行,比如进入容器查看内容
-p 指定容器的端口 -p 8080:8080
-p 主机端口:容器端口 # 主机端口映射到容器端口【常用】
-p 容器端口
-p ip:主机端口:容器端口
容器端口
-P(大p) 随机指定端口
# 调试,启动并进入容器
[root@iZwz9ga2spyeb5u9tdq8t5Z /]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql latest 3218b38490ce 8 days ago 516MB
hello-world latest feb5d9fea6a5 3 months ago 13.3kB
centos latest 5d0da3dc9764 3 months ago 231MB
[root@iZwz9ga2spyeb5u9tdq8t5Z /]# docker run -it centos /bin/bash
[root@1b87ecec82d9 /]# ls # 查看容器内的centos,基础版本,很多命令都是不完善的
bin dev etc home lib lib64 lost+found media mnt opt proc root run
# 从容器中退回主机
[root@1b87ecec82d9 /]# exit
exit
[root@iZwz9ga2spyeb5u9tdq8t5Z /]# ls
10000 bin boot dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
2、列出所有的运行中的容器
# docker ps 命令
# 列出当前正在运行的容器
-a # 列出当前正在运行的容器+带出历史运行的容器
-n=? # 显示最近创建的容器
-aq # 只显示容器的编号
[root@iZwz9ga2spyeb5u9tdq8t5Z /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@iZwz9ga2spyeb5u9tdq8t5Z /]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1b87ecec82d9 centos "/bin/bash" 12 minutes ago Exited (0) About a minute ago gracious_hoover
1f0d63432233 centos "/bin/bash" 12 minutes ago Exited (0) 12 minutes ago busy_chaplygin
9d623a804288 hello-world "/hello" 2 days ago Exited (0) 2 days ago romantic_volhard
[root@iZwz9ga2spyeb5u9tdq8t5Z /]#
3、退出容器
exit # 直接容器停止并退出
Ctrl+p+q # 容器不停止退出
[root@iZwz9ga2spyeb5u9tdq8t5Z /]# docker run -it centos /bin/bash
[root@1b036b704f0d /]# [root@iZwz9ga2spyeb5u9tdq8t5Z /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1b036b704f0d centos "/bin/bash" 25 seconds ago Up 24 seconds
4、删除容器
docker rm 容器id # 删除指定的容器,不能删除运行的容器
docker rm -f $(docker ps -aq) # 删除所有的容器
docker ps -a -q|xargs docker rm # 也是删除所有的容器
5、启动和停止容器操作
docker start 容器id # 启动容器
docker restart 容器id # 重启容器
docker stop 容器id # 停止当前正在运行的容器
docker kill 容器id # 强制停止当前容器
常用其它命令及问题
1、后台启动容器
# 命令 docker -run -d 镜像名(后台运行)
[root@iZwz9ga2spyeb5u9tdq8t5Z /]# docker run -d centos
# 问题docker ps,发现centos停止了
# 常见的坑:docker 容器使用后台运行,就必须要有一个前台进程,docker发现没有应用,就会自动停止
# nginx,容器后台启动后,发现自己没有提供前台服务,就会立刻停止
2、查看容器中的进程信息 ps
# 命令 docker top 容器id
[root@iZwz9ga2spyeb5u9tdq8t5Z /]# docker top 30182ce61101
UID PID PPID C STIME TTY TIME CMD
root 30188 30170 0 16:15 pts/0 00:00:00 /bin/bash
3、查看镜像的元数据
# 命令 docker inspect 容器id
# 调试
[root@iZwz9ga2spyeb5u9tdq8t5Z /]# docker inspect 30182ce61101
[
{
"Id": "30182ce61101a9d5e5916ce02e6805646c620ff4b2f02e80ab54be5c9eac7728",
"Created": "2021-12-29T08:15:33.482949345Z",
"Path": "/bin/bash",
"Args": [],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 30188,
"ExitCode": 0,
"Error": "",
"StartedAt": "2021-12-29T08:15:33.790951902Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6",
"ResolvConfPath": "/var/lib/docker/containers/30182ce61101a9d5e5916ce02e6805646c620ff4b2f02e80ab54be5c9eac7728/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/30182ce61101a9d5e5916ce02e6805646c620ff4b2f02e80ab54be5c9eac7728/hostname",
"HostsPath": "/var/lib/docker/containers/30182ce61101a9d5e5916ce02e6805646c620ff4b2f02e80ab54be5c9eac7728/hosts",
"LogPath": "/var/lib/docker/containers/30182ce61101a9d5e5916ce02e6805646c620ff4b2f02e80ab54be5c9eac7728/30182ce61101a9d5e5916ce02e6805646c620ff4b2f02e80ab54be5c9eac7728-json.log",
"Name": "/distracted_shockley",
"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/38f2a3c391650918159a1d6a057cf54079618ea7ce00c6d8ff502f13be4e994e-init/diff:/var/lib/docker/overlay2/44e94db1e5c8bd1dccb0eda98963b5f7ee462c9ca8a5a16459e89cdbc8bd2d14/diff",
"MergedDir": "/var/lib/docker/overlay2/38f2a3c391650918159a1d6a057cf54079618ea7ce00c6d8ff502f13be4e994e/merged",
"UpperDir": "/var/lib/docker/overlay2/38f2a3c391650918159a1d6a057cf54079618ea7ce00c6d8ff502f13be4e994e/diff",
"WorkDir": "/var/lib/docker/overlay2/38f2a3c391650918159a1d6a057cf54079618ea7ce00c6d8ff502f13be4e994e/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "30182ce61101",
"Domainname": "",
"User": "",
"AttachStdin": true,
"AttachStdout": true,
"AttachStderr": true,
"Tty": true,
"OpenStdin": true,
"StdinOnce": true,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"Cmd": [
"/bin/bash"
],
"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": "e4c00e1daf29a3b33cff6ee446c662a06a6ca9b9977faf5196454e5ebde1af00",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/e4c00e1daf29",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "75e4fe490eb3b20b0eb77b039baea5348a5680455440d8a7fe0dc469e7690a51",
"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": "9057a93001136d8bb203d651ef95b00a3d0006ebfab0aca07f6f48002705ce4b",
[root@iZwz9ga2spyeb5u9tdq8t5Z /]#
4、进入当前正在运行的容器
# 我们通常容器都是使用后台方式运行,需要进入容器,需要一些配置
# 命令
docker exec -it 容器id bashShell
# 测试
[root@iZwz9ga2spyeb5u9tdq8t5Z /]# docker exec -it 30182ce61101 /bin/bash
[root@30182ce61101 /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
[root@30182ce61101 /]# ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 08:15 pts/0 00:00:00 /bin/bash
root 15 0 0 08:49 pts/1 00:00:00 /bin/bash
root 29 0 0 08:50 pts/2 00:00:00 /bin/bash
root 43 0 0 08:50 pts/3 00:00:00 /bin/bash
root 57 0 0 08:51 pts/4 00:00:00 /bin/bash
root 72 57 0 08:51 pts/4 00:00:00 ps -ef
# 方式二
docker attach 容器id
[root@iZwz9ga2spyeb5u9tdq8t5Z /]# docker attach 30182ce61101
正在执行当前的代码...
# docker exec # 进入容器后开启一个新的终端,可以在里面操作(常用)
# docker attach # 进入容器正在执行的终端,不会启动新的进程
5、从容器内拷贝文件到主机上
docker cp 容器id:容器内路径 目的主机路径
# 运行中的容器停止了也可以拷贝
[root@iZwz9ga2spyeb5u9tdq8t5Z /]# docker cp a1d581cec002:/home/test.java /home
[root@iZwz9ga2spyeb5u9tdq8t5Z /]# cd /home
[root@iZwz9ga2spyeb5u9tdq8t5Z home]# ls
test.java test.txt
学习练习
练习一:Docker 安装 Nginx
# 1、搜索镜像 search 建议去docker搜索,可以看到帮助文档、
# 2、下载镜像 pull
# 3、运行测试
# -d 后台运行
# --name 给容器命名
# -p 主机端口:容器端口
[root@iZwz9ga2spyeb5u9tdq8t5Z /]# docker run -d --name nginx01 -p:5050:80 nginx
48d75596ac490c02afeaa74e53863f4673cab07c1e26a9fe2ddc2b951ef930cf
[root@iZwz9ga2spyeb5u9tdq8t5Z /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
48d75596ac49 nginx "/docker-entrypoint.…" 4 seconds ago Up 3 seconds 0.0.0.0:5050->80/tcp, :::5050->80/tcp nginx01
[root@iZwz9ga2spyeb5u9tdq8t5Z /]# curl localhost:5050
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
# 进入容器
[root@iZwz9ga2spyeb5u9tdq8t5Z /]# docker exec -it nginx01 /bin/bash
root@48d75596ac49:/# whereis nginx
nginx: /usr/sbin/nginx /usr/lib/nginx /etc/nginx /usr/share/nginx
root@48d75596ac49:/# cd /etc/nginx/
root@48d75596ac49:/etc/nginx# ls
conf.d fastcgi_params mime.types modules nginx.conf scgi_params uwsgi_params
-
端口映射(暴露)
端口映射
思考问题:我们每次改动Nginx配置文件,都需要进入容器内部?始分的麻烦,我要是可以在容器外部提供一个映射路径,达到在容器外修改文件名,容器内部就可以自动修改?PS:数据卷~!
练习二:Docker 安装 Tomcat
# 官方的使用
docker run -it --rm tomcat:9.0
# 我们之前的启动是后台,停止容器后,容器还是可以查到,docker run -it --rm ,一般用来调试,用完就删除【不建议】
# 下载启动
docker pull tomcat
# 启动运行
docker run -d -p 5050:8080 --name tomcat01 tomcat
# 调试访问没有问题
# 进入容器
[root@iZwz9ga2spyeb5u9tdq8t5Z /]# docker exec -it tomcat01 /bin/bash
# 发现问题:1.linux命令少了;2.没有webapps。是阿里云镜像的原因,默认是最小的镜像,会把所有不必要的都剔除,保证最小可运行环境
思考问题:我们以后部署项目,如果每次都要进入容器很麻烦。我们是不是可以在容器外提供一个映射路径,webapps,我们在外部放置项目,就自动同步到内部就好了。
可视化
- portainer(先用这个,很少用)
- Ranchr(CI/CD再用)
1、什么是portainer?
Docker图形化管理工具!protainer是一个轻量级的管理UI,提供一个后台面板供我们操作~
# 下载并启动
docker run -d -p 5050:9000 --restart=always -v /var/run/docker.sock:/var/run/docker.sock --privileged=true --name prtainer-test portainer/portainer
# 本机测试
curl localhost:5050
image.png
选择本地的
界面展示
查看容器
Dashboard
网友评论