docker私服仓库搭建

2018-06-29  本文已影响0人  vcancy

docker官方提供私服镜像,我们可以很方便的启动一个docker私服仓库

mkdir -p /data/program/docker-registry
docker run -d -p 5000:5000 -v /data/program/docker-registry:/var/lib/registry --name docker-registry --restart=always registry

注意:这样启动的私服只能通过http访问,客户端使用需要做一定的配置

Ubuntu

nano /etc/docker/daemon.json
添加"insecure-registries"
{
"insecure-registries": ["x.x.x.x:5000"]
}

这样的私服镜像是公开可访问的,我们可以在docker-registry前加上nginx做https的代理转发同时配置用户登录

注:替换xx.com为你自己申请的域名

acme.sh域名证书配置

apt install socat

curl  https://get.acme.sh | sh

cd ~/.acme.sh/

申请签发 SSL 证书
sh acme.sh --issue -d xx.com --standalone

# 证书安装目录
mkdir -p /opt/certs

cd ~/.acme.sh/

安装证书

sh acme.sh --installcert -d xx.com --keypath /opt/certs/xx.com.key --fullchainpath /opt/certs/xx.com.pem --reloadcmd     "/data/release/nginx-1.15.0/sbin/nginx -s reload"

# /opt/certs/ 是证书安装目录
# /data/release/nginx-1.15.0/sbin/nginx为nginx安装路径
# reloadcmd 是nginx 的reload 命令  acme.sh 记住重启 Nginx 的命令,以后自动更新证书的动作需要重启 Nginx,然后你会看到结果

生成 dhparam.pem 文件
openssl dhparam -out /opt/certs/dhparam.pem 2048

配置docker 私服 与nginx

mkdir -p /data/program/docker-registry

cd /data/program/docker-registry

# 安装apache 工具箱
apt install apache2-utils
# 生成私服访问账号密码
htpasswd -bc nginx.htpasswd admin 123456

nginx 配置:/data/release/nginx-1.15.0/conf/conf.d/docker-registry.conf
测试访问:https://xx.com:10090/v2/
返回{}

docker-registry.conf

  upstream docker-registry {
    server 127.0.0.1:5000;
  }

  ## Set a variable to help us decide if we need to add the
  ## 'Docker-Distribution-Api-Version' header.
  ## The registry always sets this header.
  ## In the case of nginx performing auth, the header is unset
  ## since nginx is auth-ing before proxying.
  map $upstream_http_docker_distribution_api_version $docker_distribution_api_version {
    '' 'registry/2.0';
  }

  server {
    listen 10090 ssl;
    server_name xx.com;

    # SSL
    ssl_certificate /opt/certs/xx.com.pem;
    ssl_certificate_key /opt/certs/xx.com.key;

    # Recommendations from https://raymii.org/s/tutorials/Strong_SSL_Security_On_nginx.html
    ssl_protocols TLSv1.1 TLSv1.2;
    ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';
    ssl_prefer_server_ciphers on;
    ssl_session_cache shared:SSL:10m;

    # disable any limits to avoid HTTP 413 for large image uploads
    client_max_body_size 0;

    # required to avoid HTTP 411: see Issue #1486 (https://github.com/moby/moby/issues/1486)
    chunked_transfer_encoding on;

    location /v2/ {
      # Do not allow connections from docker 1.5 and earlier
      # docker pre-1.6.0 did not properly set the user agent on ping, catch "Go *" user agents
      if ($http_user_agent ~ "^(docker\/1\.(3|4|5(?!\.[0-9]-dev))|Go ).*$" ) {
        return 404;
      }

      # To add basic authentication to v2 use auth_basic setting.
      auth_basic "Registry realm";
      auth_basic_user_file /data/program/docker-registry/nginx.htpasswd;

      ## If $docker_distribution_api_version is empty, the header is not added.
      ## See the map directive above where this variable is defined.
      add_header 'Docker-Distribution-Api-Version' $docker_distribution_api_version always;

      proxy_pass                          http://docker-registry;
      proxy_set_header  Host              $http_host;   # required for docker client's sake
      proxy_set_header  X-Real-IP         $remote_addr; # pass on real client's IP
      proxy_set_header  X-Forwarded-For   $proxy_add_x_forwarded_for;
      proxy_set_header  X-Forwarded-Proto $scheme;
      proxy_read_timeout                  900;
    }
  }
上一篇下一篇

猜你喜欢

热点阅读