Last updated 2 min read

Running Ghost Blog In a Docker Container. Installation and Migration

Related: Ghost Blog.


Installation

1. Create Docker Compose File

Create docker-compose.yml:

version: '3.1'

services:
  ghost:
    image: ghost:5.82.11
    restart: always
    ports:
	  - 127.0.0.1:3001:2368
    depends_on:
      - db
    links:
      - db
    environment:
      database__client: mysql
      database__connection__host: db
      database__connection__user: ${DB_USER}
      database__connection__password: ${DB_USER_PASSWORD}  
      database__connection__database: ${DB_NAME}
      url: ${GHOST_BLOG_URL}
      mail__transport: SMTP
      mail__options__service: Mailgun
      mail__options__host: smtp.mailgun.org
      mail__options__port: 587
      mail__options__secure: 'false'
      mail__options__auth__user: ${MAILGUN_USER}
      mail__options__auth__pass: ${MAILGUN_PASSWORD}
    volumes:
      - /var/lib/docker/volumes/${GHOST_DIR}/content:/var/lib/ghost/content
  db:
    image: mysql:8.0.31
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: ${DB_ROOT_PASSWORD}
      MYSQL_USER: ${DB_USER}
      MYSQL_PASSWORD: ${DB_USER_PASSWORD}
      MYSQL_DATABASE: ${DB_NAME}
    volumes:
      - /var/lib/docker/volumes/${GHOST_DIR}/mysql:/var/lib/mysql



Important to note. Ports and volumes might need a different config depending on your setup.

2. Prepare Environment Variables and Secrets

Create an .env file with all the the following env vars in the same dir as your docker compose file:

GHOST_BLOG_URL="https://youtr-fancy-blog-website.com"
GHOST_DIR="ghost-blogname"
DB_ROOT_PASSWORD="MYSQL_ROOT_PASSWORD"
DB_USER="ghost"
DN_USER_PASSWORD="GHOST_DB_PASSWORD"
DB_NAME="ghost_production"

MAILGUN_USER="user"
MAILGUN_PASSWORD="secret_password"

Alternatively, use another way to pass env vars to docker: [[How to pass env vars to Docker Container]]

3. Run

Run:

docker compose up -d

Migration

Migration is pretty straightforward.

1. Backup

Make a backup.

Take into account that JSON export in the Ghost Admin might not be enough because Ghost Blog JSON Export Does Not Export Comments. Than's why SQL dump is probably a better option: Backup and Restore Ghost MySQL DB Running with Docker

2. Update MySQL

If you are using MYSQL5 most likely that you will have to update it to MYSQL8 because MYSQL5 is no longer supported in the latest Ghost versions.

It can be done by updating the mysql image version in your docker-compose.yml .

Important to note. This migration is not that straightforward: MySQL 5.7 to 8.0 Upgrade Pitfalls

Also check the official docs for more info:

If you are already on MYSQL8 this update is optional.

3. Update Ghost To Latest Minor

Update the ghost image version to the latest of the same major you are on in your docker-compose.yml . For eg: 4.44-x to `4.48-x.

Restart container:

docker-compose down && docker-compose up -d

4. Update Ghost To Latest Minor

Update the ghost image version to the latest of the latest in your docker-compose.yml

Restart container again:

docker-compose down && docker-compose up -d

5. Profit!


References