课程来源:bilibili 遇见狂神说
docker英文文档网站:https://docs.docker.com/engine/install/centos/
一、理论与基础学习
前言、基础知识
docker隔离的核心技术是什么?
Linux
中的namespace技术
介绍一下什么是namespace技术?
什么是守护进程?
“守护进程(daemon)是一类在后台运行的特殊进程,用于执行特定的系统任务。很多守护进程在系统引导的时候启动,并且一直运行直到系统关闭。另一些只在需要的时候才启动,完成任务后就自动结束。”
1、购买阿里云服务器
开通安全组
获取服务器的公网ip,修改实例名称
2、安装Docker-1
在本文中采用的系统为linux下的cent OS (7)
# 清楚一些不必要的内容,移除以前的docker版本
yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
# 安装需要的依赖包
yum install -y yum-utils
# 设置镜像的仓库
yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo
## 建议选择国内的docker镜像源进行操作,现在选择的是阿里云
yum-config-manager \
--add-repo \
http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
## 更新yum软件包索引
yum makecache fast
# 安装docker-ce、容器等内容
yum install docker-ce docker-ce-cli containerd.io
## 查看可以安装的docker版本
yum list docker-ce --showduplicates | sort -r
## 可以选择自己想要的docker版本
yum install docker-ce-<VERSION_STRING> docker-ce-cli-<VERSION_STRING> containerd.io
# 启动docker
systemctl start docker
# 运行hello-world查看是否完成,见下图1
docker run hello-world
# 查看安装的docker版本,测试是否安装成功
docker version
# 通过docker images指令查看,见下图2
docker images
2、安装Docker-2
关于卸载docker
# 首先先从应用层面卸载内容,然后再从文件删除(Linux中万物都是文件)
yum remove docker-ce docker-ce-cli containerd.io
# 删除对应的文件内容
rm -rf /var/lib/docker
rm -rf /var/lib/containerd
配置阿里云容器镜像服务
登录自己的阿里云账号,产品与服务中选择容器镜像服务然后选择镜像工具中的镜像加速器
马赛克处填写自己的镜像加速地址
# 新建docker文件加
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://3wp6pyr4.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
# 重启docker服务
sudo systemctl restart docker
3、了解Docker和Run
1、run images
2、查看本地是否有这个images
3、如果有,直接运行;如果否,去docker hub上下载,去4)
4、能否找到images,找到则执行2);未找到images,就报错
4、底层原理
Docker是怎么工作的?
Docker是一个C/S结构的系统,Docker的守护jin运行在主机上。通过Socker从客户端访问!
Docker_Server收到Docker_Client的指令,就回执行在这俄格
Docker为什么是虚拟机更快?
docker比虚拟机拥有更少的抽象层
docker利用的是宿主机的内核,虚拟机需要Guest OS
所以说,新建一个容器的是狗,docker不需要重新加载一个操作系统内核,减少引导。
虚拟机加载一个新的Guest Os,分钟级别的,docker使用的宿主机的操作内核,秒级别。
5、Docker的常用命令
帮助命令
docker version # 显示docker的基本信息
docker info # 显示docker的详细信息
docker 命令 --help# 获取命令
镜像命令
docker images 查看所有本地的主机上的镜像
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest d1165f221234 5 weeks ago 13.3kB
# 解释
REPOSITORY 镜像的仓库源
TAG 镜像的标签
IMAGE ID 镜像的ID
CREATED 镜像的创建时间
SIEZ 镜像的大小
# 可选项
-a, --all # 列出所有的镜像
-q, -quiet # 只显示id
docker search 在docker hub上搜索镜像
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 10736 [OK]
mariadb MariaDB Server is a high performing open sou… 4043 [OK]
docker pull 下载镜像
# docker pull mysql 镜像名[:tag]
Using default tag: latest # 不写TAG默认是最新的版本
latest: Pulling from library/mysql
f7ec5a41d630: Pull complete # 分层下载,docker image的核心
9444bb562699: Pull complete
6a4207b96940: Pull complete
181cefd361ce: Pull complete
8a2090759d8a: Pull complete
15f235e0d7ee: Pull complete
d870539cd9db: Pull complete
5726073179b6: Pull complete
eadfac8b2520: Pull complete
f5936a8c3f2b: Pull complete
cca8ee89e625: Pull complete
6c79df02586a: Pull complete
Digest: sha256:6e0014cdd88092545557dee5e9eb7e1a3c84c9a14ad2418d5f2231e930967a38 # 签名信息
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest # 真实地址
# docker pull .io/library/mysql:latest
# docekr pull mysql:5.7 # 下载指定版本的images
5.7: Pulling from library/mysql
f7ec5a41d630: Already exists # 由于Linux的联合文件系统,某一些文件是可以实现共享
9444bb562699: Already exists
6a4207b96940: Already exists
181cefd361ce: Already exists
8a2090759d8a: Already exists
15f235e0d7ee: Already exists
d870539cd9db: Already exists
7310c448ab4f: Pull complete
4a72aac2e800: Pull complete
b1ab932f17c4: Pull complete
1a985de740ee: Pull complete
Digest: sha256:e42a18d0bd0aa746a734a49cbbcc079ccdf6681c474a238d38e79dc0884e0ecc
Status: Downloaded newer image
删除镜像
# docker rmi IMAGE_ID
# rmi => remove image
docker rmi -f IMAGE_ID # 删除指定的镜像
docker rmi -f IMAGE_ID IMAGE_ID IMAGE_ID # 删除多个镜像
docker rmi -f $(docker iamge -aq) # 删除全部的镜像
容器命令
说明: 有了镜像才可以做容器,先下载也给centos镜像作为测试学习
docker pull centos
新建容器并启动
docker run [可选参数] image
# 参数命名
--name='Name' # 容器名字, 如mysql1,mysql2,用来区别不同的容器
-d # 后台方式运行
-i # 使用交互方式运行,进入容器检查看内容
-p # 指定容器的端口
-p ip:主机端口:容器端口
-p 主机端口:容器端口
-p 容器端口
容器端口
-P # 随机指定端口
# 启动并进入容器
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 450379344707 3 days ago 449MB
hello-world latest d1165f221234 5 weeks ago 13.3kB
centos latest 300e315adb2f 4 months ago 209MB
[root@iZ2zeazmd74x8h83zuv6fcZ ~]# docker run -it centos /bin/bash
[root@4f6fb2ca9c2b /]# ls
# 退出当前容器
exit
列出所有的运行的容器
# docker ps # 输出当前运行的容器
-a # 理出当前正在运行和历史中的容器
-n=? # 输出最近的?个新建的容器
-q # 只显示当前容器的编号
# docker ps -a
[root@iZ2zeazmd74x8h83zuv6fcZ /]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4f6fb2ca9c2b centos "/bin/bash" 4 minutes ago Exited (0) 2 minutes ago kind_neumann
50e726de9d74 hello-world "/hello" 3 hours ago Exited (0) 3 hours ago kind_noyce
fbfa57e6db5f hello-world "/hello" 7 hours ago Exited (0) 7 hours ago nice_fermat
退出容器
exit # 容器停止并退出
Ctrl + p + q # 容器不停止退出
删除容器
docker rm # 容器id,不能删除正在运行的容器
docker rm -f $(docker ps -aq) # 删除所有容器
docker ps -a -q|xargs docker rm # 删除所有容器,利用linux中的管道完成
启动和停止容器
docker start 容器id # 启动容器
docker restart 容器id # 重启容器
docker stop 容器id # 暂停容器
docker kill 容器id # 去掉容器
常用其他命令
后台启动容器
# 命令docker run -d 镜像名
docker run -d centos
问题docker ps:发现centos停止
# 常见的坑,docker容器使用后台运行,就必须要有一个前台进程,docker发现没有应用就会自动停止
查看日志
dockerlogs -t -f --tail 容器
# 自己编写一段脚本
docker run -d centos /bin/sh -c 'while true;do echo bandhus chen;sleep 1; done'
# 查看进行,可以看见容器centos在运行,并且有对应的容器id
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
644fad8d9ff5 centos "/bin/sh -c 'while t…" 10 seconds ago Up 9 seconds intelligent_brahmagupta
# 由于上面写的脚本是循环脚本
# 读取文件日志
docker -tf --tail 10 644fad8d9ff5
-tf # 显示日志
--tail number # 要显示的日志条数
查看容器中的进程信息
# 命令docker top 容器id
[root@iZ2zeazmd74x8h83zuv6fcZ ~]# docker top 644fad8d9ff5
UID PID PPID C STIME TTY
root 8472 8451 0 18:59 ?
root 9215 8472 0 19:10 ?
# docker
[root@iZ2zeazmd74x8h83zuv6fcZ ~]# docker inspect 644fad8d9ff5
[
{
"Id": "644fad8d9ff501337abc078f4649bad5da9a92ec7c4d03c2ad5176b4a5bff41e",
"Created": "2021-04-13T10:59:49.295975424Z",
"Path": "/bin/sh",
"Args": [
"-c",
"while true;do echo bandhus chen;sleep 1;done"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 8472,
"ExitCode": 0,
"Error": "",
"StartedAt": "2021-04-13T10:59:49.69763237Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:300e315adb2f96afe5f0b2780b87f28ae95231fe3bdd1e16b9ba606307728f55",
"ResolvConfPath": "/var/lib/docker/containers/644fad8d9ff501337abc078f4649bad5da9a92ec7c4d03c2ad5176b4a5bff41e/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/644fad8d9ff501337abc078f4649bad5da9a92ec7c4d03c2ad5176b4a5bff41e/hostname",
"HostsPath": "/var/lib/docker/containers/644fad8d9ff501337abc078f4649bad5da9a92ec7c4d03c2ad5176b4a5bff41e/hosts",
"LogPath": "/var/lib/docker/containers/644fad8d9ff501337abc078f4649bad5da9a92ec7c4d03c2ad5176b4a5bff41e/644fad8d9ff501337abc078f4649bad5da9a92ec7c4d03c2ad5176b4a5bff41e-json.log",
"Name": "/intelligent_brahmagupta",
"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/7c18b5d372631c762562c842cafb8f7ac4c8e1f647ac71256aea400bf8d906a5-init/diff:/var/lib/docker/overlay2/468e11f2a421f1b89c42f3401ed21a18f824f477ac1f46aaa7731a6d25ef9608/diff",
"MergedDir": "/var/lib/docker/overlay2/7c18b5d372631c762562c842cafb8f7ac4c8e1f647ac71256aea400bf8d906a5/merged",
"UpperDir": "/var/lib/docker/overlay2/7c18b5d372631c762562c842cafb8f7ac4c8e1f647ac71256aea400bf8d906a5/diff",
"WorkDir": "/var/lib/docker/overlay2/7c18b5d372631c762562c842cafb8f7ac4c8e1f647ac71256aea400bf8d906a5/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "644fad8d9ff5",
"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 bandhus chen;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": "68c6ee90cb7fce04e482c37254ce14bbfb6c2d3900bdcf350babfe359db8fe43",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/68c6ee90cb7f",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "ea72ab495d9d7745a391266709fdf4fe871c8ca87492b7840415c89fad33f691",
"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": "7f0d0d0c10910c494b7470326dbd04d4f1c418e18a2aad77fcf3486f5876a290",
"EndpointID": "ea72ab495d9d7745a391266709fdf4fe871c8ca87492b7840415c89fad33f691",
"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
}
}
}
}
]
# 可以发现进程号和id之间有关联性
进入当前正在运行的容器
# 进入容器修改一些内容
# 命令一
docker exec -it 644fad8d9ff5 /bin/bash
[root@644fad8d9ff5 /]# ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 10:59 ? 00:00:00 /bin/sh -c while true;do echo ban
root 1147 0 0 11:18 pts/0 00:00:00 /bin/bash
root 1232 1 0 11:20 ? 00:00:00 /usr/bin/coreutils --coreutils-pr
root 1233 1147 0 11:20 pts/0 00:00:00 ps -ef
# 命令二
docker attach 644fad8d9ff5 # 直接进入程序
# 正在执行代码
exec # 进入容器后开启新的终端
attach # 进入容器正在执行的终端
从容器内复制内容到宿主机上
[root@iZ2zeazmd74x8h83zuv6fcZ ~]# docker run -it centos /bin/bash
# 开启一个新的centos服务
# 从docker内部复制文件,哪怕docker不再运行也是可以
docker cp dockerid:/home/bin/xxx /home
# 将dockerid的容器中 home文件下的bin中内容复制到宿主机的home文件下
6、小结
可视化命令大全
可视化操作:
portainer是docker的图形化界面管理工具
docker run -d -p 8088:9000 --restart=always -v /var/run/docker.sock:/var/run/docker.sock --privileged=true portainer/portainer
(记得要在阿里云的安全组开通8088权限,也可以尝试在内网访问端口)
curl localhost:8088