------------------------------------------------------

Docker官网:https://docs.docker.com/

Docker的github地址:https://github.com/moby/moby

Dockerhub官网

https://registry.hub.docker.com

如果docker官方registry拉取镜像速度很慢,可以尝试daocloud提供的加速器服务

https://dashboard.daocloud.io/mirror

1、docker是什么?

Docker 是一个开源项目,诞生于 2013 年初,最初是 dotCloud 公司内部的一个业余项目。它基于 Google 公司推出的 Go 语言实现。项目后来加入了 Linux 基金会,遵从了 Apache 2.0 协议,项目代码在GitHub 上进行维护。

Docker是一个开源的引擎,可以轻松的为任何应用创建一个轻量级的、可移植的、自给自足的容器。开发者可以打包他们的应用以及依赖包到一个可移植的镜像中,然后发布到任何支持docker的机器上运行。容器是完全使用沙箱机制,相互之间不会有任何接口调用。

Docker logo:

dokcer的安装和配置_nginx

Docker的思想来自于集装箱,集装箱解决了什么问题?在一艘大船上,可以把货物规整的摆放起来。并且各种各样的货物被装在集装箱里,集装箱和集装箱之间不会互相影响。那么我就不需要专门运送蔬菜的船和专门运送货物的船了。只要这些货物在集装箱里封装的好好的,那我就可以用一艘大船把他们都运走。

docker就是类似的理念。云计算就好比大货轮。docker就是集装箱。

2、docker的优点

1)快

运行时的性能快,管理操作(启动,停止,开始,重启等等) 都是以秒或毫秒为单位的。

2)敏捷

像虚拟机一样敏捷,而且会更便宜,在bare metal(裸机)上布署像点个按钮一样简单。

3)灵活

将应用和系统“容器化”,不添加额外的操作系统

4)轻量

在一台服务器上可以布署100~1000个Containers容器。

5)便宜

开源的,免费的,低成本的。

docker-ce:社区版

docker-ee: 商业版

3、docker缺点

所有容器共用linux kernel资源,资源能否实现最大限度利用,所以在安全上也会存在漏洞。

4、安装Docker

主机ip:192.168.0.52

Centos7.6-centos7.9

4Gib/2vCPU

配置主机名:

[root@holymaster1 ~]# hostnamectl set-hostname holymaster1 && bash

关闭防火墙

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

关闭iptables防火墙

[root@holymaster1 ~]# yum install iptables-services -y  #安装iptables

禁用iptables

root@holymaster1 ~]# service iptables stop   && systemctl disable iptables

清空防火墙规则

[root@holymaster1~]# iptables -F 

关闭selinux

[root@holymaster1 ~]# setenforce 0

[root@holymaster1 ~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config

注意:修改selinux配置文件之后,重启机器,selinux才能永久生效

[root@holymaster1 ~]# getenforce

显示Disabled表示selinux关闭成功

#配置时间同步

[root@holymaster1 ~]# yum install -y ntp ntpdate

[root@holymaster1 ~]# ntpdate cn.pool.ntp.org 

#编写计划任务

[root@holymaster1 ~]# crontab -e 

* */1 * * * /usr/sbin/ntpdate   cn.pool.ntp.org

重启crond服务使配置生效:

[root@holymaster1 ~]# systemctl restart crond

安装基础软件包

[root@holymaster1 ~]# 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-ce

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

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

安装docker依赖包

[root@holymaster1 ~]# yum install -y yum-utils device-mapper-persistent-data lvm2

安装docker-ce

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

#启动docker服务

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

[root@holymaster1 ~]# systemctl status docker

● docker.service - Docker Application Container Engine

Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)

Active: active (running) since 日 2024-03-17 21:31:03 CST; 28min ago

Docs: https://docs.docker.com

看到running,表示docker正常运行

#查看Docker 版本信息

[root@holymaster1 ~]# docker version    

dokcer的安装和配置_nginx_02

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

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

[root@holymaster1 ~]# modprobe br_netfilter

[root@holymaster1 ~]# cat > /etc/sysctl.d/docker.conf <

net.bridge.bridge-nf-call-ip6tables = 1

net.bridge.bridge-nf-call-iptables = 1

net.ipv4.ip_forward = 1

EOF

#使参数生效

[root@holymaster1 ~]# sysctl -p /etc/sysctl.d/docker.conf

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

在/etc/新建rc.sysinit 文件

cat /etc/rc.sysinit

#!/bin/bashfor file in /etc/sysconfig/modules/*.modules ; do

[ -x $file ] && $file

done


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

cat /etc/sysconfig/modules/br_netfilter.modulesmodprobe br_netfilter

增加权限

[root@holymaster1 ~]# chmod 755 /etc/sysconfig/modules/br_netfilter.modules

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

[root@holymaster1 ~]# lsmod |grep br_netfilter

br_netfilter 22256 0

bridge 155432 1 br_netfilter

注: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@holymaster1 ~]# systemctl restart docker  

6、配置docker镜像加速器

登陆阿里云镜像仓库

https://cr.console.aliyun.com/cn-hangzhou/instances/mirrors

如果没有开通,可开通阿里云的镜像服务

dokcer的安装和配置_docker_03

找到镜像加速器,然后按照箭头方向操作

修改/etc/docker/daemon.json,变成如下

{

 "registry-mirrors":["https://wsnzwitf.mirror.aliyuncs.com","https://registry.docker-cn.com","https://docker.mirrors.ustc.edu.cn","https://dockerhub.azk8s.cn","http://hub-mirror.c.163.com"]

}

让配置文件生效

sudo systemctl daemon-reload

sudo systemctl restart docker

dokcer的安装和配置_nginx_04

7、docker的基本用法

7.1 镜像相关操作

#从dockerhub查找镜像

[root@holymaster1 ~]# docker search centos

dokcer的安装和配置_centos_05

解释说明:

NAME: 镜像仓库源的名称

DESCRIPTION: 镜像的描述

OFFICIAL: 是否 docker 官方发布

stars: 类似 Github 里面的 star,表示点赞、喜欢的意思。

AUTOMATED: 自动构建。

#下载镜像

[root@holymaster1 ~]# docker pull centos

#查看本地镜像

[root@holymaster1 ~]# docker images

#把镜像做成离线压缩包

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

#解压离线镜像包

[root@holymaster1 ~]# docker load -i centos.tar.gz

#删除镜像

[root@holymaster1 ~]# docker rmi -f centos:latest

7.2 容器相关操作

7.2.1 以交互式方式启动并进入容器

[root@holymaster1 ~]# docker run --name=hello -it centos /bin/bash

[root@d7b8419e113f /]#

输入exit,退出容器,退出之后容器也会停止,不会再前台运行

#docker run运行并创建容器

--name 容器的名字

-i 交互式

-t 分配伪终端

centos: 启动docker需要的镜像

/bin/bash说明你的shell类型为bash,bash shell是最常用的一种shell, 是大多数Linux发行版默认的shell。 此外还有C shell等其它shell。

7.2.2 以守护进程方式启动容器

[root@holymaster1 ~]# docker run --name=hello1 -td centos

[root@holymaster1 ~]# docker ps |grep hello1

1a2b73ba0ac2   centos            "/bin/bash"             hello1

-d在后台运行docker

[root@holymaster1 ~]# docker exec -it hello1 /bin/bash

7.2.3 查看正在运行的容器

docker ps

[root@holymaster1 ~]# docker ps -a  #查看所有容器,包括运行和退出的容器

7.2.4 停止容器

docker stop hello1

7.2.5 启动已经停止的容器

docker start hello1

7.2.6 进入容器

docker exec -it hello1 /bin/bash

[root@holymaster1 ~]# docker rm -f hello1 #删除容器

[root@holymaster1 ~]# docker --help

#查看docker帮助命令

8、通过docker部署nginx服务

[root@holymaster1 ~]# docker run --name nginx -p  80 -itd centos

#-p把容器端口随机在物理机随机映射一个端口

[root@holymaster1 ~]# docker ps | grep nginx

4ee68bf3dc46 centos "/bin/bash" 3 minutes ago Up 3 minutes 0.0.0.0:32768->80/tcp, :::32768->80/tcp nginx

# 在docker里安装nginx

docker exec -it nginx /bin/bash

[root@ecfa046e9681]# ip addr

dokcer的安装和配置_centos_06

#通过上面可以看到容器的ip是172.17.0.2

#yum安装nginx

rm -rf /etc/yum.repos.d/*

curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo

yum install wget -y

yum install nginx -y

安装文本编辑器vim

yum install vim-enhanced -y

创建静态页面

mkdir /var/www/html -p

cd /var/www/html/

cat index.html


dokcer的安装和配置_nginx_07

修改nginx配置文件中的root路径,如下

vim /etc/nginx/nginx.conf

root         /var/www/html/;

启动nginx

/usr/sbin/nginx

访问docker里的nginx服务,复制一个终端窗口,执行如下命令

[root@holymaster1 ~]# docker ps | grep nginx

4ee68bf3dc46 centos "/bin/bash" 12 minutes ago Up 12 minutes 0.0.0.0:32768->80/tcp, :::32768->80/tcp nginx

#能查看到nginx容器在物理机映射的端口是32768

[root@holymaster1 ~]# curl http://192.168.0.52:32768


dokcer的安装和配置_centos_08

也可以直接访问容器的ip:port

[root@holymaster1 ~]# curl 172.17.0.2:80

dokcer的安装和配置_nginx_09

流量走向:

访问物理节点ip:port(容器在物理节点映射的端口)-->容器ip:port(容器里部署的服务的端口)-->就可以访问到容器里部署的应用了