How to Run Ghost Blog on Your Own Server with Docker

How to Run Ghost Blog on Your Own Server with Docker

If you’re looking for a lightweight and customizable blogging platform, Ghost is a great choice. It’s built for professional bloggers and journalists who need a fast, modern platform for publishing. In this article, I'll guide you through running Ghost on your own server using Docker and Docker Compose.

Prerequisites:

Before we begin, ensure you have the following:

  • A server with Docker and Docker Compose installed.
  • Basic knowledge of using the terminal.
  • A MySQL database or the ability to run MySQL using Docker.

Step 1: Create a Docker Compose File

We will use a Docker Compose file to define the services required for running Ghost and MySQL. Below is a sample docker-compose.yml file you can use.

version: '3.8'

services:
  ghost:
    image: ghost:latest
    restart: always
    ports:
      - "80:2368"
    depends_on:
      - db
    volumes:
      - /path/to/docker/ghost/content:/var/lib/ghost/content
    environment:
      database__client: mysql
      database__connection__host: db
      database__connection__user: ghostuser
      database__connection__password: mystrongpassword
      database__connection__database: ghostdb

  db:
    image: mysql:latest
    restart: always
    ports:
      - "3306:3306"
    volumes:
      - /path/to/docker/ghost/mysql:/var/lib/mysql
    environment:
      - MYSQL_ROOT_PASSWORD=mystrongpassword
      - MYSQL_DATABASE=ghostdb
      - MYSQL_USER=ghostuser
      - MYSQL_PASSWORD=mystrongpassword

Explanation:

  • Ghost Service: The Ghost service uses the latest Ghost image from Docker Hub. It exposes port 2368 (the default Ghost port) and maps it to port 80 on the host machine, making it accessible via HTTP.
  • MySQL Database: Ghost uses MySQL as its database backend. The MySQL container is configured with environment variables for the root password, the database name (ghostdb), the user (ghostuser), and the user password.

Replace the placeholder /path/to/docker/ghost/... with the actual path on your server where you want to store the Ghost content and MySQL data.

Step 2: Setting Up Volumes

Volumes in Docker allow you to persist your data even if the container stops or is deleted. In this setup:

  • The Ghost content is saved at /var/lib/ghost/content inside the Ghost container, and it's mapped to a folder on your host machine.
  • MySQL data is saved at /var/lib/mysql inside the MySQL container, and similarly, it's mapped to a local directory.

Step 3: Running the Ghost Blog

Once your docker-compose.yml file is ready, navigate to the directory where it’s located and run the following command:

docker-compose up -d

This will start both the Ghost and MySQL services in the background. The -d flag ensures that the containers run as daemons.

Step 4: Access Your Blog

After the containers are up and running, you can access your Ghost blog by visiting http://your-server-ip in a web browser. You’ll be guided through the initial setup, including setting up your admin user and configuring the blog.

Step 5: Managing Your Containers

To view the status of your containers, you can use:

docker ps

To stop the containers, run:

docker-compose down

Conclusion

Running Ghost with Docker allows for easy setup and maintenance, making it simple to get your blog up and running with minimal fuss. Using Docker Compose ensures you have both Ghost and its MySQL database running smoothly with just a few commands.

Feel free to adjust the docker-compose.yml file to your needs, such as enabling SSL with Let's Encrypt, changing ports, or adding additional services like Nginx as a reverse proxy.