Nexus3 中 docker 仓库 配置 Https

先给 Nexus3中 配置 docker 仓库

https://blog.csdn.net/mshxuyi/article/details/116357467

接下文: 

一、生成证书颁发机构证书

1、生成 CA 证书私钥

openssl genrsa -out ca.key 4096

2、生成 CA 证书 

# 调整 -subj 选项中的值以反映您的组织
openssl req -x509 -new -nodes -sha512 -days 3650 \
-subj "/C=CN/ST=Shanghai/L=Shanghai/O=example/OU=Personal/CN=nexus.mshxuyi.com" \
-key ca.key \
-out ca.crt

二、生成服务器证书

 证书通常包含一个 .crt 文件和一个 .key 文件

1、生成私钥

openssl genrsa -out nexus.mshxuyi.com.key 4096

2、生成证书签名请求(CSR)

openssl req -sha512 -new \
-subj "/C=CN/ST=Beijing/L=Beijing/O=example/OU=Personal/CN=nexus.mshxuyi.com" \
-key nexus.mshxuyi.com.key \
-out nexus.mshxuyi.com.csr

3、生成一个 x509 v3 扩展文件

cat > v3.ext <<-EOF
authorityKeyIdentifier=keyid,issuer
basicConstraints=CA:FALSE
keyUsage = digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment
extendedKeyUsage = serverAuth
subjectAltName = @alt_names

[alt_names]
DNS.1=nexus.mshxuyi.com
DNS.2=nexus.mshxuyi.com
DNS.3=nexus.mshxuyi.com
EOF

4、使用该 v3.ext 文件为您的 Nexus 主机生成证书

openssl x509 -req -sha512 -days 3650 \
-extfile v3.ext \
-CA ca.crt -CAkey ca.key -CAcreateserial \
-in nexus.mshxuyi.com.csr \
-out nexus.mshxuyi.com.crt

 三、配置 Docker 证书

1、转换 crt 为 cert,供Docker使用

# Docker守护程序将.crt文件解释为 CA 证书,并将 .cert 文件解释为客户端证书
openssl x509 -inform PEM -in nexus.mshxuyi.com.crt -out nexus.mshxuyi.com.cert

2、将服务器证书,密钥和CA文件复制到 Nexus 主机上的 Docker 证书文件夹中

mkdir -p /etc/docker/certs.d/nexus.mshxuyi.com/
cp nexus.mshxuyi.com.cert /etc/docker/certs.d/nexus.mshxuyi.com/
cp nexus.mshxuyi.com.key /etc/docker/certs.d/nexus.mshxuyi.com/
cp ca.crt /etc/docker/certs.d/nexus.mshxuyi.com

三、创建 Nginx 容器

1、新建 Dockerfile

# 创建 nginx 目录
mkdir -p /docker/nginx

vim Dockerfile

# 内容
FROM nginx:1.16-alpine

COPY ssl/nexus.mshxuyi.com.crt /etc/nginx/certs/nexus.mshxuyi.com.crt
COPY ssl/nexus.mshxuyi.com.key /etc/nginx/certs/nexus.mshxuyi.com.key

ENTRYPOINT nginx -g "daemon off;"

2、创建 nginx.conf

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;
    keepalive_timeout  65;
    #gzip  on;

    server {
        listen       443 ssl;
        server_name  nexus.mshxuyi.com;

        ssl_certificate      certs/nexus.mshxuyi.com.crt;
        ssl_certificate_key  certs/nexus.mshxuyi.com.key;
        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;
        ssl_protocols        TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers          EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5;
        ssl_prefer_server_ciphers on;

        location / {
            #root   /usr/share/nginx/html;
            #index  index.html index.htm;
            proxy_pass http://192.168.2.107:8082;
        }

    }

}

3、新建 ssl 目录,把生成的 2 个证书 放进来,如下面

[root@harbor nginx]# ll ssl
总用量 8
-rw-r--r-- 1 root root 2139 5月   8 23:33 nexus.mshxuyi.com.crt
-rw-r--r-- 1 root root 3247 5月   8 23:33 nexus.mshxuyi.com.key

4、新建 启动脚本

vim start.sh

#!/bin/bash

DOCKERNAME="nginx"

docker rm -f $DOCKERNAME
docker build -t $DOCKERNAME .

docker run --name $DOCKERNAME \
-p 443:443 \
-v /docker/$DOCKERNAME/nginx.conf:/etc/nginx/nginx.conf \
-v /etc/localtime:/etc/localtime \
-m 1G \
-d $DOCKERNAME \
--restart=always

5、整个目录如下

[root@harbor nginx]# ll /docker/nginx
总用量 12
-rw-r--r-- 1 root root  199 5月   8 23:36 Dockerfile
-rw-r--r-- 1 root root 1129 5月   9 00:59 nginx.conf
drwxr-xr-x 2 root root   64 5月   9 03:18 ssl
-rwxr-xr-x 1 root root  269 5月   9 00:14 start.sh

6、启动脚本

chmod +x start.sh && ./start.sh

7、查看成功

[root@harbor nginx]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                              NAMES
ee69cb8aa780        nginx               "/bin/sh -c 'nginx -…"   5 seconds ago       Up 4 seconds        80/tcp, 0.0.0.0:443->443/tcp       nginx
d732fd405d71        sonatype/nexus3     "sh -c ${SONATYPE_DI…"   6 days ago          Up 4 hours          0.0.0.0:8081-8082->8081-8082/tcp   nexus

8、本机 hosts 域名解析

vim /etc/hosts

# 内容
192.168.2.107 nexus.mshxuyi.com

8、Nexus 主机中 登陆 docker

[root@harbor nginx]# docker login -u admin -p admin123 nexus.mshxuyi.com
WARNING! Using --password via the CLI is insecure. Use --password-stdin.
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded

9、其它主机登陆,需要配置 hosts 解析,然后把证书拷贝到 /etc/docker/certs.d 目录,如下图

[root@jenkins ~]# ll /etc/docker/certs.d/nexus.mshxuyi.com
总用量 12
-rw-r--r-- 1 root root 2049 5月   8 02:19 ca.crt
-rw-r--r-- 1 root root 2139 5月   8 02:24 nexus.mshxuyi.com.cert
-rw-r--r-- 1 root root 3247 5月   8 02:19 nexus.mshxuyi.com.key

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值