Introduction
Ghost is a powerful app for professional publishers to create, share, and grow a business around their content. It comes with modern tools to build a website, publish content, send newsletters & offer paid subscriptions to members. Ghost can be installed on your own server and customised to suit your needs.
MySQL is an open-source relational database management system. Its name is a combination of "My", the name of co-founder Michael Widenius's daughter My, and "SQL", the acronym for Structured Query Language. A relational database organizes data into one or more data tables in which data may be related to each other; these relations help structure the data.
The Docker Compose File
---
version: "3.8"
services:
mysqldb:
image: mysqldb:latest
container_name: mysqldb
hostname: mysqldb
restart: unless-stopped
env_file:
- .env
environment:
- TZ=${TZ}
- MYSQL_DATABASE=${MYSQL_DATABASE}
- MYSQL_USER=${MYSQL_USER}
- MYSQL_PASSWORD=${MYSQL_PASSWORD}
- MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
ports:
- 3306:3306
healthcheck:
test: ["CMD", "mysqladmin" ,"ping", "-h", "localhost"]
start_period: 30s
interval: 10s
timeout: 10s
retries: 5
volumes:
- /opt/ghost/database:/var/lib/mysql
- /etc/localtime:/etc/localtime:ro
networks:
- ghost_network
labels:
- com.centurylinklabs.watchtower.enable=true
ghost:
image: ghost:latest
container_name: ghost
hostname: ghost
restart: unless-stopped
env_file:
- .env
environment:
- TZ=${TZ}
- database__client=${database__client}
- database__connection__host=${database__connection__host}
- database__connection__database=${database__connection__database}
- database__connection__user=${database__connection__user}
- database__connection__password=${database__connection__password}
- url=${url}
ports:
- 2368:2368
volumes:
- /opt/ghost/config:/var/lib/ghost/content
- /etc/localtime:/etc/localtime:ro
depends_on:
mysqldb:
condition: service_healthy
networks:
- ghost_network
labels:
- com.centurylinklabs.watchtower.enable=true
networks:
ghost_network:
name: ghost_network
external: true
docker-compose.yml
The Docker Environment Variables File
TZ=Europe/Stockholm
MYSQL_DATABASE=db
MYSQL_USER=user
MYSQL_PASSWORD=password
MYSQL_ROOT_PASSWORD=secretpassword
database__client=mysql
database__connection__host=mysqldb
database__connection__database=db
database__connection__user=user
database__connection__password=password
url=https://mywebsite.com
.env
This Docker Compose file defines two services, the app is named “ghost” and the database is called “mysqldb”. The services are built from the latest Docker images. The container names ares set to “ghost” and “mysqldb” as well.
The “restart” section ensures that the container will always be restarted if it exits (unless manually stopped). This is useful for ensuring that the services are always available, especially if they crash or are shut down unexpectedly.
The “security_opt” section prevents your container processes from gaining additional privileges. This is an important security consideration.
The “ports” section for Ghost maps port 2368 on the host machine to port 2368 in the container. This allows us to access the Ghost web interface from our local machine. The MySQLDB database maps to its standard port of 3306.
The “volumes” section maps the “/var/lib/ghost/content” and “/var/lib/mysql” directories inside the containers to local directories on the host machine. This allows us to persist data across container restarts. Ghost stores all its configuration data in its own directory and MySQLDB stores its database in its own directory, so it’s important to keep it intact.
The "labels" section allows the Watchtower service to keep this container update automatically.
Running Ghost with Docker Compose
To run Ghost with Docker Compose, first, make sure you have Docker and Docker Compose installed on your machine. Then, create a new directory for your project and save the above Docker Compose file as “docker-compose.yml” in that directory along with the environment variables as “.env”.
Next, run the following command from the same directory:
docker compose up -d
This command will start the Ghost container in the background and detach from it. You can then access the Ghost web interface by going to http://localhost:2368 in your web browser.
Conclusion
In this article, we explored how to set up and run the Ghost software using Docker Compose. We looked at the different sections of the Docker Compose file and explained how they work together to create a functional Ghost service. By running Ghost with Docker Compose, you can easily deploy and manage the tool on your own server, without having to worry about dependencies or configuration.
