I’ve noticed half of my posts on this blog have been about me fiddling with my blog. I like to tinker, and something like this blog which I actually have to maintain solo gives me an opportunity to try new technologies.

Like docker for example!

Docker

Docker’s something I’ve had some basic understanding about for a while. I’ve used it for testing, and I’ve got a vague idea how it works, but there were so many things to understand to get anything working. Just like a lot of new technologies, there’s a lot of plugins, tools and extensions to understand to figure out whats going on. Do I use Swarm or Kubernetes?

In the end, I ended up going with docker-compose, which leans more toward the simpler side. Since my setup is essentially just nginx and ghost, I went for the simplest solution!

So after a bit of googling and a lot of trial and error, I got it working.

First of all, I setup Docker with Puppet on a new host using the garehr-docker module:

include ::docker

docker::image { 'ghost': }
docker::image { 'nginx': }

This downloads the docker package. Right now it doesn’t support docker-compose yet, but there is a PR to do so.

After that it was some trial and error figuring out how to get my setup working.

ghost_nginx:
  restart: always
  build: ./ghostNginx/
  ports:
    - 80:80
    - 443:443
  links:
    - 'ghostblog:ghostblog'

ghostblog:
  container_name: ghostblog
  image: ghost
  restart: always
  ports:
    - 2368:2368
  env_file:
    - ./config.env
  volumes:
    - "./petemsGhost/themes:/usr/src/ghost/themes"
    - "./petemsGhost/apps:/usr/src/ghost/apps"
    - "./petemsGhost/images:/usr/src/ghost/images"
    - "./petemsGhost/data:/usr/src/ghost/data"
    - "./petemsGhost/config:/var/lib/ghost"

The magic bit is the link: when you link the containers, it adds an entry to /etc/hosts with the container name and IP. This means that any references to the container name get resolved properly.

$ docker exec -i -t petersouterblogcompose_ghost_nginx_1 bash
$ curl ghostblog:2368
Moved Permanently. Redirecting to https://petersouter.co.uk/

So in my nginx config, I changed all the references to 0.0.0.0:2368 to ghostblog:2368.

Right now I’m building that straight into the nginx container in the Dockerfile, copying the config and keys from my previous Puppet setup:

FROM nginx
# Delete defaults
RUN rm /etc/nginx/nginx.conf
RUN rm /etc/nginx/conf.d/default.conf
COPY nginx.conf /etc/nginx/nginx.conf
COPY sites-enabled/petersouter.co.uk.conf /etc/nginx/sites-enabled/petersouter.co.uk.conf
COPY conf.d/ghost_blog_petersouter.co.uk-upstream.conf /etc/nginx/conf.d/ghost_blog_petersouter.co.uk-upstream.conf
COPY petersouter.co.uk.crt /etc/nginx/petersouter.co.uk.crt
COPY petersouter.co.uk.key /etc/nginx/petersouter.co.uk.key

EXPOSE 80 443

CMD ["nginx", "-g", "daemon off;"]

Bish-bash-bosh, working ghost blog backed by nginx!

What’s really cool about this is upgrading ghost is just a matter of doing docker-compose rm -f to kill all the images, docker-compose build to rebuild them with the latest ghost and nginx, then docker-compose up -t to run the containers again.

Further reading

These links helped me understand what to do and helped me debug: