before posting my issue, I would like to know if it is even possible to achieve what I want.
I have, lets say, myserver.com running a docker container with nginx & letsencrypt. On the same server are 2 more docker containers running websites.
For now all is redirected fine, so www.myserver.com goes to docker 1 and site2.myserver.com goes to docker 2.
I would like to have all communication running over HTTPS, but here starts the trouble. So, my question is: is it possible for the docker with nginx and letsencrypt to connect to another docker using the certificates from letsencrypt? To me it seems to be some kind of man-in-the-middle "attack". A bit more schematic:
Browse to http:// site2.myserver.com -> nginx redirects to https:// site2.myserver.com -> connect to container 2 (192.168.0.10) on port 80. Or another option: Browse to http:// site2.myserver.com -> nginx redirects to https:// site2.myserver.com -> connect to container 2 (192.168.0.10) on port 443 having the site2.myserver.com certificates.
If it can't be done, what is the solution then? Copying the certificates to the docker containers and make them run https, so that a http request gets redirected to the https port of that container?
Browse to http:// site2.myserver.com -> nginx forwards request -> connect to container 2 (192.168.0.10) on port 443 having the site2.myserver.com certificates.
Thanks, Greggy
As I understand it your nginx reverse proxy is on the same network as the containers, so there is not much need to secure the connection between them with TLS (as this is a private network and if an attacker has access to that network he would have access to the server, too, and all the unencrypted data).
If you absolutely want valid certificates to secure the connections on your local network you could create additional sub-domains that resolve to the local IPs. Then you will need to use the manual DNS option to get your certificate (this is a certbot option where you need to manually enter a key as a TXT entry for your domain).
Example Nginx configuration to redirect http to https
Here is my way to do that:
NGINX Config file (default.conf)
Using the docker image from https://github.com/KyleAMathews/docker-nginx, I did the custom default file as follows:
Dockerfile
Here is my Dockerfile, considering that my static content is under html/ directory.
Testing
For local test, change the file /etc/hosts by adding myhost.com to 127.0.0.1 and run the following command:
curl -I http://www.myhost.com/
Result
Good, I could finally get what I wanted by merging the answers of opHASnoNAME and Paul Trehiou. What I did as an extra for opHASnoNAME's answer is to mount a filesystem between the nginx and the letsencrypt docker. It makes it possible to link the config files of nginx to the right certificates (see later).
This is what I did:
Then run whatever container with a webserver; no need to set the LETSENCRYPT variables. My current containers can be reached without setting them.
The jwilder/nginx-proxy will list up all the running containers in /etc/nginx/conf.d/default.conf. Don't add anything into this file because it will be overwritten on the next restart. Create for each webserver a new .conf file in the same directory. This file will contain the https information as suggested by Paul Trehiou. I created for example site2.conf:
The proxy_pass address is something you can take from the default.conf file, the IP addresses are listed there for each container. To be able to back up those .conf files I will recreate my nginx container and mount a local filesystem to /etc/nginx/conf.d. It will make life also easier if the container doesn't start up because of an error in the .conf files.
Thanks a lot everybody for your input, the puzzle is complete now ;-)
I would go with the out of the box solution:
JWilder Nginx + Lets Encrypt.
First we start NGINX Container as Reverse Proxy:
Next we start the Lets Encrypt Container:
For your Websites you need some Environment variables to be set:
The Nginx container will create a new entry in his config, and the lets encrypt container will request a certificate (and does the renew stuff).
More: Nginx+LetsEncrypt