blog logo Running Ghost on Docker

I've been moving most of my services to Zeit. This blog is hosted with them and runs as a Docker container. Going from a traditional server to a container wasn't as easy as I thought. The progress is simple but the guidance wasn't fully there.

My goal with this post is to make the process of getting Ghost running on Docker as easy as 🎂!

Problem

The documentation I found helps you get ghost up and running in a testing environment. Once you get into production environment, your requirements will change. In testing, you use a local SQLite or mySQL database. In production, you should have an immutable container, which means it can not change. Writing a post will violate that. Every post you write will overwritten when your container restarts.

Solution

You need an external database. That was pretty obvious but which service should you use and how do you configure it?

I recommend Heroku's ClearDB add-on. They have a free tier and fair pricing.

Your configuration file should look like the code below. We will focus only on our production settings.

Config.js file for Ghost. Put the content of this into a file named config_w_heroku.js.

config = {  
  production: {
    url: 'https://misterGF.io',
    mail: {
      transport: 'SMTP',
      options: {
        host: 'email-smtp.us-east-1.amazonaws.com',
        port: 465,
        service: 'SES',
        auth: {
          user: '*********',
          pass: '*********************************'
        }
      }
    },
    database: {
      client: 'mysql',
      connection: {
        host: 'us-cdbr-iron-east-01.cleardb.net',
        user: '******',
        password: '**********',
        database: '*****************',
        charset: 'utf8'
      },
      debug: false
    },
    server: {
      host: '0.0.0.0',
      port: '2368'
    },
    fileStorage: false,
    paths: {
      contentPath: path.join(process.env.GHOST_CONTENT, '/')
    }
  },
  ...
}

DockerFile to build your blog. This will pull the official ghost image. Then it will customize the container based on your configuration. I've included the lines required to copy a custom theme into your container. They are commented out in below.

# Grab latest official ghost container
FROM ghost:latest

# Useful labels
MAINTAINER Gil Ferreira <hi@misterGF.io>  
LABEL name="misterGF.io"

# Setup environment variables
ENV GHOST_SOURCE /usr/src/ghost/  
ENV GHOST_VERSION 0.11.8  
ENV NODE_ENV production

# Remove config.js
WORKDIR /usr/src/ghost/  
RUN rm config.js

# add our own configure file
ADD config_w_heroku.js config.js


# Copy in our custom theme
# COPY gleak/ $GHOST_SOURCE/content/themes/gleak/

# QA - show file. Can be removed
# RUN ls -la $GHOST_SOURCE/content/themes/

If you are using Zeit, you can type in now from your working directory.

Your server is now deploy!!

🐋 +👻 = 🎂