How to add self-signed certificate to docker nginx:alpine

This is my-compose.yml definition

nginx:
  image: "nginx:alpine"
  ports:
    - 5000:80
  links:
    - registry:registry
  volumes:
    - ./auth:/etc/nginx/conf.d
    - ./auth/nginx.conf:/etc/nginx/nginx.conf:ro

registry:
  image: registry:latest
  volumes:
    - ./registry-data:/var/lib/registry

I start docker as daemon on fly

sudo docker-compose -f my-compose.yml up -d

maybe I need to add my self-signed certificate to "nginx:alpine" docker, but how exactly?


Solution 1:

Depending on what sort of hoops your Docker containers need to jump through, you can accomplish this with a COPY command in your DockerFile:

COPY /path/to/certificate.pem /etc/ssl/certs/nginx-selfsigned.crt;
COPY /path/to/certificate-key.key /etc/ssl/private/nginx-selfsigned.key;

Some notes:

  • if your certificates are in .pem format, do not use any transformation processes; simply rename .pem to .crt
  • copy the renamed certificates to the proper certificate location for your nginx installation
  • ensure nginx knows where the files are expected to be:
    ssl_certificate /etc/ssl/certs/nginx-selfsigned.crt;
    ssl_certificate_key /etc/ssl/private/nginx-selfsigned.key;
    

This should do what you need it to do 👍🏻