March 24, 2017

Ngnix and Docker

The layout of the web part of hacksaw is using Ngnix to load balance all traffic to app installed on Docker containers.

https://gist.github.com/soheilhy/8b94347ff8336d971ad0
https://www.ghostforbeginners.com/deploying-ghost-with-docker/

Nginx is installed on the host itself, and proxies traffic to different docker containers. For now, ghost is installed in docker, and ngnix proxies all /blog/ URLs to the ghost port in docker

    hacksaw.co.za
         |
         |
         v
     Ngninx:443
     |         |
     v         v
ghost:2368   anotherdocker:2369

This allows me to host many web apps as I like, quickly with docker, and I can remove or upgrade simply by updating or stopping the container. So gunk left laying around.

This is the ngnix config used:

  1. all port is 301 redirected to https port 443, which uses letsencrypt

  2. paths requested with /blog/ are proxied to the docker, listerning on 4431, which hosts ghost, listerning on port 2368

    ┌─[yusufm@ubuntu-512mb-lon1-01-hacksaw] - [~/gitwork/ghost-data] - [Fri
    Mar 24, 23:43]
    └─[$] <> cat /etc/nginx/sites-available/hacksaw

    server {
    listen 80 default_server;
    listen [::]:80 default_server;

         server_name hacksaw.co.za www.hacksaw.co.za;
         return 301 https://$server_name$request_uri;
         }
    

    server {

     # SSL configuration
    
     listen 443 ssl http2 default_server;
     listen [::]:443 ssl http2 default_server;
     include snippets/ssl-hacksaw.conf;
     include snippets/ssl-params.conf;
    
    
     root /var/www/html;
    
     # Add index.php to the list if you are using PHP
     index index.html index.htm index.nginx-debian.html;
    
     server_name hacksaw.co.za;
    
     location / {
             # First attempt to serve request as file, then
             # as directory, then fall back to displaying a 404.
             location ~ /.well-known {
                     allow all;
             }
    
     try_files $uri $uri/ =404;
     }
     ##Ghost Blog - /blog and /ghost
     location /blog/ {
             proxy_set_header   X-Real-IP $remote_addr;
             proxy_set_header   Host      $http_host;
             proxy_pass http://hacksaw.co.za:4431;
     }
    

the ghost docker is run as follows, which specifies:

  1. the port it listens on the host which gets forwarded to the a port in the container

  2. the folder on the host, to store the ghost data. This way, the docker container is disposable, and can bea easily replaced, as my data is stored on the host, in git.

    docker run -d -p 4431:2368 -v /home/yusufm/gitwork/ghost-data:/var/lib/ghost ghost
    2dbcd31ebe0794164225d9f2b4d17147b529cc4953474ef77d2d896b2e0c7fe8
    ┌─[yusufm@ubuntu-512mb-lon1-01-hacksaw] - [~/gitwork/ghost-data] - [Fri
    Mar 24, 23:05]
    └─[$] <> docker ps
    CONTAINER ID IMAGE COMMAND CREATED
    STATUS PORTS NAMES
    2dbcd31ebe07 ghost "/entrypoint.sh np..." 4
    seconds ago Up 2 seconds 0.0.0.0:4431->2368/tcp stupefied_archimedes