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

# This is for if script called on reboot. 
# Ensures docker daemon is ready
sleep 10

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

docker pull dockerfile/ghost

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:/ghost-override \
--name $CONTAINER_NAME \
dockerfile/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  

Add The Config

Copy and paste the configuration below into $HOME/ghost_volume/config.js, making sure to change example.domain.com to whatever your blog's URL is.

// # Ghost Configuration
// Setup your Ghost install for various environments
// Documentation can be found at http://support.ghost.org/config/

var path = require('path'),  
    config;

config = {  
    // ### Production
    // When running Ghost in the wild, use the production environment
    // Configure your URL and mail settings here
    production: {
        url: 'http://example.domain.com',
        mail: {},
        database: {
            client: 'sqlite3',
            connection: {
                filename: path.join(__dirname, '/content/data/ghost.db')
            },
            debug: false
        },

        server: {
            // Host to be passed to node's `net.Server#listen()`
            host: '0.0.0.0',
            // Port to be passed to node's `net.Server#listen()`, for iisnode set this to `process.env.PORT`
            port: '2368'
        }
    },

    // ### Development **(default)**
    development: {
        // The url to use when providing links to the site, E.g. in RSS and email.
        // Change this to your Ghost blogs published URL.
        url: 'http://localhost:2368',

        // Example mail config
        // Visit http://support.ghost.org/mail for instructions
        // ```
        //  mail: {
        //      transport: 'SMTP',
        //      options: {
        //          service: 'Mailgun',
        //          auth: {
        //              user: '', // mailgun username
        //              pass: ''  // mailgun password
        //          }
        //      }
        //  },
        // ```

        database: {
            client: 'sqlite3',
            connection: {
                filename: path.join(__dirname, '/content/data/ghost-dev.db')
            },
            debug: false
        },
        server: {
            // Host to be passed to node's `net.Server#listen()`
            host: '0.0.0.0',
            // Port to be passed to node's `net.Server#listen()`, for iisnode set this to `process.env.PORT`
            port: '2368'
        },
        paths: {
            contentPath: path.join(__dirname, '/content/')
        }
    },

    // **Developers only need to edit below here**

    // ### Testing
    // Used when developing Ghost to run tests and check the health of Ghost
    // Uses a different port number
    testing: {
        url: 'http://0.0.0.0:2369',
        database: {
            client: 'sqlite3',
            connection: {
                filename: path.join(__dirname, '/content/data/ghost-test.db')
            }
        },
        server: {
            host: '0.0.0.0',
            port: '2369'
        },
        logging: false
    },

    // ### Testing MySQL
    // Used by Travis - Automated testing run through GitHub
    'testing-mysql': {
        url: 'http://0.0.0.0:2369',
        database: {
            client: 'mysql',
            connection: {
                host     : '0.0.0.0',
                user     : 'root',
                password : '',
                database : 'ghost_testing',
                charset  : 'utf8'
            }
        },
        server: {
            host: '0.0.0.0',
            port: '2369'
        },
        logging: false
    },

    // ### Testing pg
    // Used by Travis - Automated testing run through GitHub
    'testing-pg': {
        url: 'http://0.0.0.0:2369',
        database: {
            client: 'pg',
            connection: {
                host     : '0.0.0.0',
                user     : 'postgres',
                password : '',
                database : 'ghost_testing',
                charset  : 'utf8'
            }
        },
        server: {
            host: '0.0.0.0',
            port: '2369'
        },
        logging: false
    }
};

// Export config
module.exports = config;  

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