Running an Nginx Docker container with a self-signed SSL certificate involves a few steps. Here’s a step-by-step guide to help you set it up:
Create a Self-Signed SSL Certificate:
You can use the OpenSSL tool to generate a self-signed SSL certificate.
openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout nginx.key -out nginx.crt
This command will generate two files: nginx.key
(private key) and nginx.crt
(self-signed certificate).
Create a Dockerfile:
Create a file named Dockerfile
in your project directory with the following content:
FROM nginx:latest
# Copy the SSL certificate and key to the container
COPY nginx.crt /etc/nginx/ssl/nginx.crt
COPY nginx.key /etc/nginx/ssl/nginx.key
# Copy the Nginx configuration file
COPY nginx.conf /etc/nginx/conf.d/default.conf
Create Nginx Configuration:
Create an nginx.conf
file in the same directory as your Dockerfile with the following content:
server {
listen 443 ssl;
server_name your_domain.com; # Change this to your domain name
ssl_certificate /etc/nginx/ssl/nginx.crt;
ssl_certificate_key /etc/nginx/ssl/nginx.key;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
}
Replace your_domain.com
with your actual domain name.
Build the Docker Image:
Run the following command in your terminal to build the Docker image:
docker build -t nginx-ssl .
Run the Docker Container:
Finally, run the Docker container using the following command:
docker run -d -p 443:443 nginx-ssl
docker run --name some-nginx -v /some/content:/usr/share/nginx/html:ro -d nginx
docker run –name nginx-ssl -v //home/ubuntu/data:/usr/share/nginx/html:ro -p 443:443 -d nginx-ssl
This maps the container’s port 443 to the host’s port 443.
Access the Site:
Open a web browser and navigate to https://your_domain.com
. You might get a security warning due to the self-signed certificate, but you can proceed to the site. The browser will indicate that the connection is not fully secure because the certificate is self-signed.
Remember that using self-signed certificates is not recommended for production environments since they don’t provide the same level of security as trusted certificates issued by a certificate authority. In a production environment, you should obtain a valid SSL certificate from a trusted certificate authority like Let’s Encrypt.