docker 帮助命令
docker version # 显示docker的版本信息
docker info # 显示docker的 系统信息,以及包括镜像以及容器的数量
docker --help # 帮助命令
帮助文档地址:https://docs.docker.com/engine/reference/commandline
镜像命令
docker images # 查看所有本地主机上的镜像
[root@liulihui /]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest feb5d9fea6a5 14 months ago 13.3kB
centos latest 5d0da3dc9764 14 months ago 231MB
# 解释
REPOSITORY # 镜像的仓库源
TAG # 标签
IMAGE ID # 镜像id
CREATED # 创建时间
SIZE #镜像大小
# 可选项
-a, --all # 列出所以的镜像
-q, --quiet #只显示所有镜像id
docker search # 搜索镜像
[root@liulihui /]# docker search mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation?? 13544 [OK]
mariadb MariaDB Server is a high performing open sou?? 5165 [OK]
phpmyadmin phpMyAdmin - A web interface for MySQL and M?? 699 [OK]
percona Percona Server is a fork of the MySQL relati?? 595 [OK]
bitnami/mysql Bitnami MySQL Docker Image 79 [OK]
# 可选项 通过搜索过滤
-- filter=STARS=3000 #搜索出来的镜像STARS大于300的
[root@liulihui /]# docker search mysql --filter=STARS=3000
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation?? 13544 [OK]
mariadb MariaDB Server is a high performing open sou?? 5165 [OK]
docker pull #下载镜像
# 下载镜像 docker pull 镜像名[:tag]
[root@liulihui /]# docker pull mysql
Using default tag: latest # 如果不写tag 默认就是latest
latest: Pulling from library/mysql
72a69066d2fe: Pull complete # 分层下载,docker images的核心,联合文件系统
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 pull docker.io/library/mysql:latest
# 指定版本下载
[root@liulihui /]# 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
docker rmi # 删除镜像
[root@liulihui /]# docker rmi -f 镜像id #删除指定容器
[root@liulihui /]# docker rmi -f 镜像id 镜像id 镜像id 镜像id 镜像id #删除多个指定容器
[root@liulihui /]# docker rmi -f $(docker images -aq) # 删除全部容器
容器命令
说明:有了镜像才可以创建容器,下载一个centos镜像来测试
docker pull centos
新建容器并启动
docker run [可选参数] image
# 参数说明
--name="Name" 容器名 Tomcat1 Tomcat2 用来区分容器
-d 后台方式用下
-it 使用交互方式运行,进入容器可查看内容
-p 指定容器端口 -p 8080:8088
-p ip:主机端口:容器端口
-p 主机端口:容器端口(常用)
-p 容器端口
容器端口
-P 随机指定端口
# 启动,并进入容器
[root@liulihui /]# docker run -it centos /bin/bash
[root@4f9cb12be84a /]# ls # 查看容器内部cenos
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
#从容器中 退回主机
[root@4f9cb12be84a /]# exit
exit
[root@liulihui /]# ls
bin boot data dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
列出所以运行的容器
# docker ps 命令
# 列出当前正在运行的容器
-a # 列出当前正在运行的容器+历史运行的容器
-n=? # 显示最近创建的容器
-q # 只显示容器id
[root@liulihui /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@liulihui /]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4f9cb12be84a centos "/bin/bash" 6 minutes ago Exited (0) 4 minutes ago eager_heisenberg
8b36a208feeb feb5d9fea6a5 "/hello" 40 hours ago Exited (0) 40 hours ago interesting_buck
e0c946e8f264 centos "/bin/sh -c 'while t?? 2 days ago Exited (137) 2 days ago strange_meninsky
退出容器
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 # 强制停止当前运行容器
常用其他命令
后台启动容器
# 命令 docker run -d 镜像名
[root@liulihui /]# docker run -d centos
# 问题 docker ps 发现centos 已停止了
# 常见的坑,docker 启动后运行 就必须有一个前台进程,docker 发现没有,就会自动停止
查看日志
# 命令 docker logs -t -f --tail 10 容器id ,没有日志
# 编写一段脚本
[root@liulihui /]# docker run -d centos /bin/bash -c "while true;do echo liulihui;sleep 1;done"
# 显示日志
-tf # 显示日志
--tail number # 要显示日志条数
[root@liulihui /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
52c4f8dd8fae centos "/bin/bash -c 'while?? About a minute ago Up About a minute clever_roentgen
[root@liulihui /]# docker logs -tf --tail 10 52c4f8dd8fae
查看容器中进程信息ps
# 命令 docker top 容器id
[root@liulihui /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
52c4f8dd8fae centos "/bin/bash -c 'while?? 5 minutes ago Up 5 minutes clever_roentgen
[root@liulihui /]# docker top 52c4f8dd8fae
UID PID PPID C STIME
root 29814 29796 0 23:45
root 31222 29814 0 23:51
查看镜像元数据
# 命令 docker inspect 容器id
[root@liulihui /]# docker inspect 52c4f8dd8fae
[
{
"Id": "52c4f8dd8faefc09fbde950cb0c5b1cfae333a70c8a1a9bcd992950003213330",
"Created": "2022-12-03T07:45:27.111584019Z",
"Path": "/bin/bash",
"Args": [
"-c",
"while true;do echo liulihui;sleep 1;done"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 29814,
"ExitCode": 0,
"Error": "",
"StartedAt": "2022-12-03T07:45:27.743826817Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6",
"ResolvConfPath": "/var/lib/docker/containers/52c4f8dd8faefc09fbde950cb0c5b1cfae333a70c8a1a9bcd992950003213330/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/52c4f8dd8faefc09fbde950cb0c5b1cfae333a70c8a1a9bcd992950003213330/hostname",
"HostsPath": "/var/lib/docker/containers/52c4f8dd8faefc09fbde950cb0c5b1cfae333a70c8a1a9bcd992950003213330/hosts",
"LogPath": "/var/lib/docker/containers/52c4f8dd8faefc09fbde950cb0c5b1cfae333a70c8a1a9bcd992950003213330/52c4f8dd8faefc09fbde950cb0c5b1cfae333a70c8a1a9bcd992950003213330-json.log",
"Name": "/clever_roentgen",
"RestartCount": 0,
"Driver": "devicemapper",
"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": {
"DeviceId": "49",
"DeviceName": "docker-8:3-67617318-df3d09d629f86968c216b27da5cdc05c9ee465a5844064c43dcbaadd6d2df5d1",
"DeviceSize": "10737418240"
},
"Name": "devicemapper"
},
"Mounts": [],
"Config": {
"Hostname": "52c4f8dd8fae",
"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/bash",
"-c",
"while true;do echo liulihui;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": "c3924dc5efdf4ce2852a778d5bc1f1c295953705947065b1262737ecb1ebabf3",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/c3924dc5efdf",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "cbc6672130eff917bffa69f37af2774139df270643f59ebd4f2c424ff8178238",
"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": "9f6c5897a7b7ea30a1acd25fb193831f900143cf2bdda25a06ccf982721d6851",
"EndpointID": "cbc6672130eff917bffa69f37af2774139df270643f59ebd4f2c424ff8178238",
"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 exce -it 容器id /bin/bash
# 命令
[root@liulihui /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
52c4f8dd8fae centos "/bin/bash -c 'while?? 15 minutes ago Up 15 minutes clever_roentgen
[root@liulihui /]# docker exec -it 52c4f8dd8fae /bin/bash
[root@52c4f8dd8fae /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
# 命令
docker attach 容器id #
# 说明
docker exce -it 容器id # 进入容器后启动一个新的终端,可以再里面操作(常用)
docker attach 容器id # 进入容器正在执行的终端,不会启动新的进程
从容器内拷贝文件到主机上
# 命令
docker cp 容器id:容器内路径 目的路径
# 查看当前运行的容器
[root@liulihui home]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
52c4f8dd8fae centos "/bin/bash -c 'while?? 35 minutes ago Up 19 seconds clever_roentgen
# 进入容器内部
[root@liulihui home]# docker exec -it 52c4f8dd8fae /bin/bash
[root@52c4f8dd8fae /]# cd /home/
# 新建test.java 文件
[root@52c4f8dd8fae home]# touch test.java
# 退出容器
[root@52c4f8dd8fae home]# exit
exit
[root@liulihui home]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
52c4f8dd8fae centos "/bin/bash -c 'while?? 37 minutes ago Up About a minute clever_roentgen
[root@liulihui home]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
52c4f8dd8fae centos "/bin/bash -c 'while?? 37 minutes ago Up 2 minutes clever_roentgen
9e57f030b1b4 centos "/bin/bash -c 'while?? 38 minutes ago Exited (1) 38 minutes ago cool_spence
d305e418a158 centos "/bin/bash" 50 minutes ago Exited (0) 3 minutes ago trusting_greider
# 拷贝test.java 文件到主机上
[root@liulihui home]# docker cp 52c4f8dd8fae:/home/test.java /home/
[root@liulihui home]# ls
centos7 test.java