Dimi
Dimi

Reputation: 379

docker-compose: Why does my webapp fail to connect to mysql?

I am trying to connect two running containers: (1) a webapp and (2) a mysql db using docker compose. I setup the belowmentioned docker-compose.yml but my webapp fails to open a socket to mysql. More specifically, this php @fsockopen function throws an error ( second case ). Is there something wrong with the docker-compose config?

Note: idoit is the name of the webapp

php case

if (@fsockopen($l_dbHost, $l_t, $t_errno, $t_errstr, 5)) {
                        $l_success = true;
                        $l_message = "CONNECTED";
                        $l_dbPort = $l_t;
                    } else {
                        $l_success = false;
                        $l_message = "ERROR (" . $t_errno . ")";
                    }

The docker-compose.yml

version: "3"

services:
  db:
    image: mysql:5.7
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: ****
      MYSQL_DATABASE: ****
      MYSQL_USER:  ****
      MYSQL_PASSWORD:  ****
    ports:
      - '3306:3306'
    expose:
      - '3306'
    volumes:
      - db_data:/var/lib/mysql
  idoit:
    depends_on:
      - db
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - "8000:80"
    restart: always
    environment:
      IDOIT_DB_HOST: db:3306
      IDOIT_DB_USER:  ****
      IDOIT_DB_PASSWORD:  ****
      IDOIT_DB_NAME:  ****
      
volumes:
  db_data: {}

Upvotes: 1

Views: 148

Answers (1)

vodolaz095
vodolaz095

Reputation: 6986

seems like idoit service doesn't have access to network with mysql database.

db service have


networks:
      - backend

and idoit service - doesn't

Upvotes: 2

Related Questions