Ghost mail environment variables on Docker

I’ve been working with Docker lately and decided to test out the Ghost blogging platform. However when setting up Ghost I ran into an issue with the official Docker image making it difficult to configure mail sending.

As Ghost explains in their documentation:

You’re able to start Ghost using environment variables which match the name and case of each config option

But there aren’t many examples online! This can make things pretty difficult, especially because of quirks like this:

Env vars should [all] be lowercase

You will need to setup 5 environment variables for emails to work in Ghost. Using an env file is the best option, but to better explain it we will use the docker -e flag.

docker run -d \
-p 3001:2368 \
-v /srv/ghost/content:/var/lib/ghost/content \
-e mail__transport="SMTP" \
-e mail__from="Sample <sample@example.com>" \
-e mail__options__service="SMTP" \
-e mail__options__host="smtp.sendgrid.net" \
-e mail__options__port="587" \
-e mail__options__auth__user="username" \
-e mail__options__auth__pass="password" \
ghost

In this Docker command you can see all of the environment variables required for mail as well as the volume configuration and port mapping.

(If you want to run this command, be sure to create the volume directory with mkdir -p /srv/ghost/content)

Ghost supports all of the services supported by Nodemailer (listed here) and you can read more about Ghost’s mail configuration here.

Env file

While specifying all of the environment variables while running the command works, a tidier way of doing this would be to use a env file.

You can read about using --env -file instead of -e in the Docker run reference

docker run -d \
-p 3001:2368 \
-v /srv/ghost/content:/var/lib/ghost/content \
--env-file /srv/ghost/env.list \
ghost

This command instead uses the file /srv/ghost/env.list to load any environment variables we would like.

You will then need to create a env.list file with at least these contents:

mail__transport="SMTP"
mail__from="Sample <sample@example.com>"
mail__options__service="SMTP"
mail__options__auth__user="username"
mail__options__auth__pass="password"

Final notes

You can now use the env file that we have created to define other settings for your Ghost Docker container like url or any of the other settings available. Just remember they are all lower case environment variables and to use two underscores (__) to replace the JSON tree.

Best of luck!

(Repost Note) Ref: https://blog.ryanhalliday.com/2018/05/ghost-docker-mail-environment-variables.html