一、查看内核版本(要求大于3.10)
[root@ root ~]# uname -r
3.10.0-957.el7.x86_64
二、检验是否有旧版的docker
[root@root ~]# yum list installed | grep docker
[root@root ~]#
我这里显示没装过。如果有装的旧版,卸载命令:
[root@root ~]# yum -y remove docker-ce.x86_64 xxxxx.ce-1.el7.centos @docker-ce-stable
//或者
[root@root ~]# sudo yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
三、安装
安全前准备
3.1 更新安装包
注意:安装需要使用root用户操作
[root@root ~]# yum update
3.2 首次安装先设置仓库,后续可以从仓库安装和更新 Docker。
设置仓库:
安装依赖包:(yum-utils 提供了 yum-config-manager ,并且 device mapper 存储驱动程序需要 device-mapper-persistent-data 和 lvm2。)
[root@root ~]# sudo yum install -y yum-utils device-mapper-persistent-data lvm2
3.3 设置稳定的仓库(更新yun源)
[root@root ~]# yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
3.4 正式安装
查看所能安装的版本
[root@root sgd]# yum list docker-ce --showduplicates | sort -r
已加载插件:fastestmirror, langpacks
可安装的软件包
* updates: mirrors.aliyun.com
Loading mirror speeds from cached hostfile
* extras: mirrors.aliyun.com
docker-ce.x86_64 3:23.0.1-1.el7 docker-ce-stable
docker-ce.x86_64 3:23.0.0-1.el7 docker-ce-stable
docker-ce.x86_64 3:20.10.9-3.el7 docker-ce-stable
docker-ce.x86_64 3:20.10.8-3.el7 docker-ce-stable
docker-ce.x86_64 3:20.10.7-3.el7 docker-ce-stable
docker-ce.x86_64 3:20.10.6-3.el7 docker-ce-stable
docker-ce.x86_64 3:20.10.5-3.el7 docker-ce-stable
docker-ce.x86_64 3:20.10.4-3.el7 docker-ce-stable
docker-ce.x86_64 3:20.10.3-3.el7 docker-ce-stable
docker-ce.x86_64 3:20.10.2-3.el7 docker-ce-stable
docker-ce.x86_64 3:20.10.23-3.el7 docker-ce-stable
docker-ce.x86_64 3:20.10.22-3.el7 docker-ce-stable
docker-ce.x86_64 3:20.10.21-3.el7 docker-ce-stable
docker-ce.x86_64 3:20.10.20-3.el7 docker-ce-stable
docker-ce.x86_64 3:20.10.19-3.el7 docker-ce-stable
docker-ce.x86_64 3:20.10.18-3.el7 docker-ce-stable
docker-ce.x86_64 3:20.10.17-3.el7 docker-ce-stable
docker-ce.x86_64 3:20.10.16-3.el7 docker-ce-stable
docker-ce.x86_64 3:20.10.15-3.el7 docker-ce-stable
docker-ce.x86_64 3:20.10.14-3.el7 docker-ce-stable
docker-ce.x86_64 3:20.10.1-3.el7 docker-ce-stable
docker-ce.x86_64 3:20.10.13-3.el7 docker-ce-stable
docker-ce.x86_64 3:20.10.12-3.el7 docker-ce-stable
docker-ce.x86_64 3:20.10.11-3.el7 docker-ce-stable
docker-ce.x86_64 3:20.10.10-3.el7 docker-ce-stable
docker-ce.x86_64 3:20.10.0-3.el7 docker-ce-stable
docker-ce.x86_64 3:19.03.9-3.el7 docker-ce-stable
docker-ce.x86_64 3:19.03.8-3.el7 docker-ce-stable
docker-ce.x86_64 3:19.03.7-3.el7 docker-ce-stable
docker-ce.x86_64 3:19.03.6-3.el7 docker-ce-stable
docker-ce.x86_64 3:19.03.5-3.el7 docker-ce-stable
docker-ce.x86_64 3:19.03.4-3.el7 docker-ce-stable
docker-ce.x86_64 3:19.03.3-3.el7 docker-ce-stable
docker-ce.x86_64 3:19.03.2-3.el7 docker-ce-stable
docker-ce.x86_64 3:19.03.15-3.el7 docker-ce-stable
docker-ce.x86_64 3:19.03.14-3.el7 docker-ce-stable
docker-ce.x86_64 3:19.03.1-3.el7 docker-ce-stable
docker-ce.x86_64 3:19.03.13-3.el7 docker-ce-stable
docker-ce.x86_64 3:19.03.12-3.el7 docker-ce-stable
docker-ce.x86_64 3:19.03.11-3.el7 docker-ce-stable
docker-ce.x86_64 3:19.03.10-3.el7 docker-ce-stable
docker-ce.x86_64 3:19.03.0-3.el7 docker-ce-stable
docker-ce.x86_64 3:18.09.9-3.el7 docker-ce-stable
docker-ce.x86_64 3:18.09.8-3.el7 docker-ce-stable
docker-ce.x86_64 3:18.09.7-3.el7 docker-ce-stable
docker-ce.x86_64 3:18.09.6-3.el7 docker-ce-stable
docker-ce.x86_64 3:18.09.5-3.el7 docker-ce-stable
docker-ce.x86_64 3:18.09.4-3.el7 docker-ce-stable
docker-ce.x86_64 3:18.09.3-3.el7 docker-ce-stable
docker-ce.x86_64 3:18.09.2-3.el7 docker-ce-stable
docker-ce.x86_64 3:18.09.1-3.el7 docker-ce-stable
docker-ce.x86_64 3:18.09.0-3.el7 docker-ce-stable
docker-ce.x86_64 18.06.3.ce-3.el7 docker-ce-stable
docker-ce.x86_64 18.06.2.ce-3.el7 docker-ce-stable
docker-ce.x86_64 18.06.1.ce-3.el7 docker-ce-stable
docker-ce.x86_64 18.06.0.ce-3.el7 docker-ce-stable
docker-ce.x86_64 18.03.1.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 18.03.0.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.12.1.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.12.0.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.09.1.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.09.0.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.06.2.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.06.1.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.06.0.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.03.3.ce-1.el7 docker-ce-stable
docker-ce.x86_64 17.03.2.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.03.1.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.03.0.ce-1.el7.centos docker-ce-stable
* base: mirrors.aliyun.com
[root@root sgd]#
本次选择18.06.3.ce-3.el7
[root@root sgd]# yum install docker-ce-18.06.3.ce-3.el7
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
正在解决依赖关系
--> 正在检查事务
---> 软件包 docker-ce.x86_64.0.18.06.3.ce-3.el7 将被 安装
--> 正在处理依赖关系 container-selinux >= 2.9,它被软件包 docker-ce-18.06.3.ce-3.el7.x86_64 需要
--> 正在检查事务
---> 软件包 container-selinux.noarch.2.2.119.2-1.911c772.el7_8 将被 安装
--> 解决依赖关系完成
依赖关系解决
====================================================================================================================================================
Package 架构 版本 源 大小
====================================================================================================================================================
正在安装:
docker-ce x86_64 18.06.3.ce-3.el7 docker-ce-stable 41 M
为依赖而安装:
container-selinux noarch 2:2.119.2-1.911c772.el7_8 extras 40 k
事务概要
====================================================================================================================================================
安装 1 软件包 (+1 依赖软件包)
总下载量:41 M
安装大小:168 M
Is this ok [y/d/N]: y
Downloading packages:
(1/2): container-selinux-2.119.2-1.911c772.el7_8.noarch.rpm | 40 kB 00:00:00
warning: /var/cache/yum/x86_64/7/docker-ce-stable/packages/docker-ce-18.06.3.ce-3.el7.x86_64.rpm: Header V4 RSA/SHA512 Signature, key ID 621e9f35: NOKEY
docker-ce-18.06.3.ce-3.el7.x86_64.rpm 的公钥尚未安装
(2/2): docker-ce-18.06.3.ce-3.el7.x86_64.rpm | 41 MB 00:00:03
----------------------------------------------------------------------------------------------------------------------------------------------------
总计 11 MB/s | 41 MB 00:00:03
从 https://download.docker.com/linux/centos/gpg 检索密钥
导入 GPG key 0x621E9F35:
用户ID : "Docker Release (CE rpm) <[email protected]>"
指纹 : 060a 61c5 1b55 8a7f 742b 77aa c52f eb6b 621e 9f35
来自 : https://download.docker.com/linux/centos/gpg
是否继续?[y/N]:y
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
正在安装 : 2:container-selinux-2.119.2-1.911c772.el7_8.noarch 1/2
setsebool: SELinux is disabled.
正在安装 : docker-ce-18.06.3.ce-3.el7.x86_64 2/2
验证中 : docker-ce-18.06.3.ce-3.el7.x86_64 1/2
验证中 : 2:container-selinux-2.119.2-1.911c772.el7_8.noarch 2/2
已安装:
docker-ce.x86_64 0:18.06.3.ce-3.el7
作为依赖被安装:
container-selinux.noarch 2:2.119.2-1.911c772.el7_8
完毕!
3.5、设置开机启动
[root@root sgd]# systemctl enable docker
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
//启动
[root@root sgd]# systemctl start docker
3.6 验证
[root@root sgd]# docker version
Client:
Version: 18.06.3-ce
API version: 1.38
Go version: go1.10.3
Git commit: d7080c1
Built: Wed Feb 20 02:26:51 2019
OS/Arch: linux/amd64
Experimental: false
Server:
Engine:
Version: 18.06.3-ce
API version: 1.38 (minimum version 1.12)
Go version: go1.10.3
Git commit: d7080c1
Built: Wed Feb 20 02:28:17 2019
OS/Arch: linux/amd64
Experimental: false
3.7 测试
[root@root sgd]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@root sgd]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
[root@root sgd]#
四、基于Docker安装Nginx镜像
4.1 查看docker已经下载的(已有的)镜像
[root@root sgd]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
4.2 查询官方公有仓库nginx镜像
[root@root sgd]# docker search nginx
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
nginx Official build of Nginx. 18297 [OK]
linuxserver/nginx An Nginx container, brought to you by LinuxS… 188
bitnami/nginx Bitnami nginx Docker Image 155 [OK]
ubuntu/nginx Nginx, a high-performance reverse proxy & we… 83
privatebin/nginx-fpm-alpine PrivateBin running on an Nginx, php-fpm & Al… 72 [OK]
bitnami/nginx-ingress-controller Bitnami Docker Image for NGINX Ingress Contr… 23 [OK]
rancher/nginx-ingress-controller 11
kasmweb/nginx An Nginx image based off nginx:alpine and in… 4
ibmcom/nginx-ingress-controller Docker Image for IBM Cloud Private-CE (Commu… 4
bitnami/nginx-ldap-auth-daemon 3
bitnami/nginx-exporter 3
rapidfort/nginx RapidFort optimized, hardened image for NGINX 3
circleci/nginx This image is for internal use 2
rancher/nginx 2
rancher/nginx-ingress-controller-defaultbackend 2
vmware/nginx 2
bitnami/nginx-intel 1
rapidfort/nginx-official RapidFort optimized, hardened image for NGIN… 1
vmware/nginx-photon 1
rancher/nginx-conf 0
ibmcom/nginx-ppc64le Docker image for nginx-ppc64le 0
ibmcom/nginx-ingress-controller-ppc64le Docker Image for IBM Cloud Private-CE (Commu… 0
rancher/nginx-ssl 0
rapidfort/nginx-ib RapidFort optimized, hardened image for NGIN… 0
continuumio/nginx-ingress-ws 0
4.3 拉取该镜像(安装最新版)
[root@root sgd]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
f1f26f570256: Pull complete
84181e80d10e: Pull complete
1ff0f94a8007: Pull complete
d776269cad10: Pull complete
e9427fcfa864: Pull complete
d4ceccbfc269: Pull complete
Digest: sha256:f4e3b6489888647ce1834b601c6c06b9f8c03dee6e097e13ed3e28c01ea3ac8c
Status: Downloaded newer image for nginx:latest
[root@root sgd]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest ac232364af84 3 days ago 142MB
4.4 启动一下该镜像,使用nginx服务,代理本机8080端口(测试是不是好使)
-d: 后台运行
-p: 端口映射 冒号前是本机端口,冒号后是容器端口
[root@root sgd]# docker run -d -p 8080:80 --name nginx-8080 nginx
35b26798fd4a5dfb49a70a583fdab2cdcdaac9a03f809c0868d9c08ffea6660c
[root@shuqi2 sgd]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
35b26798fd4a nginx "/docker-entrypoint.…" 27 seconds ago Up 26 seconds 0.0.0.0:8080->80/tcp nginx-8080
8080端口已被监听;
五、本机创建实例文件夹
5.1 /文件夹下新建sgd文件夹,/sgd文件夹下新建nginx文件夹,nginx文件夹下新建conf.d文件夹,html文件夹,大致结构如下:
/sgd
|----nginx
|----conf.d
|----html
5.2 在conf.d文件夹下新建default.conf文件,内容如下:
server {
listen 80;
server_name localhost;
# 原来的配置,匹配根路径
#location / {
# root /usr/share/nginx/html;
# index index.html index.htm;
#}
# 更该配置,匹配/路径,修改index.html的名字,用于区分该配置文件替换了容器中的配置文件
location / {
root /usr/share/nginx/html;
index index-test.html index.htm;
}
}
5.3.在html中编写index-test.html,用以判断文件夹映射成功,内容如下:
<html>
<body>
<h2>it is html1</h2>
</body>
</html>
5.4 启动nginx(8080),映射路径
启动代码如下:
docker run -d -p 8080:80 -v /sgd/nginx/conf.d:/etc/nginx/conf.d -v /sgd/nginx/html:/usr/share/nginx/html nginx
-v的意思就是冒号前面代表本机路径,冒号后面代表容器内的路径,两个路径进行了映射,本机路径中的文件会覆盖容器内的文件。
nginx容器内的一些文件位置:
日志位置:/var/log/nginx/
配置文件位置:/etc/nginx/
项目位置:/usr/share/nginx/html
[root@root nginx]# docker run -d -p 8080:80 -v /sgd/nginx/conf.d:/etc/nginx/conf.d -v /sgd/nginx/html:/usr/share/nginx/html nginx
3ac4d879480772a71a3d025d80b3db3f0c2d35fc3f075b9bee129473b80e77ea
验证:
六、反向代理
此时静态页面网站已经部署上了,但是还是会显示一个端口8080出来,就十分不爽,怎么把端口干掉呢?而是换成XXXXX.com/demo1 或者 XXXXX.com/demo2这种效果呢?下面使用nginx的反向代理实现。
6.1. 增加文件夹,达到下面的效果
/sgd
|----nginx
|----conf.d
|----html
|----conf.d2
我们在conf.d2中配置另一个nginx容器的配置文件,文件内容如下:
server {
listen 80;
server_name localhost;
location /demo1 {
# 在该位置配置反向代理,将ip/demo1请求拦截,发送给8080端口,如果不是本机请使用公网ip
proxy_pass http://你的刚才的ip地址:8080/;
}
}
6.2 再启动一个nginx(80),专门作为反向代理映射,将本机80端口代理到nginx的80端口上,并映射两端的配置文件地址。
[root@root conf.d2]# docker run -d -p 80:80 -v /sgd/nginx/conf.d2:/etc/nginx/conf.d nginx
48dea2002a816a19255bc23a78111a42b4198d9ec357a0afff603c77a878e92b
demo1:
6.3 负载均衡
当有了反向代理后,自然而然就引出了负载均衡,下面简单实现负载均衡的效果,实现该效果再添加一个nginx,所以要增加一个文件夹。
/sgd
|----nginx
|----conf.d
|----html
|----conf.d2
|----html3
html3中的代码如下:
<html>
<body>
<h2>it is html3</h2>
</body>
</html>
[root@root html3]# docker run -d -p 8081:80 -v /sgd/nginx/conf.d:/etc/nginx/conf.d -v /sgd/nginx/html3:/usr/share/nginx/html nginx
ac5ae4250d915f66ce6bd77a9604d2b3cea36fe48c3f678b61490710a6fc478f
[root@root html3]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ac5ae4250d91 nginx "/docker-entrypoint.…" 24 seconds ago Up 23 seconds 0.0.0.0:8081->80/tcp vigorous_wright
48dea2002a81 nginx "/docker-entrypoint.…" 10 minutes ago Up 10 minutes 0.0.0.0:80->80/tcp eloquent_fermi
3ac4d8794807 nginx "/docker-entrypoint.…" About an hour ago Up About an hour 0.0.0.0:8080->80/tcp clever_lewin
[root@root html3]#
6.4 配置负载均衡,访问ip/demo1时,平均分发到8080端口和8081端口上,即it is html1和it is html3间接出现。
配置负载均衡,那就是配置在第二次的nginx上,就是反向代理的nginx上,我们去conf.d2文件夹下,修改default.conf文件,如下:
upstream group1{
server 你的刚才的ip地址:8080;
server 你的刚才的ip地址:8081;
}
server {
listen 80;
server_name localhost;
location /demo1 {
proxy_pass http://group1/;
}
}