Docker install/setup - Invalid database host error

The yaml produces the same error for me:

[2024-07-24 09:07:32] ERROR Invalid database host.
Invalid database host.
"Please double check your database config."

Are you using the free CE version of Portainer? What version of Portainer exactly?

Can you be more specific with the database and database connection environment variable examples? Instead of using “example” for everything can you use more specific examples. It’s possible I’m not understanding the mysql environment variables vs the ghost database connection environment variables. Thanks for helping out.

The error is in your configuration, not in the mysql image, yes.

In my case – not exposing the MySQL container to the internet, but only using it within the Docker stack – the database host would be simply mysql, referring to the mysql container.

Copied straight out of a running test Portainer stack:

version: '3.8'

services:
  mysql:
    image: mysql:8.0
    environment:
      MYSQL_ROOT_PASSWORD: ghost
      MYSQL_DATABASE: ghost
      MYSQL_USER: ghost
      MYSQL_PASSWORD: ghost
    volumes:
      - mysql_data:/var/lib/mysql
    healthcheck:
      test: ["CMD", "mysqladmin", "ping", "-h", "localhost"]
      interval: 30s
      timeout: 10s
      retries: 5
      start_period: 10s

  ghost:
    image: ghost:latest
    ports:
      - 12345:2368
    environment:
      url: ${url}
      database__client: mysql
      database__connection__host: mysql
      database__connection__user: ghost
      database__connection__password: ghost
      database__connection__database: ghost
    volumes:
      - ghost_data:/var/lib/ghost/content
    depends_on:
      - mysql

volumes:
  ghost_data:
  mysql_data:

Note: in production environments, choose better passwords (even if you don’t expose the database). For this to run, you will also need to define the url environment variable in Portainer, including https.

Okay it is probably my configuration, in your example you have the same value “ghost” set for the following env variables:

MYSQL_ROOT_PASSWORD
MYSQL_DATABASE
MYSQL_USER
MYSQL_PASSWORD
database__connection__user
database__connection__password
database__connection__database

Does that mean in your production you have the same exact real value set for all of these vars?

This is why I asked if you could be more specific with your vars. For example using the same value where you only would use the same value somewhere else in the config and not just the same example value “ghost” for all the vars.

version: '3.8'

services:
  mysql:
    image: mysql:8.0
    environment:
      MYSQL_ROOT_PASSWORD: ghost
      MYSQL_DATABASE: ghost
      MYSQL_USER: ghost
      MYSQL_PASSWORD: ghost
    volumes:
      - mysql_data:/var/lib/mysql
    healthcheck:
      test: ["CMD", "mysqladmin", "ping", "-h", "localhost"]
      interval: 30s
      timeout: 10s
      retries: 5
      start_period: 10s

  ghost:
    image: ghost:latest
    ports:
      - 12345:2368
    environment:
      url: ${url}
      database__client: mysql
      database__connection__host: mysql
      database__connection__user: ghost
      database__connection__password: ghost
      database__connection__database: ghost
    volumes:
      - ghost_data:/var/lib/ghost/content
    depends_on:
      - mysql

volumes:
  ghost_data:
  mysql_data:

I copied and pasted this into Portainer, changed the URL to a correct value and I got the same error: ERROR Invalid database host.

Please provide more specific examples of your env vars if you’re not really just using “ghost” for your working test. If you are simply just using “ghost” for everything, it didn’t work for me in Portainer CE 2.20.3.

Would I have this in a production environment? Absolutely not. But for this test, it is running with these values, yes.

  • database__client must be set to mysql
  • database__connection__host should reference the container name of your database container
  • MYSQL_DATABASE and database__connection__database need to be the same values.
  • MYSQL_USER and database__connection__user need to be the same values.
  • MYSQL_PASSWORD and database__connection__password need to be the same values.

But your problem is still one step before that: your Ghost container can’t find your database container. That’s what ERROR Invalid database host is telling you.

To me, this sounds like a classic docker networking problem, though “out of the box” Portainer should take care of this for you.

I’d suggest you enter your Ghost container and try to ping mysql. For me, that worked without issue:

On my docker host I ran this:

sudo docker exec -it test_ghost bash

(the name of the container might differ for you, so look this up in portainer first)

Inside the container:

apt update
apt install -y iputils-ping
ping mysql

(The docker image doesn’t have these utilities installed to keep the size small.)

The output of the last command resulted in something like this:

PING mysql (10.0.111.2) 56(84) bytes of data.
64 bytes from 10.0.111.2 (10.0.111.2): icmp_seq=1 ttl=64 time=0.102 ms
64 bytes from 10.0.111.2 (10.0.111.2): icmp_seq=2 ttl=64 time=0.117 ms
64 bytes from 10.0.111.2 (10.0.111.2): icmp_seq=3 ttl=64 time=0.108 ms
^C
--- mysql ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2004ms
rtt min/avg/max/mdev = 0.102/0.109/0.117/0.006 ms

This confirms that the Ghost container can indeed see the host mysql – now the question is…can yours?


Just for reference, I again copied the exact setup you played back here, pasted it into my Portainer, and it’s running:

I have several stacks running in Portainer without any issues and when I use mysql 5.7 it works fine but not with mysql 8. It is strange. I’ll ping it to see what happens but if it doesn’t ping then what though? I need to reconfigure my Portainer network, can’t do that because I have other stacks running.

I added a healthcheck to the db and it won’t deploy, states it is unhealthy:

    healthcheck:
      test: ["CMD-SHELL", "mysqladmin ping -h localhost -u $$MYSQL_USER --password=$$MYSQL_PASSWORD && mysql -u $$MYSQL_USER --password=$$MYSQL_PASSWORD -e 'SHOW DATABASES LIKE \"$$MYSQL_DATABASE\";'"]
      interval: 30s
      timeout: 10s
      retries: 5

and I see you have a ping check built in:

    healthcheck:
      test: ["CMD", "mysqladmin", "ping", "-h", "localhost"]
      interval: 30s
      timeout: 10s
      retries: 5
      start_period: 10s

With the ping check it deploys with the error, with the healthcheck I stated it doesn’t even deploy the stack.

I got it working.

I’m running a Portainer VM on Proxmox and I needed to change my CPU type to host because mysql 8 gives an error if not:

Fatal glibc error: CPU does not support x86-64-v2

Then the database created itself but the ghost container would not start for about 10 minutes because I read it can take time for the database to build itself so be patient.

Everything is working now with mysql 8 and the latest ghost. If anyone is using my same setup and you’re having the same issues this will solve it.

Big thanks to @jannis you’re awesome. Thank you for being a community supporter!