Skip to content

Cannot connect to mysql container from fpm-alpine container #320

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
igodorogea opened this issue Oct 26, 2016 · 3 comments
Closed

Cannot connect to mysql container from fpm-alpine container #320

igodorogea opened this issue Oct 26, 2016 · 3 comments

Comments

@igodorogea
Copy link

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
@yosifkit
Copy link
Member

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');"

@igodorogea
Copy link
Author

@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?

@yosifkit
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants