Docker常用命令
Docker的常用命令
一、帮助命令
# 显示docker的版本信息
docker version
# 显示docker的系统信息
docker info
# 帮助命令
docker 命令 --help
二、镜像命令
1、docker images 查看所有本地的主机上的镜像
[root@VM-0-13-centos /]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
busybox latest 491198851f0c 7 days ago 1.23MB
redis latest 621ceef7494a 6 weeks ago 104MB
postgres latest acf5fb8bfd76 6 weeks ago 314MB
hello-world latest bf756fb1ae65 14 months ago 13.3kB
sentry latest fe6992de61df 16 months ago 869MB
REPOSITORY | TAG | IMAGE ID | CREATED | SIZE |
---|---|---|---|---|
镜像的仓库源 | 镜像的标签 | 镜像的id | 镜像的创建时间 | 镜像的大小 |
# 可选项
-a, --all # 列出所有镜像
-q, --quiet # 只显示镜像的id
2、docker search 搜索镜像
[root@VM-0-13-centos /]# docker search python
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
python Python is an interpreted, interactive, objec… 5893 [OK]
# 可选项
--filter=STARS=3000 # 搜索出来的镜像就是STARS大于3000的
[root@VM-0-13-centos /]# docker search python --filter=STARS=3000
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
python Python is an interpreted, interactive, objec… 5893 [OK]
3、docker pull 下载镜像
# 下载镜像 docker pull [镜像:tag]
docker pull mysql
# 等价于
docker.io/library/mysql:latest
[root@VM-0-13-centos /]# docker pull mysql
Using default tag: latest # 如果不写tag,默认就是latest
latest: Pulling from library/mysql
a076a628af6f: Already exists
f6c208f3f991: Pull complete # 分成下载,docker images的核心 联合文件系统
88a9455a9165: Pull complete
406c9b8427c6: Pull complete
7c88599c0b25: Pull complete
25b5c6debdaf: Pull complete
43a5816f1617: Pull complete
1a8c919e89bf: Pull complete
9f3cf4bd1a07: Pull complete
80539cea118d: Pull complete
201b3cad54ce: Pull complete
944ba37e1c06: Pull complete
Digest: sha256:feada149cb8ff54eade1336da7c1d080c4a1c7ed82b5e320efb5beebed85ae8c
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest # 真实地址
# 指定版本下载
[root@VM-0-13-centos /]# docker pull mysql:5.7
5.7: Pulling from library/mysql
a076a628af6f: Already exists
f6c208f3f991: Already exists
88a9455a9165: Already exists
406c9b8427c6: Already exists
7c88599c0b25: Already exists
25b5c6debdaf: Already exists
43a5816f1617: Already exists
1831ac1245f4: Pull complete
37677b8c1f79: Pull complete
27e4ac3b0f6e: Pull complete
7227baa8c445: Pull complete
Digest: sha256:b3d1eff023f698cd433695c9506171f0d08a8f92a0c8063c1a4d9db9a55808df
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7
4、docker rmi 删除镜像
# 删除指定的容器
docker rmi -f 镜像id
# 删除多个容器
docker rmi -f 镜像id 镜像id
# 删除全部的容器
docker rmi -f $(docker images -aq)
三、容器命令
我们有了镜像才可以创建容器
linux 下载一个centos镜像来测试学习
docker pull centos
1、新建容器并启动
docker run [可选参数] image
# 参数
--name='NAME' # 容器名字 用来区分容器
-d # 后台方式运行 nohup
-it # 使用交互的方式运行 进入容器查看内容
-p # 指定容器的端口
-p ip:主机端口:容器端口
-p 主机端口:容器端口
-p 容器端口
容器端口
-P # 随机指定端口
# 启动并进入容器
[root@VM-0-13-centos /]# docker run -it centos /bin/bash
[root@0098aafe4aea /]# ls
bin etc lib lost+found mnt proc run srv tmp var
dev home lib64 media opt root sbin sys usr
# 从容器中退到主机
[root@0098aafe4aea /]# exit
exit
[root@VM-0-13-centos /]# ls
bin data etc lib lost+found mnt proc run srv tmp var
boot dev home lib64 media opt root sbin sys usr
2、列出所有运行的容器
docker ps
[root@VM-0-13-centos /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
65120b757c32 sentry "/entrypoint.sh run …" 24 hours ago Up 24 hours 9000/tcp sentry-worker-1
aea0d322c056 sentry "/entrypoint.sh run …" 24 hours ago Up 24 hours 9000/tcp sentry-cron
b82b272011a7 sentry "/entrypoint.sh run …" 24 hours ago Up 24 hours 0.0.0.0:9001->9000/tcp my-sentry
3042d4d03722 postgres "docker-entrypoint.s…" 25 hours ago Up 25 hours 5432/tcp sentry-postgres
b0db884b48a9 redis "docker-entrypoint.s…" 25 hours ago Up 25 hours 6379/tcp sentry-redis
-a # 列出当前正在运行的+历史运行过的容器
-n=? # 显示最近创建的容器
-q # 只显示容器的编号
3、退出容器
exit # 直接停止并退出
CTRL + p + q # 容器不停止退出
4、删除容器
# 删除指定的容器,不能删除正在运行的容器,如果要强制删除 rm -f
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 centos
# 问题docker ps, 发现centos停止了
# 常见的坑,docker容器使用后台运行,就必须有一个前台进程,docker发现没有应用就会自动停止
# nginx,容器启动后,发现自己没有提供服务,就会立刻停止,就没有程序了
2、查看日志
docker logs -f -t --tail 容器
# 没有日志
# 自己编写一段shell脚本
"while true;do echo yiwen;sleep 1;done"
[root@VM-0-13-centos ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a044ede503e7 centos "/bin/sh -c 'while t…" 4 seconds ago Up 4 seconds laughing_bell
# 显示日志
-tf # 显示日志
--tail number # 要显示的日志条数
docker logs -tf --tail 10 a044ede503e7
3、产看容器中的进程信息
docker top 容器
[root@VM-0-13-centos ~]# docker top a044ede503e7
UID PID PPID C STIME TTY TIME CMD
root 16705 16686 0 09:49 ? 00:00:00 /bin/sh -c while true;do echo yiwen;sleep 1;done
root 17834 16705 0 09:54 ? 00:00:00 /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 1
4、查看镜像的元数据
docker inspect 容器id
[root@VM-0-13-centos ~]# docker inspect a044ede503e7
[
{
"Id": "a044ede503e7bf3a7c8c9d6b505c9eaf245edc0425669b1120ffbb2a5d53a28f",
"Created": "2021-02-27T01:49:14.353219417Z",
"Path": "/bin/sh",
"Args": [
"-c",
"while true;do echo yiwen;sleep 1;done"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 16705,
"ExitCode": 0,
"Error": "",
"StartedAt": "2021-02-27T01:49:14.744813681Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:300e315adb2f96afe5f0b2780b87f28ae95231fe3bdd1e16b9ba606307728f55",
"ResolvConfPath": "/var/lib/docker/containers/a044ede503e7bf3a7c8c9d6b505c9eaf245edc0425669b1120ffbb2a5d53a28f/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/a044ede503e7bf3a7c8c9d6b505c9eaf245edc0425669b1120ffbb2a5d53a28f/hostname",
"HostsPath": "/var/lib/docker/containers/a044ede503e7bf3a7c8c9d6b505c9eaf245edc0425669b1120ffbb2a5d53a28f/hosts",
"LogPath": "/var/lib/docker/containers/a044ede503e7bf3a7c8c9d6b505c9eaf245edc0425669b1120ffbb2a5d53a28f/a044ede503e7bf3a7c8c9d6b505c9eaf245edc0425669b1120ffbb2a5d53a28f-json.log",
"Name": "/laughing_bell",
"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/35bea462dba8bfb14e3b8304ed1d14ebe31a48059c966ce18272d7500fd1b2fa-init/diff:/var/lib/docker/overlay2/bc4346df6b60be402228563d6b79edfd2dd0c66d71c95f1be1026f58b97ca037/diff",
"MergedDir": "/var/lib/docker/overlay2/35bea462dba8bfb14e3b8304ed1d14ebe31a48059c966ce18272d7500fd1b2fa/merged",
"UpperDir": "/var/lib/docker/overlay2/35bea462dba8bfb14e3b8304ed1d14ebe31a48059c966ce18272d7500fd1b2fa/diff",
"WorkDir": "/var/lib/docker/overlay2/35bea462dba8bfb14e3b8304ed1d14ebe31a48059c966ce18272d7500fd1b2fa/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "a044ede503e7",
"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 yiwen;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": "32126c839d0d102b9d2d39b71d99b4689989adbefb38ab97c348af6fd6de7875",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/32126c839d0d",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "4f92a28dcf4f6ab32376bffd4aa538e162498f7e53db9c1929fd12157c344b1f",
"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": "d7e8bef533e4cf816c5d821c286028783a7f00436bc8414218f3d10272659257",
"EndpointID": "4f92a28dcf4f6ab32376bffd4aa538e162498f7e53db9c1929fd12157c344b1f",
"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
}
}
}
}
]
5、进入当前正在运行的容器
# 我们容器通常都是使用后台方式运行的,需要进入容器,修改一些配置
# 命令
docker exec -it 容器id bashShell
[root@VM-0-13-centos ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a044ede503e7 centos "/bin/sh -c 'while t…" 10 minutes ago Up 10 minutes laughing_bell
[root@VM-0-13-centos ~]# docker exec -it a044ede503e7 /bin/bash
[root@a044ede503e7 /]#ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 01:49 ? 00:00:00 /bin/sh -c while true;do echo yiwen;sleep 1;done
root 675 0 0 02:00 pts/0 00:00:00 /bin/bash
root 851 1 0 02:03 ? 00:00:00 /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bi
root 852 675 0 02:03 pts/0 00:00:00 ps -ef
# 方式二
docker attach 容器id
# 执行当前的代码
exec # 进入容器后开启一个新的终端,可以在里面操作(常用)
attach # 进入容器正在执行的终端,不会启动新的进程
6、从容器内拷贝文件到主机上
docker cp 容器id:容器内路径 主机路径
[root@VM-0-13-centos home]# ls
404 MxShop yiwen yiwencc.py zhuye
# 进入docker容器内部
[root@VM-0-13-centos home]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7f1a063eb2d6 centos "/bin/bash" About a minute ago Up About a minute frosty_mclaren
[root@VM-0-13-centos home]# docker attach 7f1a063eb2d6
# 在容器内新建一个文件
[root@7f1a063eb2d6 /]# cd /home/
[root@7f1a063eb2d6 home]# ls
[root@7f1a063eb2d6 home]# touch test.py
[root@7f1a063eb2d6 home]# ls
test.py
[root@7f1a063eb2d6 home]# exit
exit
# 经文件拷贝出来到主机上
[root@VM-0-13-centos home]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7f1a063eb2d6 centos "/bin/bash" 2 minutes ago Exited (0) 8 seconds ago frosty_mclaren
[root@VM-0-13-centos home]# docker cp 7f1a063eb2d6:/home/test.py /home
[root@VM-0-13-centos home]# ls
404 MxShop test.py yiwen yiwencc.py zhuye
# 拷贝是一个手动过程,未来我们使用 -v 卷的技术,可以实现
五、练习
docker安装nginx
1、搜索镜像
docker search nginx
[root@VM-0-13-centos home]# docker search nginx
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
nginx Official build of Nginx. 14490 [OK]
jwilder/nginx-proxy Automated Nginx reverse proxy for docker con… 1971 [OK]
richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable of… 809 [OK]
jc21/nginx-proxy-manager Docker container for managing Nginx proxy ho… 152
linuxserver/nginx An Nginx container, brought to you by LinuxS… 141
tiangolo/nginx-rtmp Docker image with Nginx using the nginx-rtmp… 115 [OK]
jlesage/nginx-proxy-manager Docker container for Nginx Proxy Manager 96 [OK]
bitnami/nginx Bitnami nginx Docker Image 94 [OK]
alfg/nginx-rtmp NGINX, nginx-rtmp-module and FFmpeg from sou… 89 [OK]
nginxdemos/hello NGINX webserver that serves a simple page co… 66 [OK]
nginx/nginx-ingress NGINX Ingress Controller for Kubernetes 48
privatebin/nginx-fpm-alpine PrivateBin running on an Nginx, php-fpm & Al… 46 [OK]
nginxinc/nginx-unprivileged Unprivileged NGINX Dockerfiles 31
staticfloat/nginx-certbot Opinionated setup for automatic TLS certs lo… 19 [OK]
schmunk42/nginx-redirect A very simple container to redirect HTTP tra… 19 [OK]
nginx/nginx-prometheus-exporter NGINX Prometheus Exporter 16
centos/nginx-112-centos7 Platform for running nginx 1.12 or building … 15
centos/nginx-18-centos7 Platform for running nginx 1.8 or building n… 13
bitwarden/nginx The Bitwarden nginx web server acting as a r… 9
flashspys/nginx-static Super Lightweight Nginx Image 9 [OK]
bitnami/nginx-ingress-controller Bitnami Docker Image for NGINX Ingress Contr… 8 [OK]
mailu/nginx Mailu nginx frontend 8 [OK]
navidonskis/nginx-php5.6 Docker nginx + php5.6 on Ubuntu 7 [OK]
ansibleplaybookbundle/nginx-apb An APB to deploy NGINX 2 [OK]
wodby/nginx Generic nginx 1 [OK]
2、下载镜像
docker pull nginx
[root@VM-0-13-centos home]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
Digest: sha256:10b8cc432d56da8b61b070f4c7d2543a9ed17c2b23010b43af434fd40e2ca4aa
Status: Image is up to date for nginx:latest
docker.io/library/nginx:latest
3、启动镜像
docker run -d --name nginx01 -p 9000:80 nginx
[root@VM-0-13-centos home]# docker run -d --name nginx01 -p 9000:80 nginx
e3121d70cf04e5bf5dc5f325a2b6f92374421e03ef5e29b1e4f70fb41c4634a5
[root@VM-0-13-centos home]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e3121d70cf04 nginx "/docker-entrypoint.…" 4 seconds ago Up 3 seconds 0.0.0.0:9000->80/tcp nginx01
[root@VM-0-13-centos home]# curl localhost:9000
<!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>
4、外部访问
六、可视化
1、portainer
docker run -d -p 9000:9000 --restart=always -v /var/run/docker.sock:/var/run/docker.sock --privileged=true portainer/portainer
2、Rancher
3、什么是portainer
Docker图形化界面管理工具!提供一个后台面板供我们操作