Setting up Ghost CMS in Docker with Nginx

Setting up Ghost CMS in Docker with Nginx
Photo by Patrick Fore / Unsplash

Introduction

This guide details how to setup the dockerized version of the Ghost CMS while using Nginx as a reverse proxy.

This guide is minimal, so it is recommended to see the Ghost documentation for further configuration.

Limitations

This guide assumes that Nginx, Docker and Docker Compose have been installed and setup already.
This guide does not explain how to setup Nginx safely, how to setup SSL or how to setup Docker or Docker Compose.

Docker

This guide is setting up Ghost with the image version 5.70.1-alpine. See available images for Ghost on Docker Hub.

See the following docker-compose.yml:

version: '3.1'

services:

  ghost:
    image: ghost:5.70.1-alpine
    restart: always
    ports:
      - 8085:2368
    environment:
      database__client: mysql
      database__connection__host: db
      database__connection__user: root
      database__connection__password: example
      database__connection__database: ghost
      url: https://some.host
  db:
    image: mysql:8.0
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: example

This docker-compose.yml contains several configuration steps that might need adjustement.

Database

With the docker-compose.yml shown above two containers will be setup for Docker. The container at the end of the file is called db (this name can be changed, but notice that it is being referred to in the variable database__connection__host) and creates a dockerized instance of MySQL.

It is necessary to create a safe password and set it for MYSQL_ROOT_PASSWORD as well as the referring variable database__connection__password

Ghost

With the docker-compose.yml shown above a container referred to as ghost will be created.

For this container the port 8085 will be mapped to the port 2368. The port 2368 is the port inside the container and can not be changed. The port 8085 is the port where Nginx is forwarding the requests to. The port 8085 might be adjusted to fit the local configuration.

An important variable here is the url variable as this is being used by the Ghost instance to reroute to itself. It is necessary to have this variable and configure it properly.

With the docker-compose.yml shown above Ghost will start in production mode.

Nginx

The Nginx configuration file has to be placed in /nginx/sites-available and needs to be enabled.

See the following Nginx configuration file:

server {
        server_name www.some.host some.host;


        location / {
                proxy_pass http://127.0.0.1:8085;
        }

        proxy_set_header    Host                $http_host;
        proxy_set_header    X-Real-IP           $remote_addr;
        proxy_set_header    X-Forwarded-For     $proxy_add_x_forwarded_for;
        proxy_set_header    X-Forwarded-Proto   $scheme;
}

See the proxy_pass which forwards to port 8085. This is the port configured in the docker-compose.yml for the Ghost container.

Notice the proxy_set_header-entries. These are necessary for Ghost to function.