author:yongjie.tie
uname -r
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-uKgHV2UY-1651717151345)(1624065864157.png)]
cat /etc/os-release
系统版本查看
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-z0hOxZKR-1651717151347)(assets\1624065917300.png)]
yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
sudo yum install -y yum-utils
$ sudo yum-config-manager \
--add-repo \
yum-config-manager --add-repo= http://mirrors.aliyun.com/repo/Centos-7.repo
安装最新版本docker
yum install docker-ce docker-ce-cli containerd.io
systemctl start docker
4.查看docker版本
systemctl start docker
5.运行
docker run hello-world
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-3My8BnZj-1651717151347)(C:\Users\tyj\AppData\Roaming\Typora\typora-user-images\1624067240245.png)]
没有镜像会pull拉取镜像,通过docker images查看拉取的镜像
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-eSYfXJTT-1651717151348)(C:\Users\tyj\AppData\Roaming\Typora\typora-user-images\1624067393962.png)]
yum remove docker-ce docker-ce-cli containerd.io
sudo rm -rf /var/lib/docker
$ sudo rm -rf /var/lib/containerd
5.配置阿里云镜像加速器
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-‘EOF’
{
“registry-mirrors”: [“https://elzciopd.mirror.aliyuncs.com”]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-TveMhpwT-1651717151348)(C:\Users\tyj\AppData\Roaming\Typora\typora-user-images\1624069907959.png)]
docker是一个client-server结构的系统,docker的守护进程运行在主机上,通过Socket从客户端访问,DockerServer接收到Client这个指令就会执行
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-0K0RAW1e-1651717151348)(C:\Users\tyj\AppData\Roaming\Typora\typora-user-images\1624070276118.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-XKTPOaSx-1651717151348)(C:\Users\tyj\AppData\Roaming\Typora\typora-user-images\1624070534915.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-dPyaiZTi-1651717151349)(C:\Users\tyj\AppData\Roaming\Typora\typora-user-images\1624070550833.png)]
新建一个容器的时候,docker不需要像虚拟机一样重新加载一个计算机内核,避免引导.虚拟机需要加载Guest Os,分钟级别的,而docker是利用了宿主机的内核,省略这个复杂的过程,是秒级别的
docker info docker系统信息
docker version docker版本信息
docker images 查看本地主机所有镜像
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-VKmtwW5V-1651717151349)(C:\Users\tyj\AppData\Roaming\Typora\typora-user-images\1624071078464.png)]
使用docker search来搜索mysql信息
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-8PS0VQIS-1651717151350)(C:\Users\tyj\AppData\Roaming\Typora\typora-user-images\1624087556261.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-LpFo4kcl-1651717151350)(C:\Users\tyj\AppData\Roaming\Typora\typora-user-images\1624087845756.png)]
# 下载镜像 docker pull 镜像名[:tag]
# docker pull mysql:5.7
[root@iZwz9hgjk99oratwybdnv8Z ~]# docker pull mysql
Using default tag: latest
latest: Pulling from library/mysql
69692152171a: Pull complete #分层下载
1651b0be3df3: Pull complete
951da7386bc8: Pull complete
0f86c95aa242: Pull complete
37ba2d8bd4fe: Pull complete
6d278bb05e94: Pull complete
497efbd93a3e: Pull complete
f7fddf10c2c2: Pull complete
16415d159dfb: Pull complete
0e530ffc6b73: Pull complete
b0a4a1a77178: Pull complete
cd90f92aa9ef: Pull complete
Digest: sha256:d50098d7fcb25b1fcb24e2d3247cae3fc55815d64fec640dc395840f8fa80969
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest
下载指定版本,如果之前有的公用部分则不会下载
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-HYzVeAEu-1651717151350)(C:\Users\tyj\AppData\Roaming\Typora\typora-user-images\1624088611513.png)]
docker rmi -f 镜像id
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-sNoDBFF8-1651717151351)(C:\Users\tyj\AppData\Roaming\Typora\typora-user-images\1624089082070.png)]
有了镜像才能够创建容器,下载一个centos镜像来测试学习
docker pull centos
docker run ['可选参数'] image
# --name='Name' 容器名字 tomcat1,tomcat2,用来区分容器
# -d 后台的方式运行
# -it 使用交互方式运行,进入容器查看内容
# -p 指定容器端口 -p8080:8080
# -p ip:主机端口:容器端口(常用)
# 测试,启动容器并进入
[root@iZwz9hgjk99oratwybdnv8Z ~]# docker run -it centos /bin/bash
[root@8488a57c2c54 /]# ls
bin etc lib lost+found mnt proc run srv tmp var
dev home lib64 media opt root sbin sys usr
[root@8488a57c2c54 /]#
# docker ps 命令
# 列出当前正在运行的容器
-a # 列出正在运行的容器 + 带出历史运行过的容器
-n # 显出最近创建的容器
-q # 只显示容器的编号
[root@iZwz9hgjk99oratwybdnv8Z ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@iZwz9hgjk99oratwybdnv8Z ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9e7a9ee61ce3 centos "/bin/bash" 2 minutes ago Exited (127) 25 secon ds ago jolly_burnell
8488a57c2c54 centos "/bin/bash" 4 minutes ago Exited (0) 3 minutes ago goofy_sanderson
f46bdb8d4a57 hello-world "/hello" 6 hours ago Exited (0) 6 hours ag o gracious_elion
[root@iZwz9hgjk99oratwybdnv8Z ~]#
exit # 容器停止并退出
ctral +p +q #容器不停止退出
[root@8488a57c2c54 /]# ls
bin etc lib lost+found mnt proc run srv tmp var
dev home lib64 media opt root sbin sys usr
[root@8488a57c2c54 /]# exit
exit
[root@iZwz9hgjk99oratwybdnv8Z ~]# ls
[root@iZwz9hgjk99oratwybdnv8Z ~]# ls
docker rm 容器id #删除指定的容器,但是不能删除正在运行中的容器
docker rm -f$(docker ps aq) # 删除所有容器
启动和停止容器的操作
docker start 容器id # 启动容器
docker restart 容器id #重启容器
docker stop 容器id # 停止当前的正在运行的容器
docker kill 容器id #杀掉容器
docker exec -it 容器 bashshell # 后台方式进入正在运行的容器
后台启动应用
# docker run -d 镜像名
# 问题docker ps,发现centos停掉了
# docker常见的坑,docker 容器使用后台运行,就必须有一个前台的进程, docker发现没有应用,就会自动停止
# nginx,容器启动后,发现自己没有提供服务,就会立刻停止,就是没有程序了
查看日志
docker logs -f -t tail 10 容器id
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-FWK49Rxc-1651717151351)(C:\Users\tyj\AppData\Roaming\Typora\typora-user-images\1624092967088.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-nY8bHG9A-1651717151352)(C:\Users\tyj\AppData\Roaming\Typora\typora-user-images\1624092839348.png)]
查看容器中的进程
root@iZwz9hgjk99oratwybdnv8Z ~]# docker top 19c815c550cc
UID PID PPID C STIME TTY TIME CMD
root 14278 14258 0 16:21 pts/0 00:00:00 /bin/bash
查看镜像的元数据
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-nEzK0p2C-1651717151352)(C:\Users\tyj\AppData\Roaming\Typora\typora-user-images\1624093933081.png)]
[root@iZwz9hgjk99oratwybdnv8Z ~]# docker inspect 19c815c550cc
[
{
"Id": "19c815c550cce5988c3378d877a7071dd28fbf50b879639239286da42ebf1390"
"Created": "2021-06-19T08:21:58.995785096Z",
"Path": "/bin/bash",
"Args": [],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 14278,
"ExitCode": 0,
"Error": "",
"StartedAt": "2021-06-19T08:21:59.325972738Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:300e315adb2f96afe5f0b2780b87f28ae95231fe3bdd1e16b9ba606
"ResolvConfPath": "/var/lib/docker/containers/19c815c550cce5988c3378d87779639239286da42ebf1390/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/19c815c550cce5988c3378d877a7639239286da42ebf1390/hostname",
"HostsPath": "/var/lib/docker/containers/19c815c550cce5988c3378d877a7071239286da42ebf1390/hosts",
"LogPath": "/var/lib/docker/containers/19c815c550cce5988c3378d877a7071dd9286da42ebf1390/19c815c550cce5988c3378d877a7071dd28fbf50b879639239286da42ebf1390
"Name": "/inspiring_torvalds",
"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/9ae16c130737901203c073e98e0038ae3c797ade3a143371-init/diff:/var/lib/docker/overlay2/6ce1f6ff95d6fa17b2447c80a692c33f2061ded71185f2a4/diff",
"MergedDir": "/var/lib/docker/overlay2/9ae16c130737901203c073e98b0038ae3c797ade3a143371/merged",
"UpperDir": "/var/lib/docker/overlay2/9ae16c130737901203c073e98e0038ae3c797ade3a143371/diff",
"WorkDir": "/var/lib/docker/overlay2/9ae16c130737901203c073e98e3038ae3c797ade3a143371/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "19c815c550cc",
"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:/b
],
"Cmd": [
"/bin/bash"
],
"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": "79f8dbab1b17d92e43b2fd20014c5d862e58cf4ce68615f84775c5
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/79f8dbab1b17",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "4ad5d6efeff8a2922009ebd2abed4929dab94fa23b754214d7879
"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": "0be32eaacdcc89d2f70e744d566c2802db557ddce02a43c3",
"EndpointID": "4ad5d6efeff8a2922009ebd2abed4929dab94fa23b754b1c",
"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
}
}
}
}
]
#### 文件复制
将容器的文件复制到宿主机上
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-oQPw5cm4-1651717151352)(C:\Users\tyj\AppData\Roaming\Typora\typora-user-images\1624184045780.png)]
# 先进入对应的容器中
[root@iZwz9hgjk99oratwybdnv8Z ~]# docker exec -it 19c815c550cc /bin/bash
[root@19c815c550cc /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
[root@19c815c550cc /]# touch hello.py
[root@19c815c550cc /]# ls
bin dev etc hello.py home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
[root@19c815c550cc /]# exit
exit
[root@iZwz9hgjk99oratwybdnv8Z ~]# docker cp 19c815c550cc:/hello.py /home
[root@iZwz9hgjk99oratwybdnv8Z ~]# ls
chromedriver_win32.zip GerapyPyppeteer-master.zip
[root@iZwz9hgjk99oratwybdnv8Z ~]# cd /home
[root@iZwz9hgjk99oratwybdnv8Z home]# ls
hello.py
[root@iZwz9hgjk99oratwybdnv8Z home]#
如:复制my.sh文件到c3646071ecd2容器中
sudo docker cp my.sh c3646071ecd2:/opt/my.sh
在宿主机输入入下命令,gracious_lichterman为容器名称,需要注意的是无论容器是否启动,复制命令都会生效`
[root@VM-24-10-centos ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8b89ddca3911 d7849f44ae62 "/bin/bash" 10 hours ago Up 10 hours gracious_lichterman
99ac5ca5dc44 mysql "docker-entrypoint.s…" 38 hours ago Up 38 hours 0.0.0.0:3306->3306/tcp, 33060/tcp mysql
[root@VM-24-10-centos ~]# docker cp gracious_lichterman:/test.txt /root/test.txt
[root@VM-24-10-centos ~]# ls
test.txt
查看容器的系统版本
cat /etc/issue
(base) root@8b89ddca3911:~# cat /etc/issue
Debian GNU/Linux 11 \n \l
#已有的容器更新为自动重启
docker container update --restart=always 容器ID(或者容器名)
#创建容器的时候设置容器为自动重启 命令行中加参数 --restart=always
docker run -d --restart=always --name 设置容器名 使用的镜像
docker logs --tail=“100” 容器id
[root@iZwz98p9v97qf0rhb0i3tcZ ~]# docker logs --tail="100" d39f1453baef
2021-11-17T08:26:34+0000 [twisted.python.log#info] "82.156.167.128" - - [17/Nov/2021:08:26:33 +0000] "GET /logs/eepw/eepwredis/5fd9c526475a11ecbc0e0242ac110003.log HTTP/1.1" 206 1000 "-" "python-requests/2.24.0"
2021-11-17T08:26:37+0000 [twisted.python.log#info] "82.156.167.128" - - [17/Nov/2021:08:26:36 +0000] "GET /logs/eepw/eepwredis/5fd9c526475a11ecbc0e0242ac110003.log HTTP/1.1" 206 1000 "-" "python-requests/2.24.0"
2021-11-17T08:26:40+0000 [twisted.python.log#info] "82.156.167.128" - - [17/Nov/2021:08:26:39 +0000] "GET /logs/eepw/eepwredis/5fd9c526475a11ecbc0e0242ac110003.log HTTP/1.1" 206 1000 "-" "python-requests/2.24.0"
2021-11-17T08:26:43+0000 [twisted.python.log#info] "82.156.167.128" - - [17/Nov/2021:08:26:42 +0000] "GET /logs/eepw/eepwredis/5fd9c526475a11ecbc0e0242ac110003.log HTTP/1.1" 206 1000 "-" "python-requests/2.24.0"
2021-11-17T08:26:45+0000 [twisted.python.log#info] "82.156.167.128" - - [17/Nov/2021:08:26:44 +0000] "GET /listjobs.json?project=eepw HTTP/1.1" 200 364 "-" "python-requests/2.24.0"
2021-11-17T08:26:49+0000 [twisted.python.log#info] "82.156.167.128" - - [17/Nov/2021:08:26:48 +000
docker logs --f 容器id
1.当容器日志过大时可以使用
cd /www/server/docker/containers/60d94d5d0d2aeb8dc1af39ee09cf764d44d6cb9aa9557a2737a0183b8d402c24
cat /dev/null > *-json.log
清理日志,但是治标不治本
2.配置docker daemon
/etc/docker/daemon.json
配置文件如下,这里将每个容器可以使用的磁盘空间设置为1G:
{
"data-root": "/data/docker",
"storage-driver": "overlay2",
"storage-opts": [
"overlay2.override_kernel_check=true",
"overlay2.size=1G"
]
}
sudo systemctl restart docker
docker build -f Dockerfile -t scrapyd_pyppeteer:0.1 .
通过docker build -f 来打包镜像
6.docker安装mysql
# pwd
/opt
# mkdir -p docker_v/mysql/conf
# cd docker_v/mysql/conf
# touch my.cnf
# docker run -p 3306:3306 --name mysql -v /opt/docker_v/mysql/conf:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=123456 -d imageID
4ec4f56455ea2d6d7251a05b7f308e314051fdad2c26bf3d0f27a9b0c0a71414
命令说明:
**-p 3306:3306:**将容器的3306端口映射到主机的3306端口
**-v /opt/docker_v/mysql/conf:/etc/mysql/conf.d:**将主机/opt/docker_v/mysql/conf目录挂载到容器的/etc/mysql/conf.d
**-e MYSQL_ROOT_PASSWORD=123456:**初始化root用户的密码
-d: 后台运行容器,并返回容器ID
imageID: mysql镜像IDls
先使用 docker inspect 容器id 获取容器的元数据
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-AyRB8NDr-1651717151353)(docker%E5%AE%89%E8%A3%85%E4%BD%BF%E7%94%A8.assets/image-20220106134431482.png)]
切换到对应目录下面
[root@hkspider1 ~]# cd /var/lib/docker/overlay2/
[root@hkspider1 overlay2]# ls
查找对应的.pid文件
[root@hkspider1 diff]# find -name ‘*.pid’
删掉对应的.pid文件即可
[root@hkspider1 diff]# rm twistd.pid
重启启动对应的容器
docker start 30d5321bfe0f
使用docker ps查看,容器已经正常起来了
主机的3306端口
**-v /opt/docker_v/mysql/conf:/etc/mysql/conf.d:**将主机/opt/docker_v/mysql/conf目录挂载到容器的/etc/mysql/conf.d
**-e MYSQL_ROOT_PASSWORD=123456:**初始化root用户的密码
-d: 后台运行容器,并返回容器ID
imageID: mysql镜像IDls
先使用 docker inspect 容器id 获取容器的元数据
[外链图片转存中…(img-AyRB8NDr-1651717151353)]
切换到对应目录下面
[root@hkspider1 ~]# cd /var/lib/docker/overlay2/
[root@hkspider1 overlay2]# ls
查找对应的.pid文件
[root@hkspider1 diff]# find -name ‘*.pid’
删掉对应的.pid文件即可
[root@hkspider1 diff]# rm twistd.pid
重启启动对应的容器
docker start 30d5321bfe0f
使用docker ps查看,容器已经正常起来了