Deploy Ghost With Docker

Ghost is a markdown blogging platform that allows one to easily post content that displays well on any screen size, such as a mobile phone.

Docker allows one to easily deploy applications such as Ghost without having to spend lots of time installing packages and reading installation instructions. This tutorial assumes you already have a docker host, or know how to install docker.

Deploying your own ghost blog is as easy as saving and executing the following BASH script on your server. I recommend calling it start-blog.sh and saving it in your $HOME directory.

#!/bin/bash
if ! [ -n "$BASH_VERSION" ];then
    echo "this is not bash, calling self with bash....";
    SCRIPT=$(readlink -f "$0")
    /bin/bash $SCRIPT
    exit;
fi

# Ensure that the docker daemon has had enough time to startup
sudo service docker start &
sleep 5


DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )


DATA_DIR="$DIR/ghost_volume";
CONTAINER_NAME="ghost_blog"

docker kill $CONTAINER_NAME
docker rm $CONTAINER_NAME

docker run \
-d \
-p 80:2368 \
-v $DATA_DIR:/var/lib/ghost \
--name $CONTAINER_NAME \
ghost

echo ""
echo "You may want to manually update ghost by running:"
echo "docker pull ghost"

Once run, you will have a folder called ghost_volume on the same directory level as the script. Inside that folder is all the data that the blog uses so that your blog will still be there if your server crashes or reboots.

If you ever find that your blog is not running, just manually execute the start-blog.sh script.

Reboot Cron

Unofortunately, it appears the deployed docker container won't start after a reboot, so running this script will solve that problem for you:

TMP_CRON_FILE="/tmp/cron_file"  
crontab -l > $TMP_CRON_FILE  
echo "@reboot /bin/bash /home/$USER/start-blog.sh" >> $TMP_CRON_FILE  
sudo crontab $TMP_CRON_FILE  
rm $TMP_CRON_FILE  

Edit The Config

Inside your ghost_volume there should be a config.js. You will need to update some of the variables, such as url: 'http://localhost:2368'

Register Yourself

Navigate to your blog's IP or hostname and you should see the following screen:
Now add /admin to the end of the URL and fill in the provided form to register yourself as the administrator, in order to make new posts.

Conclusion

You have now seen how to setup your own blog using Ghost and Docker and should be able to navigate the menus on your own to create new posts. Now you may be interested in finding out how to add comments to your blog.

Author

Programster

Stuart is a software developer with a passion for Linux and open source projects.

comments powered by Disqus