Docker compose cannot RUN symfony database commands

I’m developing a symfony 5.1 project with docker-compose and using GitLab registry.

I have a mysql:5.7 images for database, nginx:stable for server and php:7.4-fpm for PHP.

but when trying to compile it cannot RUN bin/console doctrine:database:create --if-not-exists --no-interaction; so it seems it can’t connect to the database. Everything on the Dockerfile before creates database seems to work fine.

The error message I get is:

Step 10/12 : RUN bin/console doctrine:database:create –if-not-exists –no-interaction;
—> Running in 8acb003982a3

In AbstractMySQLDriver.php line 93:

An exception occurred in driver: SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known

In PDOConnection.php line 31:

SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known

In PDOConnection.php line 27:

SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known

In PDOConnection.php line 27:

PDO::__construct(): php_network_getaddresses: getaddrinfo failed: Name or service not known

doctrine:database:create [-s|–shard SHARD] [-c|–connection [CONNECTION]] [–if-not-exists] [-h|–help] [-q|–quiet] [-v|vv|vvv|–verbose] [-V|–version] [–ansi] [–no-ansi] [-n|–no-interaction] [-e|–env ENV] [–no-debug] [–]

ERROR: Service ‘php’ failed to build: The command ‘/bin/sh -c bin/console doctrine:database:create –if-not-exists –no-interaction;’ returned a non-zero code: 1

I have hardcoded in symfony .env file the database data like this: DATABASE_URL=mysql://root:[email protected]:/mydb?serverVersion=5.7

and in config/packages/doctrine.yaml

doctrine:
  dbal:
    driver: 'pdo_mysql'
    url: '%env(resolve:DATABASE_URL)%'

My mysql Dockerfile is as simple as FROM mysql:5.7

My ngingx Dockerfile:

FROM nginx:stable
COPY ./src/www/ /usr/share/nginx/html/
COPY ./src/site.conf /etc/nginx/conf.d/default.conf

My php Dockerfile is:

FROM php:7.4-fpm
RUN apt-get update 
  && apt-get install -y --no-install-recommends 
    curl 
    libpng-dev 
    libonig-dev 
    libicu-dev 
    libmemcached-dev 
    libz-dev 
    libpq-dev 
    libjpeg-dev 
    libfreetype6-dev 
    libssl-dev 
    libzip-dev 
    zip 
    unzip 
    libmcrypt-dev 
    libxml2-dev 
    libbz2-dev 
    libjpeg62-turbo-dev 
    git 
  && apt-get clean 
  && rm -rf /var/lib/apt/lists/*

RUN docker-php-ext-install mysqli pdo pdo_mysql intl mbstring exif pcntl bcmath gd

ENV COMPOSER_HOME /usr/share/nginx/html/.composer

RUN curl -sS https://getcomposer.org/installer | php -- 
    --install-dir=/usr/bin 
    --filename=composer

RUN mkdir -p $COMPOSER_HOME/cache

COPY ./src/www /usr/share/nginx/html
WORKDIR /usr/share/nginx/html/smartlink
RUN COMPOSER_MEMORY_LIMIT=-1 composer install --prefer-source --no-interaction
RUN bin/console doctrine:database:create --if-not-exists --no-interaction;
RUN bin/console doctrine:migrations:migrate --no-interaction;
RUN bin/console doctrine:schema:update --force --no-interaction;

And the docker compose file is:

version: "3.7"

services:
  
  mysql:
    container_name: symfony_mysql
    build: 
      context: .
      dockerfile: ./infrastructure/docker/mysql/Dockerfile
    ports:
      - '33060:3306'
    environment:
      MYSQL_ROOT_PASSWORD: 'root'
      MYSQL_USER: 'user'
      MYSQL_PASSWORD: 'test'
    volumes:
      - ./src/mysql:/var/lib/mysql
    env_file:
      - ./infrastructure/environments/development.env
    networks:
      - code-network

  php:
    build: 
      context: .
      dockerfile: ./infrastructure/docker/php/Dockerfile
    tty: true
    volumes:
      - ./src/www:/usr/share/nginx/html
    env_file:
      - ./infrastructure/environments/development.env
    restart: always
    networks:
      - code-network
    depends_on: 
      - mysql

  http:
    build: 
      context: .
      dockerfile: ./infrastructure/docker/nginx/Dockerfile
    ports:
      - "8080:80"
    tty: true
    volumes:
      - ./src/www:/usr/share/nginx/html
      - ./src/site.conf:/etc/nginx/conf.d/default.conf
    env_file:
      - ./infrastructure/environments/development.env
    restart: always
    networks:
      - code-network
    depends_on:
      - php

networks:
  code-network:
    driver: bridge

When running on local if I need to run symfony database commands from terminal, I need to change the hardcoded database configuration of .env to DATABASE_URL=mysql://root:[email protected]:33060/mydb?serverVersion=5.7, with this config the database commands work from the terminal, but then symfony do not connect to the database. And with the other configuration symfony connects to the database but the terminal commands do not work.

It’s like the three containers are not sharing the folder where symfony is correctly.

I’ve read a lot of tutorials to deploy symfony with this 3 containers, and I think I followed the tutorials correctly, but I can’t make it work and I do not see anything wrong or different from the tutorials. Need some help with this error please.

PD: I add the conf nginx file, it the same I’ve read on the tutorials but I’m not sure if it’s ok as each tutorial use a slightly different conf file:

server {
    listen 80;
    # listen [::]:80 default_server ipv6only=on;

    # server_name localhost;
    root /usr/share/nginx/html/smartlink/public;
    # index index.php index.html index.htm;
    client_max_body_size 100m;

    location / {
        try_files $uri /index.php$is_args$args;
    }

    location ~ ^/index.php(/|$) {
        fastcgi_pass php:9000;
        fastcgi_split_path_info ^(.+.php)(/.*)$;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        fastcgi_param DOCUMENT_ROOT $realpath_root;
        internal;
    }

    location ~ .php$ {
        return 404;
    }

    location ~ /.(?:ht|git|svn) {
        deny all;
    }

    error_log  /var/log/nginx/smartlinkapp_error.log;
    access_log /var/log/nginx/smartlinkapp_access.log;
}

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *