Docker Community Forums

Share and learn in the Docker community.

Database connection error

Hello there,

I am quite new to Docker and I spent 3 days but still no luck on fixing this issue.
In short I am trying to spin up an environment of Magento 1.9 + mySQL + myphpadmin so I can restore my current legacy Magento project onto it.
Here is the image I use - https://hub.docker.com/r/alexcheng/magento
And I try to use docker-compose.yml for setting up the environment.
I keep changing my yml file by referencing different articles but when I connect to localhost for setting up DB connection, I keep getting error = Database connection.
I tried different combinations such as hostname = db or sqldb, port 3306 etc but still no luck.
For each time I respun up the environment, I completely removed the containers and volumes.

Here is the latest version of my yml

version: '3.0'

services:
  web:
    image: alexcheng/magento:1.9.3.8
    ports:
      - 80:80
    depends_on:
      - slqdb
    volumes:
      - magento:/project/magento/www/html
    environment:
      - MYSQL_HOST=slqdb:3306
      - MYSQL_DATABASE=db
      - MYSQL_USER=dba
      - MYSQL_PASSWORD=N5GfDp5wLsE3aK5q
    networks:
      - app-network

  slqdb:
    image: mysql:latest
    container_name: sqldb
    restart: always
    hostname: sqldb
    environment:
      MYSQL_DATABASE: db
      MYSQL_USER: dba
      MYSQL_PASSWORD: N5GfDp5wLsE3aK5q
      MYSQL_RANDOM_ROOT_PASSWORD: '1'
    volumes:
      - slqdb:/project/magento/mysql
    networks:
      - app-network

  phpmyadmin:
    image: phpmyadmin:latest
    restart: always
    ports:
      - 8081:80
    environment:
      PMA_HOST: slqdb
      UPLOAD_LIMIT: 1024M
    depends_on:
      - slqdb
    networks:
      - app-network

volumes:
  slqdb:
  magento:

networks:
  app-network:
    driver: bridge

Thanks so much.

Can you please give us the full error message your’re getting?

You may want to change this typo into sqldb instead of slqdb (in many occurences); this is just confusing.

Did you try without hostname: sqldb? Anything that needs to connect to the database from within the same Compose setup can just use the service name you defined in the same file. (Now being slqdb: for you, which you indeed referred to in PMA_HOST: slqdb with the same typo.)

(Also, please use the editing toolbar to format code such as the Compose file, for which whitespace matters. I now fixed the formatting for you.)

Arjan is right: docker-compose creates user defined networks, which provide a dns-based service discovery. Within a user defined network, each container is able to communicate with other containers of the same network using the service name and container port of the application in the target container.

Note: if you want to use db you either need to rename the service to db or switch the networks: declaration to the long syntax (which allows to declare an alias) and add the alias db to the network. After this is done, the alias will be available via service discovery as well.

version: '3.0'

services:
  web:
    image: alexcheng/magento:1.9.3.8
    ports:
      - 80:80
    depends_on:
      - sqldb
    volumes:
      - magento:/project/magento/www/html
    environment:
      - MYSQL_HOST=sqldb:3306
      - MYSQL_DATABASE=db
      - MYSQL_USER=dba
      - MYSQL_PASSWORD=N5GfDp5wLsE3aK5q
    networks:
      - app-network

  sqldb:
    image: mysql:5.7
    platform: linux/x86_64
    container_name: sqldb
    restart: always
    hostname: sqldb
    environment:
      MYSQL_DATABASE: db
      MYSQL_USER: dba
      MYSQL_PASSWORD: N5GfDp5wLsE3aK5q
      MYSQL_RANDOM_ROOT_PASSWORD: '1'
    volumes:
      - sqldb:/project/magento/mysql
    networks:
      - app-network

  phpmyadmin:
    image: phpmyadmin:latest
    restart: always
    ports:
      - 8081:80
    environment:
      PMA_HOST: pmahost
      UPLOAD_LIMIT: 1024M
    depends_on:
      - sqldb
    networks:
      - app-network

volumes:
  sqldb:
  magento:

networks:
  app-network:
    driver: bridge

Thanks both @avbentem @meyay , after fixing the typo, the DB connection is gone but getting the error as attached

I followed the default URL said in the author’s doc and not sure how to fix this.

I see localhost in your screenshot. That should read sqldb to make the Magento container access the MySQL container.

Aside, seeing the following, I wonder why Magento is even asking for it:

Also, when using phpMyAdmin I think the following is wrong too: MA_HOST: pmahost. I’d say, but did not validate, that phpMyAdmin wants to know the host of the database. Which should also refer to sqldb then.