Nginx反向代理Docker私有仓库

向右看齐 2022-04-08 12:15 486阅读 0赞

环境

  • 系统:CentOS7
  • IP地址:192.168.253.128
  • Nginx版本:1.12.2
  • Docke版本:18.09.0

安装

第一步:关闭防火墙和selinux

  1. systemctl stop firewalld
  2. setenforce 0

第二步:安装Docker 18.09.0

这里使用yum方式安装,rpm包下载地址为 https://download.docker.com/linux/centos/7/x86_64/stable/Packages/
我已经提前下载好了,这里就直接用了

  1. yum install containerd.io-1.2.0-3.el7.x86_64.rpm \
  2. > docker-ce-18.09.0-3.el7.x86_64.rpm \
  3. > docker-ce-cli-18.09.0-3.el7.x86_64.rpm -y

第三步:安装Docker私有仓库

  1. yum install -y docker-distribution

第四步:启动Docker与Docker私有仓库

  1. systemctl start docker
  2. systemctl start docker-distribution

在这里插入图片描述

先拉取几个基础镜像,等一下用来测试

  1. docker image pull centos
  2. docker image pull busybox

第五步:安装Nginx

Nginx在epel源中,所以先下载epel源,然后再安装nginx

  1. yum install epel-release -y
  2. yum install nginx -y

第六步:修改Nginx配置文件并启动nginx

修改配置文件/etc/nginx/nginx.conf 添加下列内容

  1. client_max_body_size 0;
  2. location / {
  3. proxy_pass http://192.168.253.128:5000;
  4. proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
  5. proxy_redirect off;
  6. proxy_buffering off;
  7. proxy_set_header Host $host;
  8. proxy_set_header X-Real-IP $remote_addr;
  9. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  10. }

启动Nginx

  1. systemctl start nginx

第七步:镜像推送到私有的仓库

先给基础镜像打标签

  1. docker image tag docker.io/centos 192.168.253.128:80/centos:v1

推送到代理服务器

  1. [root@localhost ~]# docker image push 192.168.253.128:80/centos:v1
  2. The push refers to repository [192.168.253.128:80/centos]
  3. Get https://192.168.253.128:80/v2/: http: server gave HTTP response to HTTPS client

这时候出现报错,问题在于推送的协议是http,而本来是要求https协议的。解决方法有2个,将nginx设置ssl连接,或者设置Docker的启动参数,使得允许http协议推送
在这里插入图片描述

这里使用第二个方法,设置一下允许http方式推送
(1)修改/etc/docker/daemon.json文件(如果不存在,则创建)

  1. [root@localhost ~]# cat /etc/docker/daemon.json
  2. {
  3. "insecure-registries" : ["192.168.253.128:80"]
  4. }

(2)重启docker

  1. systemctl restart docker

然后再次推送

  1. docker image push 192.168.253.128:80/centos:v1

在这里插入图片描述
可以看到成功了

删除本地镜像
在这里插入图片描述
从私有仓库拉取

  1. docker image pull 192.168.253.128:80/centos:v1

在这里插入图片描述

参考的官方文档:
https://docs.docker-cn.com/engine/installation/linux/docker-ce/centos/#先决条件
https://docs.docker.com/registry/insecure/#deploy-a-plain-http-registry
https://docs.docker.com/config/daemon/systemd/#httphttps-proxy (如果代理设置失败可以参考这个文档,自己测试的时候发现需要按照这个文档进行proxy相关参数的设置,但是写博客的时候好像不需要也能成功。)

发表评论

表情:
评论列表 (有 0 条评论,486人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Docker Nginx 反向代理

    最近在系统性梳理网关的知识,其中网关的的功能有一个是代理,正好咱们常用的Nginx也具备次功能,今天正好使用Nginx实现一下反向代理,与后面网关的代理做一个对比,因为我使用的

    相关 Docker私有仓库

    简介 Docker的Hub上可以保存镜像,但是这个站点会比较慢,如果有需求的话可以本地搭建一个私有仓库,方便管理,使用也比较方便。 安装 第一步:安装