Install Ghost with Docker
2 min read

Install Ghost with Docker

Today, we will learn how to start a new blog with Ghost on a VPS with docker-composee. The instructions are the same no matter the OS, that's the whol point of Docker.

Configuration de docker-compose pour Ghost

Create a docker-compose.yml file with the following content :

version: '2.3'

services:
    ghost:
        image: ghost:latest
        container_name: ghost
        hostname: ghost
        volumes:
        - ./config.production.json:/var/lib/ghost/config.production.json:z
        - ghost:/var/lib/ghost/content
        environment:
            NODE_ENV: production
            VIRTUAL_PROTO: http
            VIRTUAL_HOST: nicolaskempf.fr
            VIRTUAL_PORT: 80
            LETSENCRYPT_HOST: nicolaskempf.fr
            LETSENCRYPT_EMAIL: contact@nicolaskempf.fr
        restart: always
        networks:
        - nginx-proxy
        - ghost
        links:
        - mysql

    mysql:
        image: mysql:5.7
        container_name: mysql
        volumes:
            - db:/var/lib/mysql
        environment:
            MYSQL_ROOT_PASSWORD: ROOT_PASSWORD
            MYSQL_USER: nicolas
            MYSQL_PASSWORD: MY_PASSWORD
            MYSQL_DATABASE: ghost
        restart: always
        networks:
        - ghost
networks:
    nginx-proxy:
        external: true
    ghost:
        name: ghost

volumes:
    ghost: {}
    db: {}

I use nginx-proxy and letsencrypt-nginx-proxy-companion to make the link between my containers and my domain names

We can also make the blog work without proxy by changing a bit the properties :

version: '2.3'

services:
    ghost:
        image: ghost:latest
        container_name: ghost
        hostname: ghost
        volumes:
        - ./config.production.json:/var/lib/ghost/config.production.json:z
        - ghost:/var/lib/ghost/content
        environment:
            NODE_ENV: production
        restart: always
        ports:
        - "80:80"
        networks:
        - nginx-proxy
        - ghost
        links:
        - mysql

    mysql:
        image: mysql:5.7
        container_name: mysql
        volumes:
            - db:/var/lib/mysql
        environment:
            MYSQL_ROOT_PASSWORD: ROOT_PASSWORD
            MYSQL_USER: nicolas
            MYSQL_PASSWORD: MY_PASSWORD
            MYSQL_DATABASE: ghost
        restart: always
        networks:
        - ghost
networks:
    nginx-proxy:
        external: true
    ghost:
        name: ghost

volumes:
    ghost: {}
    db: {}

Setting up the Ghost container

For Ghost, we give a JSN config file with the database informations.

{
    "url": "https://nicolaskempf.fr",
    "server": {
        "port": 2368,
        "host": "0.0.0.0"
    },
    "database": {
        "client": "mysql",
        "connection": {
            "host": "mysql",
            "port": 3306,
            "user": "nicolas",
            "password": "MY_PASSWORD",
            "database": "ghost",
            "charset": "utf8mb4"
        }
    },
    "mail": {
        "from": "'Nicolas KEMPF' <contact@nicolaskempf.fr>", 
        "transport": "SMTP",
        "logger": true,
        "options": {
            "host": "",
            "secureConnection": false,
            "auth": {
                "user": "",
                "pass": ""
            }
        }
    },
    "logging": {
        "transports": [
            "file",
            "stdout"
        ]
    },
    "process": "systemd",
    "paths": {
        "contentPath": "/var/lib/ghost/content"
    }
}

If everything works well, Ghost launches and create the tables it needs in our database.

In MySQL, there is nothing to do, the environment variables are enough. It is possible you get errors while launching the services, the time mysql accepts the connexions. To solve this, wee will add an endpoint to Ghost to don't launch the container until it is connected to the db.

If you meet the following error, you will have to delet the db volume and start again :

ER_HOST_NOT_PRIVILEGED: Host ‘172.30.0.3’ is not allowed to connect to this MySQL server

Once restarted, your website can be reached at the selected url. You only have left to go on /ghost to create your admin account.

Vous aimez mes articles? Inscrivez-vous pour être alerté des nouveautés.