neděle 4. listopadu 2018

Nginx Proxy With lets encrypt using Docker compose

I invested some work in managing my little hosted server more efficiently.
I use docker to run my services for a long time now. My budget for hosted machine is too small to start even single node Kubernetes which is my preferred way to manage containers. I ended up in using docker compose.

There is a little drawback in using compose. Every compose file creates it's own network by default and for my scenario, when I use the single server to host multiple domains through nginx proxy container it did not work as expected.

When multiple compose configurations are started, each has its own config and by default each runs in separate network.

Here is a simple trick which will do it.

 Create dedicated network prior starting it all and connect all the compose configurations to this network by default instead letting the compose create a network per configuration.

I start my reverse proxy containers using following compose:

version: '2'

services:

  nginx-proxy:
    image: jwilder/nginx-proxy
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - "nginx-proxy-vhosts:/etc/nginx/vhost.d"
      - "nginx-proxy-html:/usr/share/nginx/html"
      - "/var/run/docker.sock:/tmp/docker.sock:ro"
      - "nginx-proxy-certs:/etc/nginx/certs"

  letsencrypt-nginx-proxy-companion:
    image: jrcs/letsencrypt-nginx-proxy-companion
    volumes:
      - "/var/run/docker.sock:/var/run/docker.sock:ro"
    volumes_from:
      - "nginx-proxy"

volumes:
  nginx-proxy-vhosts:
  nginx-proxy-html:
  nginx-proxy-certs:
 

networks:
   default:
     external:
        name: proxy      


The last lines starting with networks will do it.

If you want to expose the service through this proxy, add the same section at the end of compose file. Eg:

version: '2'

services:
  some.service.name:
    image: php:apache
    environment:
      VIRTUAL_HOST: your.own.domain
      LETSENCRYPT_HOST:
your.own.domain
      LETSENCRYPT_EMAIL: your.own@email.xxx
    volumes:
      - "./html:/var/www/html"

networks:
  default:
    external:
      name: proxy





And that is all ... for today