1.2 安装 docker 容器并配置镜像加速器

2023-05-16

1.2.1 实验环境准备

实验环境: CENTOS7.9 64 位

主机名: hou

主机 ip: 10.0.8.120(这个 ip 大家可以根据自己所在环境去配置,配置成静态 IP)

4g 内存、4vCPU、100G 硬盘、网络 NAT 模式

配置静态 IP

把虚拟机或者物理机配置成静态 ip 地址,这样机器重新启动后 ip 地址也不会发生改变。以 hou 主机为例,修改静态 IP:

修改/etc/sysconfig/network-scripts/ifcfg-ens33 文件,变成如下:


PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static
IPADDR=10.0.8.120
NETMASK=255.255.255.0
GATEWAY=10.0.8.254
DNS1=10.0.8.254
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
DEVICE=ens33
ONBOOT=yes  

#修改配置文件之后需要重启网络服务才能使配置生效,重启网络服务命令如下:

systemctl restart network

注:/etc/sysconfig/network-scripts/ifcfg-ens33 文件里的配置说明:

NAME=ens33         #网卡名字,跟 DEVICE 名字保持一致即可

DEVICE=ens33         #网卡设备名,大家 ip addr 可看到自己的这个网卡设备名,每个人机器可能这个名字不一样,需要写自己的

BOOTPROTO=static         #static 表示静态 ip 地址

ONBOOT=yes         #开机自启动网络,必须是 yes

IPADDR=10.0.8.120         #ip 地址,需要跟自己电脑所在网段一致

NETMASK=255.255.255.0         #子网掩码,需要跟自己电脑所在网段一致

GATEWAY=10.0.8.254         #网关,在自己电脑打开 cmd,输入 ipconfig /all 可看到

DNS1=10.0.8.254         #DNS,在自己电脑打开 cmd,输入 ipconfig /all 可看到

关闭 selinux


[root@hou ~]# setenforce 0
[root@hou ~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
注意:修改 selinux 配置文件之后,重启机器,selinux 才能永久生效
[root@hou ~]# getenforce
Disabled
#看到 Disabled 说明 selinux 成功关闭  

配置主机名


[root@hou ~]# systemctl stop firewalld && systemctl disable firewalld  

关闭防火墙


[root@hou ~]# systemctl stop firewalld && systemctl disable  firewalld  

配置时间同步


[root@hou ~]# yum install -y ntp ntpdate
[root@hou ~]# ntpdate cn.pool.ntp.org  

编写计划任务


[root@hou ~]# crontab -e
​
* */1 * * * /usr/sbin/ntpdate cn.pool.ntp.org  

重启 crond 服务使配置生效:


[root@hou ~]# systemctl restart crond  

选择一种安装 docker 的方式:

1. 在线安装

配置 docker-ce 国内 yum 源(阿里云)


[root@hou ~]#yum -y install yum-utils
[root@hou ~]# yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo  

2. 配置 docker-ce 的离线 yum 源:

下面需要的 k8s-docker.tar.gz 压缩包在附件里


[root@hou ~]# mkdir /opt -p
[root@hou ~]# tar xf k8s-docker.tar.gz -C /opt/
[root@hou ~]# tee /etc/yum.repos.d/k8s-docker.repo << 'EOF'
[k8s-docker]
name=k8s-docker
baseurl=file:///opt/k8s-docker
enable=1
gpgcheck=0
EOF  

开始安装 docker

安装基础软件包


[root@hou ~]# yum install -y wget net-tools nfs-utils lrzsz gcc gcc-c++ make cmake libxml2-devel openssl-devel curl curl-devel unzip sudo ntp libaio-devel wget vim ncurses-devel autoconf automake zlib-devel python-devel epel-release openssh-server socat ipvsadm conntrack   

安装 docker 环境


[root@hou ~]# yum install docker-ce -y  

启动 docker 服务


[root@hou ~]# systemctl start docker && systemctl enable docker  

查看 Docker 版本信息


[root@hou ~]# docker version   

查看 docker 状态


[root@hou ~]# systemctl status docker
#显示 running,说明 docker 正常起来了  

1.2.2 开启包转发功能和修改内核参数

内核参数修改:br_netfilter 模块用于将桥接流量转发至 iptables 链,br_netfilter 内核参数需要开启转发。


[root@hou ~]# cat > /etc/sysctl.d/docker.conf <<EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
EOF
[root@hou ~]# sysctl -p /etc/sysctl.d/docker.conf  

报错:


sysctl: cannot stat /proc/sys/net/bridge/bridge-nf-call-ip6tables: No such file or directory
sysctl: cannot stat /proc/sys/net/bridge/bridge-nf-call-iptables: No such file or directory  

解决方法:


[root@hou ~]# modprobe brnetfilter
[root@hou ~]# sysctl -p /etc/sysctl.d/docker.conf  

重启后模块失效,下面是开机自动加载模块的脚本

在/etc/新建 rc.sysinit 文件


[root@hou ~]# cat /etc/rc.sysinit
#!/bin/bash
for file in /etc/sysconfig/modules/*.modules ; do
[ -x $file ] && $file
done  

在/etc/sysconfig/modules/目录下新建文件如下


[root@hou ~]# cat /etc/sysconfig/modules/br_netfilter.modules
modprobe brnetfilter
增加权限
[root@hou ~]# chmod 755 /etc/sysconfig/modules/br_netfilter.modules  

重启机器模块也会自动加载


[root@hou ~]# lsmod |grep br_netfilter
br_netfilter 22209 0
bridge 136173 1 brnetfilter  

注:
Docker 安装后出现:WARNING: bridge-nf-call-iptables is disabled 的解决办法:
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1

将 Linux 系统作为路由或者 VPN 服务就必须要开启 IP 转发功能。当 Linux 主机有多个网卡时一个网卡收到的信息是否能够传递给其他的网卡 如果设置成 1 的话 可以进行数据包转发 可以实现VxLAN 等功能。不开启会导致 docker 部署应用无法访问。  

重启 docker


[root@hou ~]# systemctl restart docker  

1.2.3 docker 基本用法-操作镜镜像拉取过程

搜索镜像


[root@hou ~]# docker search centos #默认从 Docker Hub 中搜索镜像。
NAME                              DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
centos                            The official build of CentOS.                   7091      [OK]       
centos/systemd                    systemd enabled base container.                 107                  [OK]
centos/mysql-57-centos7           MySQL 5.7 SQL database server                   92                   
centos/postgresql-96-centos7      PostgreSQL is an advanced Object-Relational …   45                   
centos/httpd-24-centos7           Platform for running Apache httpd 2.4 or bui…   44                   
centos/python-35-centos7          Platform for building and running Python 3.5…   39                   
centos/php-56-centos7             Platform for building and running PHP 5.6 ap…   34                   
centos/mysql-56-centos7           MySQL 5.6 SQL database server                   22                   
centos/postgresql-10-centos7      PostgreSQL is an advanced Object-Relational …   19                   
kasmweb/centos-7-desktop          CentOS 7 desktop for Kasm Workspaces            18                   
centos/nginx-112-centos7          Platform for running nginx 1.12 or building …   16                   
centos/nodejs-8-centos7           Platform for building and running Node.js 8 …   14                   
centos/mariadb-101-centos7        MariaDB 10.1 SQL database server                13                   
centos/mongodb-36-centos7         MongoDB NoSQL database server                   8                    
centos/redis-32-centos7           Redis in-memory data structure store, used a…   6                    
centos/mariadb-102-centos7        MariaDB 10.2 SQL database server                6                    
centos/mongodb-34-centos7         MongoDB NoSQL database server                   3                    
centos/ruby-25-centos7            Platform for building and running Ruby 2.5 a…   3                    
continuumio/centos5_gcc5_base                                                     3                    
kasmweb/core-centos-7             CentOS 7 base image for Kasm Workspaces         2                    
datadog/centos-i386                                                               0                    
bitnami/centos-extras-base                                                        0                    
bitnami/centos-base-buildpack     Centos base compilation image                   0                    [OK]
ibmcom/fhe-toolkit-centos         The IBM Fully Homomorphic Encryption (FHE) T…   0                    
ibmcom/fhe-toolkit-centos-amd64   The IBM Fully Homomorphic Encryption (FHE) T…   0                      

下载镜像

方法 1:从 docker hub 上下载


[root@hou ~]# docker pull centos
Using default tag: latestError response from daemon: Get https://registry-1.docker.io/v2/: net/http: TLS handshake timeout  

#报错了,因为网络的问题。 无法连接到 dockerhub 下载镜像。 如果你的网络没有问题,你可以下载。

解决:可以换种方法下载镜像

方法 2:配置镜像加速器


[root@hou ~]# tee /etc/docker/daemon.json << 'EOF'
{
"registry-mirrors":["https://rsbud4vc.mirror.aliyuncs.com","https://registry.docker-cn.com","https://docker.mirrors.ustc.edu.cn","https://dockerhub.azk8s.cn","http://hub-mirror.c.163.com","http://qtid6917.mirror.aliyuncs.com","https://rncxm540.mirror.aliyuncs.com","https://e9yneuy4.mirror.aliyuncs.com"]
} 
EOF  

重启 docker 服务使配置生效


[root@hou ~]# systemctl daemon-reload && systemctl restart docker  

重新下载,就可以了。


[root@hou ~]# docker pull centos
[root@hou ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED        SIZE
centos       latest    5d0da3dc9764   6 months ago   231MB  

方法 3:把之前下载好的镜像通过 docker load -i 解压出来:

把 centos.tar.gz、nginx.tar.gz 镜像包上传到 hou机器的 root 目录,通过 docker load -i 解压镜像


[root@hou ~]# docker load -i /root/nginx.tar.gz  

#注意:把镜像打成离线包


[root@hou ~]# docker save -o centos.tar.gz centos  

查看镜像列表 列出本地所有镜像。


[root@hou ~]# docker images 
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
centos       latest    5d0da3dc9764   6 months ago    231MB
nginx        latest    f6d0b4767a6c   15 months ago   133MB  

注:docker 镜像相当于对操作系统+程序+程序依赖的库打一个包

软件是依赖操作系统中的库或二进制文件。 如果我把软件所依赖的库和二进制文件打包在一起发布,不用物理机系统上的文件,也就不依赖物理机操作系统了。

删除镜像


[root@hou ~]# docker rmi -f <镜像名字>  

镜像打标签


[root@hou ~]# docker tag centos:latest centos:v1  

查看帮助命令


[root@hou ~]# docker --help  

物料包:

链接:度娘网盘,自行取哈 
提取码:oopp

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

1.2 安装 docker 容器并配置镜像加速器 的相关文章

随机推荐