Docker的常用命令
1. 帮助命令
docker version # 显示docker的版本信息
docker info # 显示docker的系统信息,包括镜像和容器的数量
docker 命令 --help # 帮助命令
帮助文档的地址: https://docs.docker.com/engine/reference/commandline/docker/
2. 镜像指令
2.1 docker images 查看所有本地的主机上的镜像
[root@abc123 /]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu 18.04 c090eaba6b94 6 days ago 63.3MB
hello-world latest bf756fb1ae65 13 months ago 13.3kB
# 解析
REPOSITORY 镜像的仓库源
TAG 镜像的标签
IMAGE ID 镜像的ID
CREATE 镜像的创建时间
SIZE 镜像的大小
# 可选项
Options:
-a, --all # 列出所有进行
-q, --quiet # 只显示镜像的id
2.2 docker search搜索镜像
[root@abc123 /]# docker search nginx
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
nginx Official build of Nginx. 14356 [OK]
jwilder/nginx-proxy Automated Nginx reverse proxy for docker con… 1948 [OK]
richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable of… 802 [OK]
linuxserver/nginx An Nginx container, brought to you by LinuxS… 140
# 可选项,通过收藏来过滤
--filter=STARS=3000 #搜索出来的镜像就是START大于3000
[root@abc123 /]# docker search nginx --filter=STARS=3000
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
nginx Official build of Nginx. 14356 [OK]
[root@abc123 /]#
2.3 docker pull 下载镜像
# 下载镜像 docker pull 镜像名[:tag] 版本号
[root@abc123 /]# docker pull mysql
Using default tag: latest
latest: Pulling from library/mysql
a076a628af6f: Pull complete
f6c208f3f991: Pull complete
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
# 等价
docker pull mysql
docker pull docker.io/library/mysql:latest
# 指定版本下载
[root@abc123 /]# docker pull mysql:5.7
[root@abc123 /]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu 18.04 c090eaba6b94 6 days ago 63.3MB
mysql latest c8562eaf9d81 9 days ago 546MB
hello-world latest bf756fb1ae65 13 months ago 13.3kB
2.4 删除镜像 docker rmi
$# docker rmi -f 容器id
3. 容器命令
说明:有了进行才可以创建容器,linux, 下载一个centos镜像来测试学习
docker pull centos
3.1 新建容器并启动
docker run --help
docker run [可选参数] image
# 参数说明
--name="Name" # 容器名字 tomcat01 tomcat02, 用了区分容器
-d # 后台方式运行, ja nohup
-it # 使用交互方式运行,进入容器查看内容
-p # 指定容器的端口 -p 8080:8080
-p ip:主机端口:容器端口
-p 主机端口:容器端口 (常用)
-p 容器端口
-P # 随机指定端口
# 测试,启动并进入容器
[root@abc123 /]# docker run -it ubuntu:18.04 /bin/bash
root@9ec2ccc58e4f:/# ls # 9ec2ccc58e4f对接ubuntu镜像id 查看容器内的centos 很多命令不完善
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
root@9ec2ccc58e4f:/#
内部的Ubuntu与外面的主机系统,没有任何关系 如下:
# 从容器中退回主机
root@9ec2ccc58e4f:/# exit
exit
[root@abc123 /]#ls
bin boot dev etc home lib lib64 lost+found media mnt opt proc root run sbin source srv sys tmp usr var
[root@abc123 /]#
3.2 列出所有运行的容器
# docker ps 命令
# 列出当前正在运行的容器
-a # 列出正在运行的容器 + 带出历史运行过的容器
-n=? # 显示最近创建的容器
-q # 只显示容器编号
[root@abc123 /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@abc123 /]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9ec2ccc58e4f ubuntu:18.04 "/bin/bash" 8 minutes ago Exited (0) 4 minutes ago recursing_banach
a1d2610d2451 mysql "docker-entrypoint.s…" 31 minutes ago Exited (1) 31 minutes ago funny_ritchie
a819234ad088 hello-world "/hello" About an hour ago Exited (0) About an hour ago clever_burnell
a8920ae29c07 hello-world "/hello" 47 hours ago Exited (0) 47 hours ago angry_shtern
bd1e3053b0bd hello-world "/hello" 47 hours ago Exited (0) 47 hours ago charming_yalow
[root@abc123 /]#
3.3 退出容器
exit # 直接容器停止并退出
Ctrl + P + Q # 容器不停止退出
3.4 删除容器
#docker rm 容器id # 删除指定容器,不能删除正在运行的容器
#docker rm -f $(docker ps -aq)
3.5 启动和停止容器的操作
docker start 容器id # 启动容器
docker restart 容器id # 重启容器
docker stop 容器id # 停止当前正在进行的容器
docker kill容器id # 强制停止当前容器
4. 常用其他命令(最重要)
4.1 后台启动容器
# 命令 docker run -d 镜像名!
[root@abc123 /]# docker run -d ubuntu:18.04
# 问题是docker ps, 发现ubuntu:18.04停止了
# 常见的坑:docker 容器使用后台运行,就不行有一个前台进程,docker发现没有应用了,就会自动停止
#nginx,容器启动后,发现自己没有提供服务,就会立刻停止,就是没有程序了
4.2 查看日志
docker log -f -t --tail 容器,没有日志
# 自己编写一段shell脚本
[root@abc123 /]# docker run -d ubuntu:18.04 /bin/sh -c "while true;do echo baidu_ CSDN;sleep 1;done"
[root@abc123 /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
48fa5f46316a ubuntu:18.04 "/bin/sh -c 'while t…" 6 seconds ago Up 4 seconds fervent_cartwright
# 显示日志
-tf # 显示日志
--tail number # 要显示的日志条数
[root@abc123 /]# docker logs -tf --tail 10 48fa5f46316a
2021-01-28T03:22:00.734215574Z baidu_CSDN
2021-01-28T03:22:01.735742403Z baidu_CSDN
2021-01-28T03:22:02.737314554Z baidu_CSDN
2021-01-28T03:22:03.738787507Z baidu_CSDN
2021-01-28T03:22:04.740148201Z baidu_CSDN
2021-01-28T03:22:05.741556806Z baidu_CSDN
2021-01-28T03:22:06.743063490Z baidu_CSDN
4.3 显示容器中的进程信息 ps
# 命令 docker top 容器id
[root@abc123 /]# docker top 48fa5f46316a
UID PID PPID C STIME TTY TIME
root 6650 103126 0 11:25 ?
root 103126 103104 0 11:18 ?
4.4 查看镜像元数据
# 命令
docker inspect 容器id
[root@abc123 /]# docker inspect 48fa5f46316a
[
{
"Id": "48fa5f46316a4d47122631d760427ee0ff377b54d33472b0b0d6cdedb8487ad1",
"Created": "2021-01-28T03:18:45.791614518Z",
"Path": "/bin/sh",
"Args": [
"-c",
"while true;do echo baidu_CSDN;sleep 1;done"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 103126,
"ExitCode": 0,
"Error": "",
"StartedAt": "2021-01-28T03:18:46.362070952Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:c090eaba6b9480dcd1daf063e4efe271711745c21a73c53bdc94c8d9610d2fd9",
"ResolvConfPath": "/var/lib/docker/containers/48fa5f46316a4d47122631d760427ee0ff377b54d33472b0b0d6cdedb8487ad1/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/48fa5f46316a4d47122631d760427ee0ff377b54d33472b0b0d6cdedb8487ad1/hostname",
"HostsPath": "/var/lib/docker/containers/48fa5f46316a4d47122631d760427ee0ff377b54d33472b0b0d6cdedb8487ad1/hosts",
"LogPath": "/var/lib/docker/containers/48fa5f46316a4d47122631d760427ee0ff377b54d33472b0b0d6cdedb8487ad1/48fa5f46316a4d47122631d760427ee0ff377b54d33472b0b0d6cdedb8487ad1-json.log",
"Name": "/fervent_cartwright",
"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/87d8e10698174fbe832260fa36e9ab5cf7415b980ee5338e6e394c7eacc56b9c-init/diff:/var/lib/docker/overlay2/f11f93ac58f7fe3dce26c7767d07d3959dc9914d937b2a27ed058e95963cbdd7/diff:/var/lib/docker/overlay2/fec274c0a7f57df2d2347620715ee6e160958ee8b62bb2fce0d4428f9cdf60d0/diff:/var/lib/docker/overlay2/00e3bc601aa74dca75e728b385ab351212048468fa963eae6c5354b380d13065/diff",
"MergedDir": "/var/lib/docker/overlay2/87d8e10698174fbe832260fa36e9ab5cf7415b980ee5338e6e394c7eacc56b9c/merged",
"UpperDir": "/var/lib/docker/overlay2/87d8e10698174fbe832260fa36e9ab5cf7415b980ee5338e6e394c7eacc56b9c/diff",
"WorkDir": "/var/lib/docker/overlay2/87d8e10698174fbe832260fa36e9ab5cf7415b980ee5338e6e394c7eacc56b9c/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "48fa5f46316a",
"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 baidu_CSDN;sleep 1;done"
],
"Image": "ubuntu:18.04",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {}
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "3c8c5c93d5cf8424b62f74421f0cd8979bfa8738ac997c898aa94e66b6af0d1e",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/3c8c5c93d5cf",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "8ce5aa38ff9dd5384eac278d2565b05a2fa81c209f59f6d4351f1498b8d50cfa",
"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": "7b266db4e2d5ea50f8ea09a4bc6274a2c20448193be428988e4d0936c42294f8",
"EndpointID": "8ce5aa38ff9dd5384eac278d2565b05a2fa81c209f59f6d4351f1498b8d50cfa",
"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
}
}
}
}
]
4.5 进入当前正在进行的容器
# 我们通常都是使用后台的方式进行的,需要进入容器,修改一些配置
# 命令
docke exec -it 容器id bashShell
# 测试
[root@abc123 /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
48fa5f46316a ubuntu:18.04 "/bin/sh -c 'while t…" 14 minutes ago Up 14 minutes fervent_cartwright
[root@abc123 /]# docker exec -it 48fa5f46316a /bin/bash
root@48fa5f46316a:/# ls
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
root@48fa5f46316a:/# ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 03:18 ? 00:00:00 /bin/sh -c while true;do echo baidu CSDN;sleep 1;done
root 891 0 0 03:33 pts/0 00:00:00 /bin/bash
root 923 1 0 03:33 ? 00:00:00 sleep 1
root 924 891 0 03:33 pts/0 00:00:00 ps -ef
# 方式二
docker attach 容器id
# 测试
[root@abc123 /]# docker attach 48fa5f46316a
正在执行当前的代码...
# docker exec # 进入容器后开始一个新的终端,可以在里面操作(常用)
# docker attach # 进入容器正在执行的终端,不会启动新的进程!
4.6 从容器内拷贝文件到主机上
docker cp 容器id:容器内路径 目的的主机路径
# 当前主机目录下,没有test.py
[root@abc123 /]# ls
anaconda3 TensorRT
condarc.txt inference_engine_demos_build nginx-packages script
[root@abc123 /]# docker run -it ubuntu:18.04 /bin/bash
root@cbc8aca5298d:/# [root@abc123 /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
cbc8aca5298d ubuntu:18.04 "/bin/bash" 13 seconds ago Up 12 seconds pedantic_faraday
[root@abc123 /]# cd /home
# 开始并进入容器
[root@abc123 home]# docker attach cbc8aca5298d
root@cbc8aca5298d:/# cd /home
# 在容器内新建一个文件test.py
root@cbc8aca5298d:/home# touch test.py
root@cbc8aca5298d:/home# ls
test.py
root@cbc8aca5298d:/home# exit
exit
[root@abc123 home]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@abc123 home]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
cbc8aca5298d ubuntu:18.04 "/bin/bash" 3 minutes ago Exited (0) 13 seconds ago pedantic_faraday
# 将test.py拷贝到主机上
[root@abc123 home]# docker cp cbc8aca5298d:/home/test.py /home/
[root@abc123 home]# ls
anaconda3 TensorRT test.py
condarc.txt inference_engine_demos_build nginx-packages script
# 拷贝是一个手动过程,为了可以使用 -V 卷的技术,实现自动同步 /home/ /home/