Php: Cannot connect to mysql container from fpm-alpine container

Created on 26 Oct 2016  路  3Comments  路  Source: docker-library/php

I'm not able to connect to a db container with fpm-alpine image. Do I miss something or is it not possible with this image?
Bellow is my full configuration:

docker-compose.yml:

docker-mysql:
  image: mariadb:latest
  container_name: docker-mysql
  environment:
    - MYSQL_ROOT_PASSWORD=123123
    - MYSQL_DATABASE=test-db
    - MYSQL_USER=user
    - MYSQL_PASSWORD=pass

docker-php-fpm:
  build: .
  dockerfile: docker/php-fpm/Dockerfile
  container_name: neos-docker-php-fpm
  volumes:
    - ./:/var/www/app
  links:
    - docker-mysql

docker/php-fpm/Dockerfile:

FROM php:fpm-alpine

RUN apk add --update freetype-dev libpng-dev libjpeg-turbo-dev libxml2-dev autoconf g++ imagemagick-dev libtool make \
    && docker-php-ext-configure gd \
        --with-gd \
        --with-freetype-dir=/usr/include/ \
        --with-png-dir=/usr/include/ \
        --with-jpeg-dir=/usr/include/ \
    && docker-php-ext-install gd mbstring tokenizer pdo pdo_mysql imap opcache \
    && pecl install imagick \
    && docker-php-ext-enable imagick \
    && apk del autoconf g++ libtool make \
    && rm -rf /tmp/* /var/cache/apk/*

WORKDIR "/var/www/app"

When I try to ssh into container

~ docker exec -it docker-php-fpm /bin/ash

and test the connection to db I get

/var/www/neos-docker # php -r "new PDO('mysql:host=localhost;port=3306;charset=utf8', 'root', '123123');"

Fatal error: Uncaught PDOException: SQLSTATE[HY000] [2002] No such file or directory in Command line code:1
Stack trace:
#0 Command line code(1): PDO->__construct('mysql:host=loca...', 'root', '123123')
#1 {main}
  thrown in Command line code on line 1

Most helpful comment

My first guess is that it is dying trying to find the mysql unix socket file, when it needs to be connecting over a non-localhost connection. If you are using the compose file to start them, then you can connect to the dns name provided by docker (ie, links: docker-mysql, sets the dns name to be docker-mysql). So you have to change your connection line:

- php -r "new PDO('mysql:host=localhost;port=3306;charset=utf8', 'root', '123123');"
+ php -r "new PDO('mysql:host=docker-mysql;port=3306;charset=utf8', 'root', '123123');"

All 3 comments

My first guess is that it is dying trying to find the mysql unix socket file, when it needs to be connecting over a non-localhost connection. If you are using the compose file to start them, then you can connect to the dns name provided by docker (ie, links: docker-mysql, sets the dns name to be docker-mysql). So you have to change your connection line:

- php -r "new PDO('mysql:host=localhost;port=3306;charset=utf8', 'root', '123123');"
+ php -r "new PDO('mysql:host=docker-mysql;port=3306;charset=utf8', 'root', '123123');"

@yosifkit yes, you are right. Modifying the host solved the problem.

What should I do make it work with localhost or 127.0.0.1?
Any ideas?

From one container to another? they would have to be in the same net namespace. Not something I would really recommend since if you ever deploy it, then it would be limited to running the containers on the same host.

Was this page helpful?
0 / 5 - 0 ratings