帮助命令
docker version # 显示docker版本信息
docker info # 显示docker的系统信息
docker 命令 --help # 帮助命令
帮助文档位置:https://docs.docker.com/reference/
镜像命令
docker images 查看所有本地的主机上的镜像
帮助文档地址:https://docs.docker.com/engine/reference/commandline/images/
[hekai@localhost docker]$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest d1165f221234 31 hours ago 13.3kB
# 解释
REPOSITORY 镜像的仓库源
TAG 镜像的标签
IMAGE ID 镜像的id
CREATED 镜像的创建时间
SIZE 镜像的大小
# 不会可以使用--help查看选项
[hekai@localhost docker]$ docker images --help
Usage: docker images [OPTIONS] [REPOSITORY[:TAG]]
List images
Options:
-a, --all Show all images (default hides intermediate images)
--digests Show digests
-f, --filter filter Filter output based on conditions provided
--format string Pretty-print images using a Go template
--no-trunc Don't truncate output
-q, --quiet Only show numeric IDs
# 所有命令的学习都类似
docker search 被搜索的镜像名
dockerhub也可以搜索:https://hub.docker.com/
# 命令行搜索mysql
[hekai@localhost ~]$ docker images search mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 10583 [OK]
mariadb MariaDB Server is a high performing open sou… 3960 [OK]
mysql/mysql-server Optimized MySQL Server Docker images. Create… 776 [OK]
网页搜索
# 参数查看
[hekai@localhost ~]$ docker search --help
Usage: docker search [OPTIONS] TERM
Search the Docker Hub for images
Options:
-f, --filter filter Filter output based on conditions provided
--format string Pretty-print search using a Go template
--limit int Max number of search results (default 25)
--no-trunc Don't truncate output
# 使用--filter搜索过滤
[hekai@localhost ~]$ docker search mysql --filter=STARS=3000
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 10583 [OK]
mariadb MariaDB Server is a high performing open sou… 3960 [OK]
[hekai@localhost ~]$ docker search mysql --filter=STARS=5000
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 10583 [OK]
docker pull 要下载的镜像名
# docker pull 镜像名:tag
# 首先看一下--help
[hekai@localhost ~]$ docker pull --help
Usage: docker pull [OPTIONS] NAME[:TAG|@DIGEST]
Pull an image or a repository from a registry
Options:
-a, --all-tags Download all tagged images in the repository
--disable-content-trust Skip image verification (default true)
--platform string Set platform if server is multi-platform capable
-q, --quiet Suppress verbose output
# 尝试使用下载mysql
[hekai@localhost ~]$ docker pull mysql
Using default tag: latest # 不加tag,默认就是latest
latest: Pulling from library/mysql
45b42c59be33: Pull complete # 分层下载,docker image核心:联合文件系统
b4f790bd91da: Pull complete
325ae51788e9: Pull complete
adcb9439d751: Pull complete
174c7fe16c78: Pull complete
698058ef136c: Pull complete
4690143a669e: Pull complete
f7599a246fd6: Pull complete
35a55bf0c196: Pull complete
790ac54f4c47: Pull complete
b0ddd5d1b543: Pull complete
1aefd67cb33d: Pull complete
Digest: sha256:7706e4c382be813b58ef514f2bdac747cd463a6866c6c81165d42a1d0e4fe947 # 签名
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest # 真实地址
# 上面命令等价于
docker pull docker.io/library/mysql:latest
# 指定版本下载,要从dockerhub查看,要有的版本添加tag
[hekai@localhost ~]$ docker pull mysql:5.7
5.7: Pulling from library/mysql
45b42c59be33: Already exists
b4f790bd91da: Already exists
325ae51788e9: Already exists
adcb9439d751: Already exists
174c7fe16c78: Already exists
698058ef136c: Already exists
4690143a669e: Already exists
66676c1ab9b3: Pull complete
25ebf78a38b6: Pull complete
a6510e5d6228: Pull complete
90ca045d52c5: Pull complete
Digest: sha256:9fc60b229633ce1d1f2ee306705152d4b001056fb27c1b5debe23a732df72b70
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7
docker rmi 要删除的镜像名
# 查看帮助文档可以使用参数进行删除处理
[hekai@localhost ~]$ docker rmi --help
Usage: docker rmi [OPTIONS] IMAGE [IMAGE...]
Remove one or more images
Options:
-f, --force Force removal of the image
--no-prune Do not delete untagged parents
# 使用镜像ID来删除
[hekai@localhost ~]$ docker rmi -f d54bd1054823
Untagged: mysql:5.7
Untagged: mysql@sha256:9fc60b229633ce1d1f2ee306705152d4b001056fb27c1b5debe23a732df72b70
Deleted: sha256:d54bd1054823adac521a08c227f8f7106e93158184f0978eda0eb6ab7b4a5b38
Deleted: sha256:81df52d6b6986ce243ecad13c5a33d6685cca38a7f246e275c20b459068ac8bc
Deleted: sha256:76c8845eec0bc7703e321f1f5a312033cb73d4bbea7212517cdfd84ef52f532d
Deleted: sha256:b851a484b18c5d3d25497260c111631ae3adf924eb10baa533b2a5b03b339d1a
Deleted: sha256:b5133b076285236e7fd98c42c1f18f57e2b4ed98daaed7b0afb3b98b804d6f25
# 删除所有镜像
[hekai@localhost ~]$ docker rmi -f d54bd1054823
# 有项目时谨慎操作,删除后没有镜像了
[hekai@localhost ~]$ docker images -a
REPOSITORY TAG IMAGE ID CREATED SIZE
[hekai@localhost ~]$
容器命令
说明:有了镜像才可以创建容器,linux,下载一个centos镜像来测试学习
[hekai@localhost ~]$ docker pull centos
Using default tag: latest
latest: Pulling from library/centos
7a0437f04f83: Pull complete
Digest: sha256:5528e8b1b1719d34604c87e11dcd1c0a20bedf46e83b5632cdeac91b8c04efc1
Status: Downloaded newer image for centos:latest
docker.io/library/centos:latest
新建容器并启动
docker run [可选参数] imageName
# 参数很多,详细的看--help说明
--name="Name" 容器名字(用于区分)
-d 后台方式运行
-it 使用交互方式运行,并进入容器查看内容
-p 指定容器的端口 -p 8080:8080
-p ip:主机端口:容器端口
-p 主机端口:容器端口(常用)
-p 容器端口
-P 随机指定端口(大写P)
# 运行centos
[hekai@localhost ~]$ docker run -it centos /bin/bash
[root@fe6c8790977f /]#
# 变为root@fe6c8790977f /了,表明进入centos系统了
# 退出容器,回到主机
[root@fe6c8790977f /]# exit
exit
[hekai@localhost ~]$
列出所有正在运行的容器
# docker ps命令
# 列出当前运行的容器
-a # 列出当前运行的容器+历史运行过的容器
-n=? # 显示最近创建的容器
-q # 只显示容器的ID
# 具体查看
[hekai@localhost ~]$ docker ps --help
Usage: docker ps [OPTIONS]
List containers
Options:
-a, --all Show all containers (default shows just running)
-f, --filter filter Filter output based on conditions provided
--format string Pretty-print containers using a Go template
-n, --last int Show n last created containers (includes all states) (default -1)
-l, --latest Show the latest created container (includes all states)
--no-trunc Don't truncate output
-q, --quiet Only display container IDs
-s, --size Display total file sizes
# 使用
# 查看正在运行的容器
[hekai@localhost ~]$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[hekai@localhost ~]$
# 上面exit了,所以没有在运行的了
# 查看运行过的容器
[hekai@localhost ~]$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fe6c8790977f centos "/bin/bash" 3 minutes ago Exited (0) About a minute ago xenodochial_rhodes
82b433f723cd d1165f221234 "/hello" 21 hours ago Exited (0) 21 hours ago romantic_herschel
退出容器
exit # 容器直接停止并退出了
Ctrl+P+Q # 容器不停止并退出
# 启动容器并使用Ctrl+P+Q退出
[hekai@localhost ~]$ docker run -it centos /bin/bash
[root@2b5761a51caf /]# [hekai@localhost ~]$
[hekai@localhost ~]$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2b5761a51caf centos "/bin/bash" 18 seconds ago Up 18 seconds dreamy_brahmagupta
删除容器
docker rm 容器id # 删除指定容器
docker rm -f $(docker ps -aq) # 删除所有容器
docker ps -a | xargs docker rm # 删除所有容器
# 使用
# 首先查看历史容器运行情况
[hekai@localhost ~]$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2b5761a51caf centos "/bin/bash" 2 minutes ago Up 2 minutes dreamy_brahmagupta
fe6c8790977f centos "/bin/bash" 12 minutes ago Exited (0) 11 minutes ago xenodochial_rhodes
82b433f723cd d1165f221234 "/hello" 21 hours ago Exited (0) 21 hours ago romantic_herschel
# 删除fe6c8790977f
[hekai@localhost ~]$ docker rm fe6c8790977f
fe6c8790977f
[hekai@localhost ~]$
# 删除2b5761a51caf
[hekai@localhost ~]$ docker rm 2b5761a51caf
Error response from daemon: You cannot remove a running container 2b5761a51cafa9c659b40807fc76df3198063c5592ad7d3d1aec7501d6f9d546. Stop the container before attempting removal or force remove
[hekai@localhost ~]$
# 会报错,因为在后台运行
# 使用docker rm -f $(docker ps -aq)删除所有容器
[hekai@localhost ~]$ docker rm -f $(docker ps -aq)
2b5761a51caf
82b433f723cd
[hekai@localhost ~]$
# 已经全部删除
[hekai@localhost ~]$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[hekai@localhost ~]$
启动和停止容器操作
docker start 容器id
docker restart 容器id
docker stop 容器id
docker kill 容器id
# docker start与docker stop的使用
[hekai@localhost ~]$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[hekai@localhost ~]$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8beb48e73fc4 centos "/bin/bash" 10 seconds ago Exited (0) 7 seconds ago objective_lovelace
[hekai@localhost ~]$ docker start 8beb48e73fc4
8beb48e73fc4
[hekai@localhost ~]$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8beb48e73fc4 centos "/bin/bash" 35 seconds ago Up 3 seconds objective_lovelace
[hekai@localhost ~]$ docker stop 8beb48e73fc4
8beb48e73fc4
[hekai@localhost ~]$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
常用其他命令
后台启动容器
# 命令docker run -d 镜像名
[hekai@localhost ~]$ docker run -d centos
dc8302e323d9458b1fd5953b4f3e85b62719cd449d9ecca664a878a9b3eccbfb
[hekai@localhost ~]$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[hekai@localhost ~]$
# docker ps发现centos停止了!
# 常见的坑:docker容器后台运行,必须要有前台进程,没有就会停止
# 比如nginx,容器后台启动后,发现自己没有提供服务,就会立刻停止
查看日志
# 查看log最后10行
# 这个id对应的容器要运行着的有输出的容器
# docker logs -f -t --tail 10 容器id
# 这里写一个死循环演示
docker run -d centos /bin/bash -c "while true;do echo kuangshen;sleep 2;done"
[hekai@localhost ~]$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
91c8a4e888c2 centos "/bin/bash -c 'while…" 6 seconds ago Up 4 seconds relaxed_kilby
# 显示日志
-tf # 显示日志
--tail number # 要显示的日志条数
[hekai@localhost ~]$ docker logs -tf --tail 10 91c8a4e888c2
2021-03-07T08:32:32.424353088Z kuangshen
2021-03-07T08:32:34.427269919Z kuangshen
2021-03-07T08:32:36.429872414Z kuangshen
2021-03-07T08:32:38.432673709Z kuangshen
2021-03-07T08:32:40.435887830Z kuangshen
2021-03-07T08:32:42.438803465Z kuangshen
2021-03-07T08:32:44.441780459Z kuangshen
进程命令
# 命令docker top 容器id
[hekai@localhost ~]$ docker top 91c8a4e888c2
UID PID PPID C STIME TTY TIME CMD
root 1485 1456 0 16:32 ? 00:00:00 /bin/bash -c while true;do echo kuangshen;sleep 2;done
root 3700 1485 0 16:36 ? 00:00:00 /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 2
查看镜像的元数据
# 命令docker inspect 容器id
[hekai@localhost ~]$ docker inspect 91c8a4e888c2
[
{
"Id": "91c8a4e888c21ff161314e5e895cfac2e60f2bcf3fcaf35cf2af0b1705ba5a26",
"Created": "2021-03-07T08:32:09.706013152Z",
"Path": "/bin/bash",
"Args": [
"-c",
"while true;do echo kuangshen;sleep 2;done"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 1485,
"ExitCode": 0,
"Error": "",
"StartedAt": "2021-03-07T08:32:10.389729529Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:300e315adb2f96afe5f0b2780b87f28ae95231fe3bdd1e16b9ba606307728f55",
"ResolvConfPath": "/var/lib/docker/containers/91c8a4e888c21ff161314e5e895cfac2e60f2bcf3fcaf35cf2af0b1705ba5a26/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/91c8a4e888c21ff161314e5e895cfac2e60f2bcf3fcaf35cf2af0b1705ba5a26/hostname",
"HostsPath": "/var/lib/docker/containers/91c8a4e888c21ff161314e5e895cfac2e60f2bcf3fcaf35cf2af0b1705ba5a26/hosts",
"LogPath": "/var/lib/docker/containers/91c8a4e888c21ff161314e5e895cfac2e60f2bcf3fcaf35cf2af0b1705ba5a26/91c8a4e888c21ff161314e5e895cfac2e60f2bcf3fcaf35cf2af0b1705ba5a26-json.log" ,
"Name": "/relaxed_kilby",
"RestartCount": 0,
"Driver": "overlay2",
"Platform": "linux",
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile": "docker-default",
"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/167abb722868659ced56842f64195a313c1df979ab65eb054d4a8856ecab6467-init/diff:/var/lib/docker/overlay2/1db1c788013b606e98d68285613ce492 71037dc780ba7121123b24ba72905877/diff",
"MergedDir": "/var/lib/docker/overlay2/167abb722868659ced56842f64195a313c1df979ab65eb054d4a8856ecab6467/merged",
"UpperDir": "/var/lib/docker/overlay2/167abb722868659ced56842f64195a313c1df979ab65eb054d4a8856ecab6467/diff",
"WorkDir": "/var/lib/docker/overlay2/167abb722868659ced56842f64195a313c1df979ab65eb054d4a8856ecab6467/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "91c8a4e888c2",
"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 kuangshen;sleep 2;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": "34258a3791a3fcd90ffd0841e627831f3c6d347a78e04bb9ef95780b67900da4",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/34258a3791a3",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "2de0d2e9babd0ae3692342fcc37e40504d364ef239ba0177024a686d099fe904",
"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": "1d62f1dd42f99e2215656458889ec868c208dfe4ac5fb9510b13185d218ec6c5",
"EndpointID": "2de0d2e9babd0ae3692342fcc37e40504d364ef239ba0177024a686d099fe904",
"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
[hekai@localhost ~]$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
91c8a4e888c2 centos "/bin/bash -c 'while…" 9 minutes ago Up 9 minutes relaxed_kilby
[hekai@localhost ~]$ docker exec -it 91c8a4e888c2 /bin/bash
[root@91c8a4e888c2 /]#
# 方式二:docker attach 容器id
[hekai@localhost ~]$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
91c8a4e888c2 centos "/bin/bash -c 'while…" 12 minutes ago Up 12 minutes relaxed_kilby
[hekai@localhost ~]$ docker attach 91c8a4e888c2
kuangshen
kuangshen
kuangshen
kuangshen
# 然后进入那个循环中没法出来了,要重启终端!删除容器
[hekai@localhost ~]$ docker rm -f $(docker ps -aq)
两种方式进入容器的区别
docker exec # 进入容器后开启一个新的终端,可以在里面操作(常用)
docker attach # 进入容器正在执行的终端,不会启动新的进程!
从容器内拷贝文件到主机
# docker cp 容器id:容器内路径 目的主机路径
# 启动一个容器,然后Ctrl+P+Q不停止运行退出
[hekai@localhost ~]$ docker run -it centos /bin/bash
[root@1a6459ce2109 home]# cd home
[root@1a6459ce2109 home]# touch yinbinqiu.go
[root@1a6459ce2109 /]# exit
# 查看运行过的容器
[hekai@localhost test_local]$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1a6459ce2109 centos "/bin/bash" 7 minutes ago Exited (0) 3 minutes ago brave_antonelli
[hekai@localhost ~]$ mkdir /home/hekai/test_local
[hekai@localhost ~]$ docker cp 1a6459ce2109:/home/yinbinqiu.go /home/hekai/test_local
[hekai@localhost ~]$ cd /home/hekai/test_local
[hekai@localhost test_local]$ ls
yinbinqiu.go
[hekai@localhost test_local]$
# 成功将docker中的文件拷贝到本机!
# 这个例子是手动操作,后面可以使用 -v 卷的技术,自动同步文件夹
学习方式:按照视频将所有命令敲一遍,记录笔记!
参考:https://www.bilibili.com/video/BV1og4y1q7M4?p=12&spm_id_from=pageDriver