ghost connecting to mariadb gives connection error

1.3k Views Asked by At

I am trying to to get my ghost data to be saved on a mariadb docker container rather than ghosts inbuilt database. Its on docker-compose, where I have:

docker-compose.yml

version: '3'

services:
  mariadb-container:
    image: mymariadb:latest
    restart: always
    ports:
      - 3306:3306
    # volumes:
    #   - ~/blog/mariadb:/var/lib/mysql

  ghost-container:
    image: myghost:latest
    restart: always
    ports:
      - 2368:2368
    depends_on:
      - mariadb-container
    # volumes:
    #   - ~/blog/content:/var/lib/ghost/content
    links:
      - mariadb-container

myghost/Dockerfile

FROM ghost:latest

EXPOSE 2368

ADD ./config.production.json /var/lib/ghost

myghost/config.production.json

{
    "url": "http://localhost:2368/",
    "server": {
      "port": 2368,
      "host": "0.0.0.0"
    },
    "database": {
        "client": "mysql",
        "connection": {
          "host": "localhost",
          "port": 3306,
          "user": "root",
          "password": "pass123456",
          "database": "ghostblog",
          "socketPath": "/var/run/mysqld/mysqld.sock"
        }
    },
    "mail": {
      "transport": "Direct"
    },
    "logging": {
      "transports": [
        "file",
        "stdout"
      ]
    },
    "process": "systemd",
    "paths": {
      "contentPath": "/var/lib/ghost/content"
    }

mymariadb/Dockerfile

FROM mariadb:latest

EXPOSE 3306

ENV MYSQL_ROOT_PASSWORD=pass123436
ENV MYSQL_DATABASE=ghostblog

ERRORS::

But with this i get:

ghost-container_1    | NAME: RollbackError
ghost-container_1    | CODE: ENOENT
ghost-container_1    | MESSAGE: connect ENOENT /var/run/mysqld/mysqld.sock
ghost-container_1    |
ghost-container_1    | level:normal
ghost-container_1    |
ghost-container_1    | OuterError: The server has encountered an error.
ghost-container_1    | RollbackError: connect ENOENT /var/run/mysqld/mysqld.sock

Now, I included the socketPath in config.production.json because without it I got:

ghost-container_1    | NAME: RollbackError
ghost-container_1    | CODE: ECONNREFUSED
ghost-container_1    | MESSAGE: connect ECONNREFUSED 127.0.0.1:3306
ghost-container_1    |
ghost-container_1    | level:normal
ghost-container_1    |
ghost-container_1    | OuterError: The server has encountered an error.
ghost-container_1    | RollbackError: connect ECONNREFUSED 0.0.0.0:3306

I've also made sure the 'bind-address' is commented out in the configuration for mariadb, as this was the first things I saw when googling the error, I've also tried replacing localhost with 0.0.0.0 or 127.0.0.1 in the config.production.json and also in docker-compose.yml but with no change in error.

Also, I'm regulary doing the docker build --no-cache and docker-compose up --force-recreate to make sure the changes I made are actually being loaded.

Does anyone know what I am doing wrong?

2

There are 2 best solutions below

0
Taron Saribekyan On

Your database and ghost are in different containers, you can create network to allow them communicate with each other via network.

docker-compose.yml

version: '3'

services:
  mariadb:
    image: mymariadb:latest
    restart: always
    ports:
      - 3306:3306
    # volumes:
    #   - ~/blog/mariadb:/var/lib/mysql
    networks:
        - web-network

  ghost:
    image: myghost:latest
    restart: always
    ports:
      - 2368:2368
    depends_on:
      - mariadb-container
    # volumes:
    #   - ~/blog/content:/var/lib/ghost/content
    networks:
        - web-network

networks:
  web-network:
    driver: bridge

ghost config (database part only)

"database": {
    "client": "mysql",
    "connection": {
      "host": "mariadb", // your database container's name in network
      "port": 3306,
      "user": "root",
      "password": "pass123456",
      "database": "ghostblog"
    }
},
0
Luis Arteaga On

Maybe it helps you if you are looking on a running docker environment with nginx, letsencrypt, ghost and mariadb:

Sources

Important Note

Right now there is a docker bug when you trying to pull from ghost:latest. If you are pulling from ghost:1.22.1 - it works. (You have to change it in the docker-compose.yml)