I had a website running using HTTPS behind a load balancer, and didn’t want to bother setting up HTTP as well. Instead, I configured the load balancer to point to a very simple Nginx webserver that does nothing else than redirecting HTTP to HTTPS. So from the application side I only had to take care of HTTPS and could ignore additional configuration. As a nice side-effect, the Nginx redirection is generic so that I only need to run a single instance for all my applications.
Since I don’t need anything else than Nginx on the Docker image, I used Alpine Linux as a base and added Nginx, or more precisely the preconfigured Nginx alpine-stable docker image from https://hub.docker.com/_/nginx/. The Dockerfile looks like the following:
1 2 3 4 |
FROM nginx:alpine EXPOSE 80 COPY nginx.conf /etc/nginx/nginx.conf |
And the related nginx.conf file, which gets copied when the docker image is created like this:
1 2 3 4 5 6 7 8 9 10 11 |
events { } http { server { listen 80; location / { return 301 https://$host$request_uri; } } } |
Assuming the Dockerfile and nginx.conf are in the same directory, a simple docker build command creates the docker image which can be loaded into your docker host. Writing a simple script to include this step in your build automation should be fairly trivial, depending on your needs.