2.安装docker容器并配置镜像加速
1 初始化前置环境
1.1 环境:
虚拟机:VMware 16
操作系统:CentOS7.9
docker https://www.docker.com/
仓库docker hub https://hub.docker.com/
仓库harbor :https://goharbor.io/
1.2 修改操作系统IP 从动态IP 改成 静态IP
修改网络配置文件
/etc/sysconfig/network-scripts/ifcfg-ens33
root@localhost home]# cd /etc/sysconfig/network-scripts
[root@localhost network-scripts]# ls
ifcfg-ens33 ifdown-isdn ifup ifup-plip ifup-tunnel
ifcfg-lo ifdown-post ifup-aliases ifup-plusb ifup-wireless
ifdown ifdown-ppp ifup-bnep ifup-post init.ipv6-global
ifdown-bnep ifdown-routes ifup-eth ifup-ppp network-functions
ifdown-eth ifdown-sit ifup-ib ifup-routes network-functions-ipv6
ifdown-ib ifdown-Team ifup-ippp ifup-sit
ifdown-ippp ifdown-TeamPort ifup-ipv6 ifup-Team
ifdown-ipv6 ifdown-tunnel ifup-isdn ifup-TeamPort
[root@localhost network-scripts]# vi ifcfg-ens33
原始文件:
TYPE="Ethernet"
PROXY_METHOD="none"
BROWSER_ONLY="no"
BOOTPROTO="dhcp"
DEFROUTE="yes"
IPV4_FAILURE_FATAL="no"
IPV6INIT="yes"
IPV6_AUTOCONF="yes"
IPV6_DEFROUTE="yes"
IPV6_FAILURE_FATAL="no"
IPV6_ADDR_GEN_MODE="stable-privacy"
NAME="ens33"
UUID="1f275e53-bf1a-4c3b-b72d-115e366c0ecc"
DEVICE="ens33"
ONBOOT="yes"
修改后的文件:
TYPE="Ethernet"
PROXY_METHOD="none"
BROWSER_ONLY="no"
# 将动态IP改成静态IP防止安装docker配置后IP又发生改变
# BOOTPROTO="dhcp"
BOOTPROTO=static
IPADDR=192.168.2.225
NETMASK=255.255.255.0
GATEWAY=192.168.2.1
DNS1=192.168.2.1
DEFROUTE="yes"
IPV4_FAILURE_FATAL="no"
IPV6INIT="yes"
IPV6_AUTOCONF="yes"
IPV6_DEFROUTE="yes"
IPV6_FAILURE_FATAL="no"
IPV6_ADDR_GEN_MODE="stable-privacy"
NAME="ens33"
UUID="1f275e53-bf1a-4c3b-b72d-115e366c0ecc"
DEVICE="ens33"
ONBOOT="yes"
ping 百度 看看网络是否通
[root@localhost network-scripts]# ping www.baidu.com
PING www.a.shifen.com (112.80.248.76) 56(84) bytes of data.
64 bytes from 112.80.248.76 (112.80.248.76): icmp_seq=1 ttl=58 time=3.52 ms
64 bytes from 112.80.248.76 (112.80.248.76): icmp_seq=2 ttl=58 time=3.38 ms
64 bytes from 112.80.248.76 (112.80.248.76): icmp_seq=3 ttl=58 time=3.08 ms
1.3 修改主机名称
让后面的主机名称相关配置显得标准一点,也让作者便于记忆
hostnamectl set-hostname host.cyx.com
[root@localhost network-scripts]# hostnamectl set-hostname host.cyx.com
[root@localhost network-scripts]# hostname
host.cyx.com
[root@localhost network-scripts]#
1.4 关闭防火墙,防止docker访问端口时候有限制,不然要单独放开各种端口。
systemctl stop firewalld && systemctl disable firewalld
[root@localhost network-scripts]# systemctl stop firewalld && systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@localhost network-scripts]#
#iptables 防火墙 ,第二种防火墙类型,也关闭
[root@localhost network-scripts]# service iptables stop && systemctl disable iptables
1.5 关闭selinux
SElinux(Security Enhanced Linux) 是linux 安全系统
三剑客 grep 、sed(对某行数据进行curd) 、awt(报告生成器,格式化文本)
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
# 查看当前配置
[root@localhost network-scripts]# getenforce
Enforcing
# 修改配置文件
[root@localhost network-scripts]# sed -i 's/SELINUX=enforcing/SELINUX=disable/g' /etc/selinux/config
# 重启生效
[root@localhost network-scripts]# reboot
1.6 时间同步
时间同步外网服务器 cn.pool.ntp.org ,如果公司有自己的时间同步服务器也可以天这个服务器地址
安装时间同步组件
[root@host chenj1]# yum install -y ntp ntpdate
设置时间同步的定时任务
[root@host chenj1]# crontab -e
保存下面输入文件
* */1 * * * /usr/sbin/ntpdate cn.pool.ntp.org
重启任务生效
[root@host chenj1]# service crond restart
#安装时间同步组件
[root@host chenj1]# yum install -y ntp ntpdate
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: mirrors.163.com
* extras: mirrors.163.com
* updates: mirrors.163.com
#此处返回表示已经安装最新组件
Package ntp-4.2.6p5-29.el7.centos.2.x86_64 already installed and latest version
Package ntpdate-4.2.6p5-29.el7.centos.2.x86_64 already installed and latest version
Nothing to do
#设置时间同步的定时任务
[root@host chenj1]# crontab -e
no crontab for root - using an empty one
crontab: installing new crontab
#保存下面输入文件
* */1 * * * /usr/sbin/ntpdate cn.pool.ntp.org
#重启任务
[root@host chenj1]# service crond restart
#查看定时任务保存在哪
[root@host chenj1]# cd /var/spool/cron/
[root@host cron]# ll
total 4
-rw------- 1 root root 47 Sep 26 08:10 root
#这里可以看到一个root文件,因为我用的root账号登录,
#所以生成了一个root文件。
#通过cat命令可以看到这个文件的内容就是前面编辑的内容。
[root@host cron]# cat root
2 安装docker
2.1 在线安装
配置docker-ce 国内 yum 源(阿里云)完成在线安装
# 配置docker-ce 国内 yum 源(阿里云)完成在线安装
sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
#刷新缓存
sudo yum makecache fast
# 按调测使用docker时候可能会用到的工具
sudo yum -y install wget net-tools nfs-utils lrzsz gcc gcc-c++ make cmake libxml2-devel openssl-devel curl curl-devel unzip sudo ntplibaio-devel wget vim ncurses-devel autoconf automake zlib-devel python-devel epel-release openssh-server socat ipvsadm conntrack ntpdate
# 安装docker环境依赖
sudo yum install -y yum-utils device-mapper-persistent-data lvm2
# 安装docker-ce
sudo yum install -y docker-ce docker-ce-cli
# 安装一个额外的学习容器 生产环境不用安装
sudo yum install -y containerd.io
# 查看docker版本 安装的是最新版本
[root@host my-docker-start]# docker version
Client: Docker Engine - Community
Version: 20.10.8
API version: 1.41
Go version: go1.16.6
Git commit: 3967b7d
Built: Fri Jul 30 19:55:49 2021
OS/Arch: linux/amd64
Context: default
Experimental: true
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
[root@host my-docker-start]#
# 启动docker 并且设置 成 开机启动
[root@host my-docker-start]# systemctl start docker && systemctl enable docker
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
#查看docker启动结果
[root@host my-docker-start]# systemctl status docker
● docker.service - Docker Application Container Engine
Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)
#下面这个running 表示docker已经启动了
Active: active (running) since Sun 2021-09-26 08:45:58 PDT; 19s ago
Docs: https://docs.docker.com
Main PID: 5049 (dockerd)
CGroup: /system.slice/docker.service
└─5049 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd....
Sep 26 08:45:58 host.cyx.com dockerd[5049]: time="2021-09-26T08:45:58.328494067-07...pc
Sep 26 08:45:58 host.cyx.com dockerd[5049]: time="2021-09-26T08:45:58.328520041-07...pc
Sep 26 08:45:58 host.cyx.com dockerd[5049]: time="2021-09-26T08:45:58.328533572-07...pc
Sep 26 08:45:58 host.cyx.com dockerd[5049]: time="2021-09-26T08:45:58.360811011-07...."
Sep 26 08:45:58 host.cyx.com dockerd[5049]: time="2021-09-26T08:45:58.478611784-07...s"
Sep 26 08:45:58 host.cyx.com dockerd[5049]: time="2021-09-26T08:45:58.543156339-07...."
Sep 26 08:45:58 host.cyx.com dockerd[5049]: time="2021-09-26T08:45:58.566425872-07....8
Sep 26 08:45:58 host.cyx.com dockerd[5049]: time="2021-09-26T08:45:58.566560550-07...n"
Sep 26 08:45:58 host.cyx.com systemd[1]: Started Docker Application Container Engine.
Sep 26 08:45:58 host.cyx.com dockerd[5049]: time="2021-09-26T08:45:58.586589095-07...k"
Hint: Some lines were ellipsized, use -l to show in full.
#查看当前运行的docker 版本
root@host my-docker-start]# docker -v
Docker version 20.10.8, build 3967b7d
#查看当前下载的镜像 目前一个没有
[root@host my-docker-start]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
[root@host my-docker-start]#
2.2 离线安装
主要是配本地 yum 环境 -- 没试了