Nginx Docker Container with HTTPS protocol

Topic General

Many times you need to test a functionality on https website and you are searching the working image of docker container.

There are many images available in docker hub but you need to configure them accordingly.

However if you already working with very basic Nginx docker container, you might find this article useful which will help you to configure https on basic Nginx docker container.

So here we go ….

Let start with generating a single Self-Signed Certificate first.

These kind of certificates do not verify the identity of a server like commercially-signed certificates, so you will get the https prompt but without genuine certificate.

Open your terminal and type the command as below :

Once you fire the command it will ask for certain predefined inputs but the most important is :

Common Name (e.g. server FQDN or YOUR name). You need to enter the domain name associated with your server or your server’s public IP address.

Here you can see the command has different arguments, so let me brief them one by one :

openssl:  This is a command line tool for creating and managing OpenSSL certificates, keys, and other files.
req -x509: It specifies to use X.509 certificate signing request (CSR) management. The “X.509” is a public key infrastructure standard that SSL and TLS adheres to for its key and certificate management.
nodes: With this opetion openssl skip the option to secure our certificate with a passphrase. We just need Nginx to be able to read the file, without user intervention, when the server starts up. A passphrase become hurdle since it would need the passphrase after every restart.
days 365: This option will make the certificate generated valid for a full year
newkey rsa:2048: It specifies the openssl to make an RSA key that is 2048 bits long.
keyout: This line tells openssl where to place the generated private key file that we are creating.
out: This tells openssl where to place the certificate that we are creating.

Once you get the certificate and verified, proceed for next step.

Create a Nginx default.conf file in your local which will specify the certificate name and locations and turn on the ssl flag.

Now create a Dockerfile and point the certificates and default.conf.

I have used the basic nginx image from dockerhub. I have also created one html file to load over sample page.

Now lets run the docker file to build the container

Once the container is built you can start/run the container

You can verify the running docker container with ‘docker ps’

You can also try to check if there is any error with ‘docker logs <container-id>

If you need to enter into the container and use bash shell, you can use :

 

Now lets browse the website on Port 80 (we have redirected it to Port 8123 as my machine’s port is already in use)

We have not used the verified certificate and that’s why its showing certificate error – You can get the certified one from your Certificate Authority or used Verisign one to avoid these errors, but since this is just for our testing purpose I have used the basic one.

You can verify the certificate details through the browser by clicking on https symbol.

Now browse the website on Port 443 (we have redirected it to Port 8124 as my machine’s port 443 is already in use)

Let me go to my sample html page on https

That’s it – You have successfully tested the SSL enabled Ngnix Docker Container.

Happy learning !!

3 comments… add one

  • Vijay K

    Excellent article.

  • Tony Cesar

    Perfect for me, thanks.

  • Alina

    Great post!

Leave a Comment