声明:参考B站视频,自学成长记录
https://www.bilibili.com/video/BV1og4y1q7M4?from=search&seid=12319898488763080460
docker简介
Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的镜像中,然后发布到任何流行的 Linux或Windows 机器上,也可以实现虚拟化
容器是完全使用沙箱机制,相互之间不会有任何接口
docker结构
安装docker
参考官方文档:https://docs.docker.com/engine/install/centos/
卸载docker
[root@localhost ~]# yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
需要的安装包
[root@localhost ~]# yum install -y yum-utils
......
更新完毕:
yum-utils.noarch 0:1.1.31-54.el7_8
完毕!
设置镜像的仓库(推荐阿里云)
[root@localhost ~]# yum-config-manager \
--add-repo \
https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
更新yum软件包索引
[root@localhost ~]# yum makecache fast
安装社区版docker ce:社区版 / ee:企业版本
[root@localhost ~]# yum install docker-ce
启动docker / 加入开机启动项
[root@localhost ~]# systemctl start docker
[root@localhost ~]# systemctl enable docker
查看docker 版本
[root@localhost ~]# docker version
测试
[root@localhost ~]# docker run hello-world
配置镜像加速 --> 提高镜像下载速度
#1.创建一个目录
[root@localhost ~]# sudo mkdir -p /etc/docker
#2.编写配置文件
[root@localhost ~]# sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://t2wwyxhb.mirror.aliyuncs.com"]
}
EOF
{
"registry-mirrors": ["https://t2wwyxhb.mirror.aliyuncs.com"]
}
#3.重启服务
[root@localhost ~]# sudo systemctl daemon-reload
查看已经下载的镜像
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest bf756fb1ae65 7 months ago 13.3kB
列出所有镜像ID
[root@localhost ~]# docker images -qa
bf756fb1ae65
查看镜像的元数据
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 0d120b6ccaa8 6 days ago 215MB
[root@localhost ~]# docker inspect 0d120b6ccaa8
[
{
"Id": "sha256:0d120b6ccaa8c5e149176798b3501d4dd1885f961922497cd0abef155c869566",
"RepoTags": [
"centos:latest"
],
"RepoDigests": [
"centos@sha256:76d24f3ba3317fa945743bb3746fbaf3a0b752f10b10376960de01da70685fbd"
],
"Parent": "",
"Comment": "",
"Created": "2020-08-10T18:19:49.837885498Z",
"Container": "3b04ecd9fb2d3f921f12d858edf5f3a6aa7c36c8e1e6f74bd32555fd4d7f7da2",
"ContainerConfig": {
"Hostname": "3b04ecd9fb2d",
"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",
"#(nop) ",
"CMD [\"/bin/bash\"]"
],
"ArgsEscaped": true,
"Image": "sha256:69587a438b2c9b803db8ed4f6e6b5abce25a6b8ec2583a394807cf82bfd23693",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"org.label-schema.build-date": "20200809",
"org.label-schema.license": "GPLv2",
"org.label-schema.name": "CentOS Base Image",
"org.label-schema.schema-version": "1.0",
"org.label-schema.vendor": "CentOS"
}
},
"DockerVersion": "18.09.7",
"Author": "",
"Config": {
"Hostname": "",
"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"
],
"ArgsEscaped": true,
"Image": "sha256:69587a438b2c9b803db8ed4f6e6b5abce25a6b8ec2583a394807cf82bfd23693",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"org.label-schema.build-date": "20200809",
"org.label-schema.license": "GPLv2",
"org.label-schema.name": "CentOS Base Image",
"org.label-schema.schema-version": "1.0",
"org.label-schema.vendor": "CentOS"
}
},
"Architecture": "amd64",
"Os": "linux",
"Size": 215102299,
"VirtualSize": 215102299,
"GraphDriver": {
"Data": {
"MergedDir": "/var/lib/docker/overlay2/4b49de406d785061094f0ea9156599b5c00a38fe9df828a7d4f784f01f8bc36f/merged",
"UpperDir": "/var/lib/docker/overlay2/4b49de406d785061094f0ea9156599b5c00a38fe9df828a7d4f784f01f8bc36f/diff",
"WorkDir": "/var/lib/docker/overlay2/4b49de406d785061094f0ea9156599b5c00a38fe9df828a7d4f784f01f8bc36f/work"
},
"Name": "overlay2"
},
"RootFS": {
"Type": "layers",
"Layers": [
"sha256:291f6e44771a7b4399b0c6fb40ab4fe0331ddf76eda11080f052b003d96c7726"
]
},
"Metadata": {
"LastTagTime": "0001-01-01T00:00:00Z"
}
}
]
搭积木地基:Centos
docker中下载centos镜像
[root@localhost ~]# docker pull centos
查看下载的centos镜像
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 0d120b6ccaa8 6 days ago 215MB
通过centos镜像启动容器
[root@localhost ~]# docker run -it centos /bin/bash
[root@e2e837f35589 /]#
从容器中退出
exit # 退出容器,容器停止
Ctrl +Q + P # 退出容器,但是容器不会停止
进入到指定容器ID内
[root@localhost ~]# docker attach ff84530bcb86 #在正在执行的终端操作
[root@ff84530bcb86 /]#
[root@localhost ~]# docker exec -it ff84530bcb86 /bin/bash #开启新的终端执行操作
[root@ff84530bcb86 /]#
查看指定容器ID的进程信息
[root@localhost ~]# docker top ff84530bcb86
UID PID PPID C STIME TTY TIME CMD
root 12718 12699 0 19:12 pts/0 00:00:00 /bin/bash
查看指定容器ID的日志信息
[root@localhost ~]# docker logs -tf de1de0f343be # 时时刷新
[root@localhost ~]# docker logs --tail 5 de1de0f343be # 从末尾显示5行
查看容器元数据
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ff84530bcb86 centos "/bin/bash" 13 minutes ago Up 13 minutes stoic_margulis
[root@localhost ~]# docker inspect ff84530bcb86
[
{
"Id": "ff84530bcb86612f60bf7c3b49e51b8cc7abb44be129f6eae54f53e72824ae88",
"Created": "2020-08-17T11:12:36.189793565Z",
"Path": "/bin/bash",
"Args": [],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 12718,
"ExitCode": 0,
"Error": "",
"StartedAt": "2020-08-17T11:12:36.471575797Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:0d120b6ccaa8c5e149176798b3501d4dd1885f961922497cd0abef155c869566",
"ResolvConfPath": "/var/lib/docker/containers/ff84530bcb86612f60bf7c3b49e51b8cc7abb44be129f6eae54f53e72824ae88/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/ff84530bcb86612f60bf7c3b49e51b8cc7abb44be129f6eae54f53e72824ae88/hostname",
"HostsPath": "/var/lib/docker/containers/ff84530bcb86612f60bf7c3b49e51b8cc7abb44be129f6eae54f53e72824ae88/hosts",
"LogPath": "/var/lib/docker/containers/ff84530bcb86612f60bf7c3b49e51b8cc7abb44be129f6eae54f53e72824ae88/ff84530bcb86612f60bf7c3b49e51b8cc7abb44be129f6eae54f53e72824ae88-json.log",
"Name": "/stoic_margulis",
"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,
"Capabilities": null,
"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/e34dc9cb99966af591a0124861f2b26ede6fe662b7c3124befd9b4050e68c082-init/diff:/var/lib/docker/overlay2/4b49de406d785061094f0ea9156599b5c00a38fe9df828a7d4f784f01f8bc36f/diff",
"MergedDir": "/var/lib/docker/overlay2/e34dc9cb99966af591a0124861f2b26ede6fe662b7c3124befd9b4050e68c082/merged",
"UpperDir": "/var/lib/docker/overlay2/e34dc9cb99966af591a0124861f2b26ede6fe662b7c3124befd9b4050e68c082/diff",
"WorkDir": "/var/lib/docker/overlay2/e34dc9cb99966af591a0124861f2b26ede6fe662b7c3124befd9b4050e68c082/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "ff84530bcb86",
"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": "centos",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"org.label-schema.build-date": "20200809",
"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": "fb984c18baa0d99ec40187c3b58645bb7db41d1133dbd50a876e458398486a23",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/fb984c18baa0",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "13e12357222e8822416f35153117a04ea02b64088fccec8e0cbf44804a589e48",
"Gateway": "172.17.0.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "172.17.0.5",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"MacAddress": "02:42:ac:11:00:05",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "3bed570d9a537511ac6c5c78cebc5d90fbcf99586d40248abc8088c671b94a05",
"EndpointID": "13e12357222e8822416f35153117a04ea02b64088fccec8e0cbf44804a589e48",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.5",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:05",
"DriverOpts": null
}
}
}
}
]
继续搭积木:Centos + Nginx
检索nginx镜像
[root@localhost ~]# docker search nginx
下载nginx镜像
[root@localhost ~]# docker pull nginx
通过nginx镜像启动运行容器
# -d 后台运行
# --name 给容器命名
# -p 宿主机端口:容器内部端口
[root@localhost ~]# docker run -d --name nginx8081 -p 8081:80 nginx
de1de0f343be19bcdeceebf5eb6e0c682b2aedd35f162ec0f264d5b746672c20
命令模式下测试nginx是否启动
[root@localhost ~]# curl localhost:8081
返回如下,表示成功
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
网页访问测试nginx是否启动
容器外向容器内复制文件
[root@localhost docker]# docker cp /etc/docker/nginx.conf 71aeed1dda0e:/etc/nginx/nginx.conf
启动/停止/重启容器
# 根据容器ID 启动容器
[root@localhost ~]# docker start de1de0f343be
de1de0f343be
# 根据容器ID 停止容器
[root@localhost ~]# docker stop de1de0f343be
de1de0f343be
# 根据容器ID 重启容器
[root@localhost ~]# docker restart de1de0f343be
de1de0f343be
逆向操作
# 停止docker
[root@localhost ~]# systemctl stop docker
# 删除镜像
[root@localhost ~]# docker rmi -f bf756fb1ae65 # 根据镜像ID删除
[root@localhost ~]# docker rmi -f $(docker images -qa) # 删除所有镜像
[root@localhost ~]# docker image inspect --format='{{.RepoTags}} {{.Id}} {{.Parent}}' $(docker image ls -q --filter since=1b4a17450e32) # 查看镜像依赖
# 通过容器ID删除容器
[root@localhost ~]# docker rm 2b04ef5b73d5 # 不能删除正在运行的容器
[root@localhost ~]# docker rm -f 2b04ef5b73d5 # 强制删掉
[root@localhost ~]# docker rm -f $(docker ps -qa) # 强制删除所有容器
# 1.1 卸载docker依赖
[root@localhost ~]# yum remove docker-ce docker-ce-cli containerd.io
# 1.2删除docker资源
[root@localhost ~]# rm -rf /var/lib/docker # docker的默认工作路径!