Host multiple websites with SSL, Docker and Nginx

Image result for letsencrypt logoImage result for nginxImage result for docker logo





Using Docker to deploy multiple Nginx websites with SSL enabled





Today in this post I will guide on how to deploy multiple websites by using Docker, with SSL enabled.


Let's start!


1. Make a directory named `dockerize`, and go into the directory.

 mkdir ~/dockerize; cd ~/dockerize


2.  Now create a docker network named `nginx-proxy-network`.

 docker network create nginx-proxy-network


3. Inside `dockerize` directory, create a file named `docker-compose.yml`. This file is to automate our deployment.

 touch ~/dockerize/docker-compose.yml


4. Now edit `docker-compose.yml` with your favorite text editor. Paste the following text.

version: '3'

services:
  nginx:
    image: nginx:1.17.5
    container_name: nginx-proxy
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - conf:/etc/nginx/conf.d
      - vhost:/etc/nginx/vhost.d
      - html:/usr/share/nginx/html
      - certs:/etc/nginx/certs
    labels:
      - "com.github.jrcs.letsencrypt_nginx_proxy_companion.nginx_proxy=true"

  dockergen:
    image: jwilder/docker-gen:0.7.3
    container_name: nginx-proxy-gen
    depends_on:
      - nginx
    command: -notify-sighup nginx-proxy -watch -wait 5s:30s /etc/docker-gen/templates/nginx.tmpl /etc/nginx/conf.d/default.conf
    volumes:
      - conf:/etc/nginx/conf.d
      - vhost:/etc/nginx/vhost.d
      - html:/usr/share/nginx/html
      - certs:/etc/nginx/certs
      - /var/run/docker.sock:/tmp/docker.sock:ro
      - ./nginx.tmpl:/etc/docker-gen/templates/nginx.tmpl:ro

  letsencrypt:
    image: jrcs/letsencrypt-nginx-proxy-companion:v1.12
    container_name: nginx-proxy-le
    depends_on:
      - nginx
      - dockergen
    environment:
      NGINX_PROXY_CONTAINER: nginx-proxy
      NGINX_DOCKER_GEN_CONTAINER: nginx-proxy-gen
    volumes:
      - conf:/etc/nginx/conf.d
      - vhost:/etc/nginx/vhost.d
      - html:/usr/share/nginx/html
      - certs:/etc/nginx/certs
      - /var/run/docker.sock:/var/run/docker.sock:ro

volumes:
  conf:
  vhost:
  html:
  certs:

# Do not forget to 'docker network create nginx-proxy' before launch, and to add '--network nginx-proxy' to proxied containers. 

networks:
  default:
    external:
      name: nginx-proxy-network


5. Execute following to download nginx.tmpl

 curl https://raw.githubusercontent.com/jwilder/nginx-proxy/master/nginx.tmpl > ~/dockerize/nginx.tmpl



6. Now you can deploy the Nginx proxy.

 docker-compose up -d  


7. Run the following command to create directories, and create index files for each site.

 mkdir -p webpage/mainpage/html; mkdir -p webpage/another_page/html
 echo 'hello from mainpage' > mainpage/html/index.html
 echo 'hello from another_page' > another/html/index.html


8. Change directory into `webpage`.

 cd webpage


9. Create a `docker-compose` configuration file, to deploy virtual hosts automatically. In this part, you will need to change VIRTUAL_HOST, LETSENCRYPT_HOST, and LETSENCRYPT_EMAIL, based on your configuration.

version: '3'

services:
  mainpage:
    image: nginx:1.17.5
    expose:
      - 80
    environment:
      VIRTUAL_HOST: example.com
      LETSENCRYPT_HOST: example.com
      LETSENCRYPT_EMAIL: contact@example.com
    volumes:
      - ./mainpage/html:/usr/share/nginx/html

  another_page:
    image: nginx:1.17.5
    expose:
      - 80
    environment:
      VIRTUAL_HOST: another-page.example.com
      LETSENCRYPT_HOST: another-page.example.com
      LETSENCRYPT_EMAIL: contact@another-page.example.com
    volumes:
      - ./another_page/html:/usr/share/nginx/html

networks:
    default:
        external:
            name: nginx-proxy-network


10. Now you can deploy your website using docker-compose.

 docker-compose up -d



source: https://blog.ssdnodes.com/blog/host-multiple-ssl-websites-docker-nginx/

Comments