docker How to Add Certificates

  • 2021-10-25 00:11:21
  • OfStack

1. Upgrade processing: sudo apt-get update

The problems such as missing package and old version of package can be solved. If not, it is missing authentication, and it is necessary to generate your own authentication certificate.

2. Generate your own certification

Create a folder first


mkdir -p certs

After that, the certificate is created, and the certificate is generated in the folder just created


openssl req -newkey rsa:4096 -nodes -sha256 -keyout /root/certs/domain.key -x509 -days 365 -out /root/certs/domain.crt

After that, put the certificate generated by certs in the/etc/docker/directory

Then restart the docker service sudo service docker restart

After Reboot

Supplement: Configure HTTPS certificates using nginx installed by Docker

Create a new ssl. conf and put the file in the conf. d folder


server {
  listen 443;
  server_name localhost;
  ssl on;
  root html;
  index index.html index.htm;
  ssl_certificate cert/1533224843981.pem;
  ssl_certificate_key cert/1533224843981.key;
  ssl_session_timeout 5m;
  ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
  ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  ssl_prefer_server_ciphers on;
  location / {
   root html;
   index index.html index.htm;
  }
 }

Note: cert is the relative path, if linux folder and nginx. conf, if window is under conf folder.

Run


 docker run --name mynginx -p 443:443 -v /opt/data/nginx/nginx.conf:/etc/nginx/nginx.conf
 -v /opt/data/nginx/conf.d:/etc/nginx/conf.d/default.conf 
-v /opt/data/nginx/www:/www -v /opt/data/nginx/cert:/etc/nginx/cert 
-v /opt/data/nginx/ssl.conf:/etc/nginx/conf.d/ssl.conf -d nginx

Related articles: