Host Ghost on Docker

Introduction: Host Ghos Blog on Docker! ๐Ÿš€

Looking to self-host your Ghost blog using Docker containers? Your search ends here! We're diving into the world of Docker Compose to guide you in effortlessly hosting Ghost with a sprinkle of enthusiasm. Buckle up; it's simpler than you think!

Step 1: Docker Compose File

Here's a version 3.1 Docker Compose file. It consists of Ghost with an Alpine image and a MySQL container:

version: '3.1'

services:

  ghost:
    image: ghost:5-alpine
    user: node
    restart: always
    ports:
      - "2368:2368"
    environment:
      database__client: mysql
      database__connection__host: db
      database__connection__user: root
      database__connection__password: MYNEWPASSWORD
      database__connection__database: ghost
      url: https://codetalks.net
    volumes:
      - ghost-content:/var/lib/ghost/content
      - ./wait-for-it.sh:/bin/wait-for-it.sh
    command: ["/bin/sh", "-c", "/bin/wait-for-it.sh db:3306 -- /usr/local/bin/docker-entrypoint.sh node current/index.js"]
    depends_on:
      - db

  db:
    image: mysql:8.0
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: MYNEWPASSWORD
    volumes:
      - ghost-db:/var/lib/mysql


volumes:
  ghost-content:
  ghost-db:
docker-compose.yml

Docker Volumes Note:

Docker volumes act as independent storage units, allowing data to survive even if containers are stopped or deleted.

Caution! โš ๏ธ

If you don't use volumes, use mounts! If you don't use either YOU WILL ACCIDENTALLY DELETE YOUR ENTIRE BLOG. Be careful and make sure you use volumes or mounts!!!

The Magic of "wait-for-it":

While "depends_on" means that ghost waits for db to start, that doesn't mean the MySQL DB is ready. depends_on. wait-for-it is a script that checks that something is available before running something else. Here it makes sure the db is actually ready before starting Ghost. Download wait-for-it here:

wait-for-it/wait-for-it.sh at master ยท vishnubob/wait-for-it
Pure bash script to test and wait on the availability of a TCP host and port - vishnubob/wait-for-it

Step 2: Run the Commands

Here comes the magic! ๐ŸŽฉ Execute these simple commands:

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

Caution! โš ๏ธ

Ensure backups and understand the effects of changing versions or configurations. It's crucial, trust me!

Step 3: Enjoy Your Ghost Blog

Head over to your localhost:2368, and there's your Ghost blog, up and running smoothly. To make your own posts, go to localhost:2368/ghost

Conclusion

We've taken a technical yet friendly ride through hosting Ghost using Docker. Have questions? Encountered an issue? I've been there! Drop a comment, and let's solve it together.

Happy hosting! ๐ŸŽ‰