Search code examples
symfonydockeramazon-ecs

Symfony cannot connect to mysql DB on docker ECS container


I'm using amazon-ecs to launch docker containers that I have. Everything works fine locally, but when I'm running the containers on ECS I'm getting the following error:

"NOTICE: PHP message: Unable to open PDO connection [wrapped: SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known]"

I'm linking the containers in the docker-compose file, and I'm able to ping the the mysql container from the nginx container, so I know their linked.

docker-compose

version: '2'

services:
  nginx:
    image: "xxxx.dkr.ecr.us-east-2.amazonaws.com/nginx:latest"
    ports:
      - "8086:80"
    links:
      - fpm
      - mysql

  fpm:
    image: "xxxx.dkr.ecr.us-east-2.amazonaws.com/php-fpm:latest"
    links:
      - redis

  mysql:
    image: "xxxx.dkr.ecr.us-east-2.amazonaws.com/mysql:latest"
    environment:
      MYSQL_DATABASE: strix
      MYSQL_USER: strix
      MYSQL_PASSWORD: rRCd29b3fG76ypM3
      MYSQL_ROOT_PASSWORD: root
  redis:
    image: redis:latest

My symfony database.yml has the following:

dev:
  propel:
    param:
      classname: DebugPDO
      debug: { realmemoryusage: true, details: { time: { enabled: true }, slow: { enabled: true, threshold: 0.1 }, mem: { enabled: true }, mempeak: { enabled: true }, memdelta: { enabled: true } } }

task:
  propel:
    param:
      profiler: false

test:
  propel:
    param:
      classname: DebugPDO
all:
  propel:
    class: sfPropelDatabase
    param:
      classname: PropelPDO
      dsn: 'mysql:host=mysql;dbname=strix'
      username: strix
      password: xxxx
      encoding: utf8
      persistent: true
      pooling: true

I'm not sure if there's some network config that I have wrong on ECS or if I'm pointing to the wrong hostname. Any help would be appreciated. I am not familiar with symfony and am trying to raise an old application from the dead.


Solution

  • Turns out when I was running docker on my local machine, all the containers could talk to each other even though I hadn't explicitly linked them. In this case the fpm container needed to connect to the mysql container (and was doing so locally) but I didn't know this. When it was up on ECS, because it was not explicitly linked, it was throwing the connection error.

    I simply fixed it by adding mysql to the fpm links

    fpm:
      image: "xxxx.dkr.ecr.us-east-2.amazonaws.com/php-fpm:latest"
      links:
        - redis
        - mysql