Docker 环境初始化安装
Posted 青衫解衣
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Docker 环境初始化安装相关的知识,希望对你有一定的参考价值。
安装docker
参考官网:https://docs.docker.com/engine/install/centos/
1.移除本机的docker(防止有老的docker):
sudo yum remove docker \\
docker-client \\
docker-client-latest \\
docker-common \\
docker-latest \\
docker-latest-logrotate \\
docker-logrotate \\
docker-engine
2.安装yum-utils,添加docker yum源
sudo yum install -y yum-utils
sudo yum-config-manager \\
--add-repo \\
https://download.docker.com/linux/centos/docker-ce.repo
3.安装docker
sudo yum install docker-ce docker-ce-cli containerd.io
配置docker 镜像加速器
个人注册个阿里云的账号,进入容器服务。
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-EOF
"registry-mirrors": ["https://4z7jtuuf.mirror.aliyuncs.com"]
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
修改docker 的存储根目录
docker 根的默认路径是/var/lib/docker
参数:"data-root": "/data",
[root@harbor ~]# cat /etc/docker/daemon.json
"registry-mirrors": ["https://v16stybc.mirror.aliyuncs.com"],
"insecure-registries": ["https://testharbor.sit.51zhaoyou.com"],
"exec-opts": ["native.cgroupdriver=systemd"],
"data-root": "/data",
"log-driver": "json-file",
"log-opts":
"max-size":"100m"
自定义安全dockcer仓库
简单解释就是改为自己或者公司的私有仓库,然后不需要认证登录下载。
官网链接:https://docs.docker.com/registry/insecure/
参数:"insecure-registries": ["https://testharbor.sit.51zhaoyou.com"],不定义默认获取https://hub.docker.com/
设置docker守护进程参数
live-restore重载docker守护进程而不重启容器。需要结合--restart=always,否则重启docker容器就down了。
"live-restore": true
[root@harbor <sub>]# docker run -itd --restart=always --name nginx nginx:latest
a897ce1045a2085de0f8fea36315287571c4bb824b5f624ff0d8fa3db247e891
[root@harbor </sub>]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a897ce1045a2 nginx:latest "/docker-entrypoint.…" 3 seconds ago Up 2 seconds 80/tcp nginx
[root@harbor <sub>]# vim /etc/docker/daemon.json
[root@harbor </sub>]# systemctl restart docker
[root@harbor ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a897ce1045a2 nginx:latest "/docker-entrypoint.…" 24 seconds ago Up 22 seconds 80/tcp nginx
修改docker dns配置
参数:"dns": ["114.114.114.114","8.8.8.8"],只对配置后的新容器生效,之前的容器不生效。
配置前
[root@harbor <sub>]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a897ce1045a2 nginx:latest "/docker-entrypoint.…" 5 minutes ago Up 5 minutes 80/tcp nginx
[root@harbor </sub>]# docker exec -it nginx cat /etc/resolv.conf
# Generated by NetworkManager
nameserver 192.168.1.1
nameserver 192.168.0.1
配置后
[root@harbor <sub>]# cat /etc/docker/daemon.json
"registry-mirrors": ["https://v16stybc.mirror.aliyuncs.com"],
"insecure-registries": ["https://testharbor.sit.51zhaoyou.com"],
"exec-opts": ["native.cgroupdriver=systemd"],
"dns": ["114.114.114.114","8.8.8.8"],
"data-root": "/data",
"log-driver": "json-file",
"live-restore": true,
"log-opts":
"max-size":"100m"
[root@harbor </sub>]# systemctl restart docker
[root@harbor ~]# docker exec -it nginx cat /etc/resolv.conf
# Generated by NetworkManager
nameserver 192.168.1.1
nameserver 192.168.0.1
新增容器启动查看dns解析
[root@harbor <sub>]# docker run -itd --restart=always --name redis redis:latest
Unable to find image redis:latest locally
latest: Pulling from library/redis
a2abf6c4d29d: Already exists
c7a4e4382001: Pull complete
4044b9ba67c9: Pull complete
c8388a79482f: Pull complete
413c8bb60be2: Pull complete
1abfd3011519: Pull complete
Digest: sha256:db485f2e245b5b3329fdc7eff4eb00f913e09d8feb9ca720788059fdc2ed8339
Status: Downloaded newer image for redis:latest
e01e4204dd570e055fdce1a435714a1a407ac1f374d79fc35c49a57864a82c75
[root@harbor </sub>]# docker exec -it redis cat /etc/resolv.conf
nameserver 114.114.114.114
nameserver 8.8.8.8
以上是关于Docker 环境初始化安装的主要内容,如果未能解决你的问题,请参考以下文章
商城项目02_环境搭建安装vagrantdockermysqlredis从0搭建各个微服务项目数据库初始化
商城项目02_环境搭建安装vagrantdockermysqlredis从0搭建各个微服务项目数据库初始化
商城项目02_环境搭建安装VAGRANTDOCKERMYSQLREDIS从0搭建各个微服务项目数据库初始化安装NGINX