Search code examples
mysqldockerdocker-composemura

Mura CMS database connectivity issue inside docker using docker-compose.yml file


I am trying to setup Mura CMS using Docker on my local machine. MYSQL database seems not to be working.

I am using the docker-compose up command. I have been trying the following example: https://github.com/blueriver/docker-muracms/tree/master/examples/blueriver-muracms-mysql

version: '3.3'

services:

  muracms:
    image: blueriver/muracms:latest
    depends_on:
      - svc_muradb
    environment:
      MURA_ADMIN_USERNAME: admin
      MURA_ADMIN_PASSWORD: 5trongP@55w0rd
      MURA_ADMINEMAIL: youremail@domain.com
      MURA_APPRELOADKEY: appreload
      MURA_DATASOURCE: dsn_muracms
      MURA_DATABASE: muradb
      MURA_DBTYPE: mysql
      MURA_DBUSERNAME: root
      MURA_DBPASSWORD: 5trongP@55w0rd 
      MURA_DBHOST: svc_muradb
      MURA_DBPORT: 3306
      MURA_SITEIDINURLS: "false"
      MURA_INDEXFILEINURLS: "true"
      MURA_TESTBOX: "true"
      MURA_USESSL: "false"
    volumes:
      - ./www/modules:/var/www/modules
      - ./www/plugins:/var/www/plugins
      - ./www/sites:/var/www/sites
      - ./www/themes:/var/www/themes
    ports:
      - "8888:8888"

  # DB
  svc_muradb:
    image: mysql:latest
    environment:
      MYSQL_DATABASE: muradb
      MYSQL_ROOT_PASSWORD: 5trongP@55w0rd
    volumes:
      - vol_muradb:/var/lib/mysql
    ports:
      - "5001:3306"

# Mounts
volumes:
  vol_muradb:

The following error message I see when I browse localhost:8888:

500 Error
Could not create connection to database server.
Code:0
Type:database
lucee.runtime.exp.DatabaseException: Could not create connection to database server.
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
    at com.mysql.jdbc.Util.getInstance(Util.java:408)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:918)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:897)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:886)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:860)

Solution

  • This is because when muracms start, the svc_muradb still not finish its init.

    The depends_on just assure svc_muradb start first, and then start muracms. But the start process svc_muradb is asynchronous. So before the svc_muradb ready, your muracms has probability already want to connect the db.

    This issue documented in official guide, which suggest you to write a wrapper for svc_muradb command.

    Like follows, I just give you an example:

    docker-compose.yml:

    version: "2"
    services:
      web:
        build: .
        ports:
          - "80:8000"
        depends_on:
          - "db"
        command: ["./wait-for-it.sh", "db:5432", "--", "python", "app.py"]
      db:
        image: postgres
    

    wait-for-it.sh:

    #!/bin/sh
    # wait-for-postgres.sh
    
    set -e
    
    host="$1"
    shift
    cmd="$@"
    
    until PGPASSWORD=$POSTGRES_PASSWORD psql -h "$host" -U "postgres" -c '\q'; do
      >&2 echo "Postgres is unavailable - sleeping"
      sleep 1
    done
    
    >&2 echo "Postgres is up - executing command"
    exec $cmd 
    

    With above workaround, the web container will run wait-for-it.sh, and first it will try to link its dependency container db:5432, only after it confirms can link to database with psql client, then it will run the real command python app.py.

    For your part, you need to change related to mysql client test, then everything is ok.