Simple Way to Use Ghost via Docker

2 min read
Simple Way to Use Ghost via Docker

Recently, I tried to upgrade my self-hosted Ghost blog (https://ron.sh/) from version 3 to 4. It is installed in an Ubuntu machine but for some reason, ghost-cli is failing. I made several attempts but none of these are working for me.

Yesterday, I tried another approach - using Docker. With this approach, I am not reliant to ghost-cli anymore. The Docker approach worked for me and I believe this is more flexible and easily upgradable.

Here is my Compose file:

version: "3.8"

services:

  <site-name>:  # update site-name
    image: ghost:4.5.0-alpine
    restart: always
    ports:
      - "127.0.0.1:8085:2368"
    env_file:
      - env/<site-name>.env  # update site-name
    volumes:
      - ./.content/<site-name>:/var/lib/ghost/content  # update site-name

With this format I can extend and host multiple Ghost containers in one docker-compose.yml file. [I am currently hosting 2 Ghost blogs]

Also, I can run my Ghost server locally with the env file below. mail* is optional but you will need this in production to send "secure" emails. In production, replace url with the correct URL.

url=http://localhost:8085
mail__transport=<mail-transport>
mail__options__service=<mail-service>
mail__options__host=<mail-host>
mail__options__port=<mail-port>
mail__options__auth__user=<mail-user>
mail__options__auth__pass=<mail-password>

The last step in deploying to production is making sure that you have a SSL certificate. I used nginx to route my traffic and Let's Encrypt for the free SSL certificate. You can generate a certificate using certbot using the following command.

certbot --nginx -d <domain>

Next, edit the generated nginx configuration in (/etc/nginx/sites-available/default) by changing the location / block and adding client_max_body_size 50m; (this is for uploading large images).

	location / {
		proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
		proxy_set_header X-Forwarded-Proto $scheme;
		proxy_set_header X-Real-IP $remote_addr;
		proxy_set_header Host $http_host;
		proxy_pass http://127.0.0.1:8085;
	}

    client_max_body_size 50m;

If you are using another host and port, just replace proxy_pass http://<host>:<port> with correct values.

If all are set properly, just run docker-compose up -d. Enjoy!

Required files when migrating

  1. Export all contents from your Ghost admin page (including redirects and routes). Make sure to rename routes.yml to routes.yaml before importing it to the new server as Ghost 4 does not accept .yml file.
  2. Copy the image folder from the content directory. You can't export images from from the admin page 😢. Just put the image folder in the generated content directory .content/<site-name> (whatever you put in the Compose file). For some images to reflect properly, you need to restart the container/s with docker-compose down && docker-compose up -d.

Final Thoughts

  • Using the Docker approach is much simpler and more portable.
  • You can host multiple Ghost servers easily with a single Compose file.
  • No need to create a separate Linux non-root user as required by ghost-cli.

Read more articles like this in the future by buying me a coffee!

Buy me a coffeeBuy me a coffee