5

I want to setup a small docker environment with php 7.2.10-fpm, nginx and mysql 5.7 in order to run Laravel 5.7 in it. Unfortunatelly I get the following error when trying to connect to mysql from my app service (trying to run docker-composer exec app php artisan migrate):

In Connector.php
SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known
PDO::__construct(): php_network_getaddresses: getaddrinfo failed: Name or service not known

Trying to run docker-compose exec app mysql -u root -p returns: ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2 "No such file or directory")

Starting and building my container via docker-compose up --build throws no errors. It says that mysql is running and waiting for connections. I've also deleted and recreated all images via docker system prune --force --volumes.

Maybe I miss something in my docker-compose.yml?

version: '3.3'
services:
  app:
    build:
      context: ./
      dockerfile: app.dockerfile
    working_dir: /var/www
    volumes:
      - ./../backend:/var/www
    environment:
      - "DB_PORT=3306"
      - "DB_HOST=database"
  web:
    build:
      context: ./
      dockerfile: web.dockerfile
    working_dir: /var/www
    depends_on:
      - mysql
    volumes:
      - ./../backend:/var/www
    ports:
        - 8080:80
  mysql:
    image: mysql:5.7
    volumes:
      - db_data:/var/lib/mysql
    restart: always
    environment:
      - "MYSQL_DATABASE=shop"
      - "MYSQL_USER=shop_u"
      - "MYSQL_PASSWORD=secret"
      - "MYSQL_ROOT_PASSWORD=secret"
    ports:
      - 33061:3306
    command: --disable-partition-engine-check
volumes:
  db_data:

App.dockerfile:

FROM php:7.2.10-fpm

RUN apt-get update
RUN apt-get install -y libmcrypt-dev
RUN apt-get install -y mysql-client
RUN apt-get install -y libmagickwand-dev --no-install-recommends
RUN pecl install imagick
RUN docker-php-ext-enable imagick
RUN docker-php-ext-install pdo_mysql
RUN pecl install mcrypt-1.0.1
RUN docker-php-ext-enable mcrypt
RUN php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
RUN php -r "if (hash_file('SHA384', 'composer-setup.php') === '93b54496392c062774670ac18b134c3b3a95e5a5e5c8f1a9f115f203b75bf9a129d5daa8ba6a13e2cc8a1da0806388a8') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
RUN php composer-setup.php
RUN php -r "unlink('composer-setup.php');"
RUN mv composer.phar /usr/local/bin/composer
RUN apt-get install -y git
RUN apt-get update && apt-get install -y zlib1g-dev
RUN docker-php-ext-install zip

And finally my .env-file from Laravel:

DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=shop
DB_USERNAME=shop_u
DB_PASSWORD=secret

Update

docker-compose images lists me the following containers:

+-----------------+-------------------+----------------+
|    Container    |    Repository     |    ImageID     |
+-----------------+-------------------+----------------+
| docker_app_1    |  docker_app       |   a94dba376130 |
| docker_mysql_1  |  docker_database  | 563a026a1511   |
| docker_web_1    |  docker_web       |   d9024ab40b73 |
+-----------------+-------------------+----------------+

Running ping docker_mysql_1 from docker exec -it docker_app_1 bash results in bash: ping: command not found.

Running ping docker_mysql_1 from docker exec -it docker_web_1 bash works fine.

Brotzka
  • 2,959
  • 4
  • 35
  • 56
  • can you ping your mysql container from inside the php container by using its service name? – lvthillo Oct 05 '18 at 05:54
  • Can you tell me how to ping an container from within another container? ``docker-compose exec app ping mysql_1`` results in ``OCI runtime exec failed: exec failed: container_linux.go:348: starting container process caused "exec: \"PING\": executable file not found in $PATH": unknown`` – Brotzka Oct 05 '18 at 06:07
  • 1
    find container ID and use docker exec -it container-id bash – lvthillo Oct 05 '18 at 06:33
  • I've updated my post. Pinging from app does not work. Pinging from web works fine. – Brotzka Oct 05 '18 at 07:35
  • The output of the first only means that ping isn't installed in the image, but because you can ping from mysql to your app means that you will probably also be able to ping from your app to mysql which means your containers can access eachother. – lvthillo Oct 05 '18 at 07:39
  • I would recommend to set container_name in your docker compose to define your own container names instead of using docker_mysql_1: https://docs.docker.com/compose/compose-file/#container_name. Then you can use the values you choose fro container_name in your configs and connection strings. – lvthillo Oct 05 '18 at 07:40
  • But this won't fix my problem. I stil get ``ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2 "No such file or directory")`` when trying to access mysql from my app container. – Brotzka Oct 05 '18 at 07:57
  • 1
    The error seems to tell that your app tries to connect to a MySQL database on its localhost (inside the app container) instead of connecting to the MySQL database container (by using the container_name) – lvthillo Oct 05 '18 at 08:04

0 Answers0