一,如何启动与停止引擎服务
#启动docker systemctl start docker #停止docker systemctl stop docker #重启docker systemctl restart docker #查看docker状态 systemctl status docker
二,docker引擎安装校验与查看docker的详细信息
#查看docker概要信息 docker info #查看docker帮助文档 docker –help #安装校验 docker -v #docker的版本号 docker version #docker的详细信息
配置镜像加速
可以通过配置国内镜像源的方式,从国内获取镜像,提高拉取速度。这里介绍中国科学技术大学 (LUG@USTC)的开源镜像:
1.编辑文件 daemon.json
1. mkdir -p /etc/docker 2. vi /etc/docker/daemon.json
2.在文件中输入以下内容并保存
1. { 2. "registry-mirrors": ["http://hub-mirror.c.163.com", 3. "https://docker.mirrors.ustc.edu.cn"] 4. }
3.重新加载配置信息及重启 Docker 服务
1. # 重新加载某个服务的配置文件 2. systemctl daemon-reload 3. # 重新启动 4. docker sudo systemctl restart docker
拉取hello-world镜像的流程
docker run hello-world 命令执行流程图如下
三,Docker命令
1.查看镜像
[root@localhost ~]# systemctl start docker [root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE mysql 5.7 eef0fab001e8 3 weeks ago 495MB hello-world latest feb5d9fea6a5 14 months ago 13.3kB centos 7 eeb6ee3f44bd 14 months ago 204MB
REPOSITORY :镜像在仓库中的名称,本文中以后都简称镜像名称
TAG :镜像标签
IMAGE ID :镜像
ID CREATED :镜像的创建日期(不是获取该镜像的日期)
SIZE :镜像大小
注:这些镜像都是存储在 Docker 宿主机的 /var/lib/docker 目录下。
2.搜索镜像
如果你需要从网络中查找需要的镜像,可以通过以下命令搜索。
格式:docker search 镜像名称
[root@localhost ~]# docker search mysql NAME DESCRIPTION STARS OFFICIAL AUTOMATED mysql MySQL is a widely used, open-source relation… 13533 [OK] mariadb MariaDB Server is a high performing open sou… 5163 [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] databack/mysql-backup Back up mysql databases to... anywhere! 76 linuxserver/mysql-workbench 45 ubuntu/mysql MySQL open source fast, stable, multi-thread… 38 linuxserver/mysql A Mysql container, brought to you by LinuxSe… 38 circleci/mysql MySQL is a widely used, open-source relation… 28 google/mysql MySQL server for Google Compute Engine 22 [OK] rapidfort/mysql RapidFort optimized, hardened image for MySQL 13 bitnami/mysqld-exporter 4 ibmcom/mysql-s390x Docker image for mysql-s390x 2 vitess/mysqlctld vitess/mysqlctld 1 [OK] newrelic/mysql-plugin New Relic Plugin for monitoring MySQL databa… 1 [OK] hashicorp/mysql-portworx-demo 0 rapidfort/mysql-official RapidFort optimized, hardened image for MySQ… 0 docksal/mysql MySQL service images for Docksal - https://d… 0 mirantis/mysql 0 rapidfort/mysql8-ib RapidFort optimized, hardened image for MySQ… 0 cimg/mysql 0 drud/mysql 0 silintl/mysql-backup-restore Simple docker image to perform mysql backups… 0 [OK] corpusops/mysql https://github.com/corpusops/docker-images/ 0
NAME :镜像名称
DESCRIPTION :镜像描述
STARS :用户评价,反应一个镜像的受欢迎程度
OFFICIAL :是否为官方构建
AUTOMATED :自动构建,表示该镜像由
Docker Hub 自动构建流程创建的。
3.拉取镜像
拉取镜像就是从中央仓库下载镜像到本地。
格式:docker pull 镜像名称
通过查看 tag 信息,如果我们要下载 centos8的镜像
[root@localhost ~]# docker pull centos:8 8: Pulling from library/centos a1d0c7532777: Pull complete Digest: sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177 Status: Downloaded newer image for centos:8 docker.io/library/centos:8
4.删除镜像
# 删除单个镜像
docker rmi 镜像ID
# 删除多个镜像
docker rmi 镜像ID 镜像ID 镜像ID
注:docker images -q 可以查询到所有镜像的 ID,通过组合命令可以实现删除所有镜像的操作。
docker rmi `docker images -q`
注意:如果通过某个镜像创建了容器,则该镜像无法删除。
解决办法:先删除镜像中的容器,再删除该镜像。
四,容器相关命令
1.查看容器
查看正在运行的容器。
1. [root@localhost ~]# docker ps 2. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
字段说明:
CONTAINER ID :容器 ID
IMAGE :所属镜像
COMMAND :
CREATED :创建时间
STATUS :容器状态
PORTS :端口
NAMES :容器名称
2.查看停止的容器
docker ps -f status=exited
3.查看所有容器(包括运行和停止)。
docker ps -a
4.查看容器的ID
docker ps -q
5.查看最后一次运行的容器。
docker ps -l
6.列出最近创建的 n 个容器。
docker ps -n 5
7.查看docker内容器的运行状态
docker stats
五,创建与启动容器
格式:docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
选项: -i :表示运行容器; -t :表示容器启动后会进入其命令行。加入这两个参数后,容器创建就能登录进去。即分配一个伪 终端; --name :为创建的容器命名; -v :表示目录映射关系(前者是宿主机目录,后者是映射到容器上的目录),可以使用多个 -v 做多个目录或文件映射。注意:最好做目录映射,在宿主机上做修改,然后共享到容器上; -d :在 run 后面加上 -d 参数,则会创建一个守护式容器在后台运行(这样创建容器后不会自动登 录容器,如果只加 -i -t 两个参数,创建容器后就会自动进容器里); -p :表示端口映射,前者是宿主机端口,后者是容器内的映射端口。可以使用多个 -p 做多个端口 映射。 -P :随机使用宿主机的可用端口与容器内暴露的端口映射。
1.创建并进入容器
下面这行命令的意思就是通过镜像 AA 创建一个容器 BB,运行容器并进入容器的 /bin/bash 。
格式:docker run -it --name 容器名称 镜像名称:标签 /bin/bash
[root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE mysql 5.7 eef0fab001e8 3 weeks ago 495MB centos 7 eeb6ee3f44bd 14 months ago 204MB centos 8 5d0da3dc9764 14 months ago 231MB [root@localhost ~]# docker run -it --name centos7 centos:7 /bin/bash
注意:Docker 容器运行必须有一个前台进程, 如果没有前台进程执行,容器认为是空闲状态,就 会自动退出。
退出当前容器:exit
2.守护式方式创建容器
格式:docker run -di --name 容器名称 镜像名称:标签或镜像ID号
[root@localhost ~]# docker run -id --name centos7-3 eeb6ee3f44bd /bin/bash f2e90a5827ee64ad150ab95d3fe74a91dc3d355424243ae4e33a72f898353c69
3.登录守护式容器方式
格式:docker exec -it 容器名称|容器ID /bin/bash
[root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f2e90a5827ee eeb6ee3f44bd "/bin/bash" About a minute ago Up About a minute centos7-3 [root@localhost ~]# docker exec -it centos7-3 /bin/bash [root@f2e90a5827ee /]#
六,停止与启动容器
1.停止容器
格式:docker stop 容器名称|容器ID
2.启动容器
格式:docker start 容器名称|容器ID
3.重启容器
格式:docker restart 容器名称|容器ID
4.停止容器
格式:docker kill 容器名称|容器ID
七,删除容器
1.删除指定容器
格式:docker rm 容器名称|容器ID
2.删除多个容器
格式:docker rm 容器名称|容器ID 容器名称|容器ID
八,文件拷贝
如果我们需要将文件拷贝到容器内可以使用 cp 命令。
格式:docker cp 需要拷贝的文件或目录 容器名称:容器目录
[root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f2e90a5827ee eeb6ee3f44bd "/bin/bash" 5 minutes ago Up 5 minutes centos7-3 [root@localhost ~]# docker cp /root/1.sql centos7-3:/root [root@localhost ~]# docker exec -it centos7-3 /bin/bash [root@f2e90a5827ee /]# ls /root/ 1.sql anaconda-ks.cfg
也可以将文件从容器内拷贝出来(命令在宿主机中使用)。
格式:docker cp 容器名称:容器目录 需要拷贝的文件或目录
[root@f2e90a5827ee /]# touch /root/test.txt [root@f2e90a5827ee /]# exit exit [root@localhost ~]# docker cp centos7-3:/root/test.txt /tmp/ [root@localhost ~]# ls /tmp/ test.txt
九,目录挂载(容器数据卷操作)
创建容器添加 -v 参数,格式为 宿主机目录:容器目录
例如:
[root@localhost ~]# ls 1.sql all-mysql.sql file1 ip_yes.txt nginx school2022-11-15 userinfo.txt 1.txt anaconda-ks.cfg fornum.sh mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz num.txt sh.bzip2.tar UserSystem.sh 2022-10-21all.sql cut_nginx_log.sh info.txt mysqldata original-ks.cfg test a1-mysql.sql etc.tar.zip install.cfg mysqldata2022-08-15.sql passwd test.txt [root@localhost ~]# docker run -it -v /root/:/root/ --name centos7-4 centos:7 [root@5edc752038e0 /]# ls /root 1.sql all-mysql.sql fornum.sh mysqldata passwd userinfo.txt 1.txt anaconda-ks.cfg info.txt mysqldata2022-08-15.sql school2022-11-15 2022-10-21all.sql cut_nginx_log.sh install.cfg nginx sh.bzip2.tar UserSystem.sh etc.tar.zip ip_yes.txt num.txt test a1-mysql.sql file1 mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz original-ks.cfg test.txt
# 多目录挂载
docker run -di -v /宿主机目录:/容器目录 -v /宿主机目录2:/容器目录2 镜像名
1.匿名挂载
匿名挂载只需要写容器目录即可,容器外对应的目录会在 /var/lib/docker/volumes 中生成。
# 匿名挂载
[root@localhost ~]# docker run -di -v /root/ --name centos7-5 centos:7 98b42386e4181ba46a85173740e97c47111e755c19f6716dcd72561e5d24cd3d
# 查看 volume 数据卷信息
[root@localhost ~]# docker volume ls DRIVER VOLUME NAME local 7c342abc52d25a5f2e221fb58e01303dcb08761543e5e9c251967263c6c60d58 local 20777204e22f6797a4a333525769460941d0e76c78020cadafe2bb88a2db6447 local cb89c1bfd7e7b237cdd9256e773d387d45b70a1a7dc74ee12708790aa0f2f200 local docker_centos_data
2.具名挂载
具名挂载就是给数据卷起了个名字,容器外对应的目录会在 /var/lib/docker/volume 中生成。
# 具名挂载
[root@localhost ~]# docker run -di -v centos7-6-root:/root/ --name centos-6 centos:7 47411c26febf3237a56312e68046fb66e411d17bd9a42d8b90c85fe18b3577c2
# 查看 volume 数据卷信息
[root@localhost ~]# docker volume ls DRIVER VOLUME NAME local 7c342abc52d25a5f2e221fb58e01303dcb08761543e5e9c251967263c6c60d58 local 20777204e22f6797a4a333525769460941d0e76c78020cadafe2bb88a2db6447 local cb89c1bfd7e7b237cdd9256e773d387d45b70a1a7dc74ee12708790aa0f2f200 local centos7-6-root local docker_centos_data
3.指定目录挂载
一开始给大家讲解的挂载方式就属于指定目录挂载,这种方式的挂载不会在 /var/lib/docker/volume 目录生成内容。
格式:docker run -di -v /宿主机目录:/容器目录 -v /宿主机目录2:/容器目录2 镜像名
4.查看目录挂载关系
通过 docker volume inspect 数据卷名称 可以查看该数据卷对应宿主机的目录地址。
[root@localhost ~]# docker volume inspect centos7-6-root [ { "CreatedAt": "2022-11-30T06:55:40Z", "Driver": "local", "Labels": null, "Mountpoint": "/var/lib/docker/volumes/centos7-6-root/_data", "Name": "centos7-6-root", "Options": null, "Scope": "local" } ]
通过 docker inspect 容器ID或名称 ,在返回的 JSON 节点中找到 Mounts ,可以查看详细的数据 挂载信息。
5.只读/读写挂载
# 只读。只能通过修改宿主机内容实现对容器的数据管理。
格式:docker run -it -v /宿主机目录:/容器目录:ro 镜像名
[root@localhost ~]# docker run -it -v /root/:/root:ro centos:7 [root@f3dad8bf6874 /]# ls /root 1.sql a1-mysql.sql etc.tar.zip install.cfg mysqldata2022-08-15.sql passwd test.txt 1.txt all-mysql.sql file1 ip_yes.txt nginx school2022-11-15 userinfo.txt 2022-10-21all.sql anaconda-ks.cfg fornum.sh mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz num.txt sh.bzip2.tar UserSystem.sh cut_nginx_log.sh info.txt mysqldata original-ks.cfg test [root@f3dad8bf6874 /]# echo 233 > /root/1.sql bash: /root/1.sql: Read-only file system
# 读写,默认。宿主机和容器可以双向操作数据。
格式:docker run -it -v /宿主机目录:/容器目录:rw 镜像名
6.volumes-from(继承)
# 容器 centos7-01 指定目录挂载
docker run -di -v /mydata/docker_centos/data:/usr/local/data --name centos7-01 centos:7
# 容器 centos7-04 和 centos7-05 相当于继承 centos7-01 容器的挂载目录
docker run -di --volumes-from centos7-01:ro --name centos7-04 centos:7 docker run -di --volumes-from centos7-01:rw --name centos7-05 centos:7
十,查看容器 IP 地址
我们可以通过以下命令查看容器的元信息。
格式:docker inspect 容器名称|容器ID
[root@localhost ~]# docker inspect centos7-2 [ { "Id": "cab1b43029bb4e68058971d06e62e67fcc3d3df49cdd9a2c285ca332bbb5d003", "Created": "2022-11-30T06:29:54.080391503Z", "Path": "/bin/bash", "Args": [], "State": { "Status": "exited", "Running": false, "Paused": false, "Restarting": false, "OOMKilled": false, "Dead": false, "Pid": 0, "ExitCode": 127, "Error": "", "StartedAt": "2022-11-30T06:29:54.237043945Z", "FinishedAt": "2022-11-30T06:30:06.600353835Z" }, "Image": "sha256:eeb6ee3f44bd0b5103bb561b4c16bcb82328cfe5809ab675bb17ab3a16c517c9", "ResolvConfPath": "/var/lib/docker/containers/cab1b43029bb4e68058971d06e62e67fcc3d3df49cdd9a2c285ca332bbb5d003/resolv.conf", "HostnamePath": "/var/lib/docker/containers/cab1b43029bb4e68058971d06e62e67fcc3d3df49cdd9a2c285ca332bbb5d003/hostname", "HostsPath": "/var/lib/docker/containers/cab1b43029bb4e68058971d06e62e67fcc3d3df49cdd9a2c285ca332bbb5d003/hosts", "LogPath": "/var/lib/docker/containers/cab1b43029bb4e68058971d06e62e67fcc3d3df49cdd9a2c285ca332bbb5d003/cab1b43029bb4e68058971d06e62e67fcc3d3df49cdd9a2c285ca332bbb5d003-json.log", "Name": "/centos7-2", "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/63a8e3e1f155f38a51bebce3b9a233b2bff1ba4630e5993276a0d3cce0dbc61f-init/diff:/var/lib/docker/overlay2/9b62e8d0a4882ca9b207fae4a54e220a00f5556d52f0fa8ad933e3863763b43b/diff", "MergedDir": "/var/lib/docker/overlay2/63a8e3e1f155f38a51bebce3b9a233b2bff1ba4630e5993276a0d3cce0dbc61f/merged", "UpperDir": "/var/lib/docker/overlay2/63a8e3e1f155f38a51bebce3b9a233b2bff1ba4630e5993276a0d3cce0dbc61f/diff", "WorkDir": "/var/lib/docker/overlay2/63a8e3e1f155f38a51bebce3b9a233b2bff1ba4630e5993276a0d3cce0dbc61f/work" }, "Name": "overlay2" }, "Mounts": [], "Config": { "Hostname": "cab1b43029bb", "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": "eeb6ee3f44bd", "Volumes": null, "WorkingDir": "", "Entrypoint": null, "OnBuild": null, "Labels": { "org.label-schema.build-date": "20201113", "org.label-schema.license": "GPLv2", "org.label-schema.name": "CentOS Base Image", "org.label-schema.schema-version": "1.0", "org.label-schema.vendor": "CentOS", "org.opencontainers.image.created": "2020-11-13 00:00:00+00:00", "org.opencontainers.image.licenses": "GPL-2.0-only", "org.opencontainers.image.title": "CentOS Base Image", "org.opencontainers.image.vendor": "CentOS" } }, "NetworkSettings": { "Bridge": "", "SandboxID": "4faf35abf5f60d628fc545b1472f0b86dcad768bdc869406a7a2e9288be77df2", "HairpinMode": false, "LinkLocalIPv6Address": "", "LinkLocalIPv6PrefixLen": 0, "Ports": {}, "SandboxKey": "/var/run/docker/netns/4faf35abf5f6", "SecondaryIPAddresses": null, "SecondaryIPv6Addresses": null, "EndpointID": "", "Gateway": "", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "IPAddress": "", "IPPrefixLen": 0, "IPv6Gateway": "", "MacAddress": "", "Networks": { "bridge": { "IPAMConfig": null, "Links": null, "Aliases": null, "NetworkID": "c9406d1cb33e3be61d308ad252c3114db3f70de5adb27fb750679f42800c0b31", "EndpointID": "", "Gateway": "", "IPAddress": "", "IPPrefixLen": 0, "IPv6Gateway": "", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "MacAddress": "", "DriverOpts": null } } } } ]
也可以直接执行下面的命令直接输出 IP 地址。
格式:docker inspect --format='{{.NetworkSettings.IPAddress}}' 容器名称|容器ID
[root@localhost ~]# docker inspect --format='{{.NetworkSettings.IPAddress}}' centos7-3 172.17.0.2
感谢大家的支持,如果喜欢可以给博主点一个关注,后续还会更新Docker方面的知识拓展