0
votes

I have a problem. When I try to create a database from Dockerfile, I got an error :

In AbstractMySQLDriver.php line 112:

An exception occurred in driver: SQLSTATE[HY000] [2002] php_network_getaddr esses: getaddrinfo failed: Temporary failure in name resolution

In Exception.php line 18:

SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Tempor ary failure in name resolution

In PDOConnection.php line 38:

SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Tempor ary failure in name resolution

In PDOConnection.php line 38:

PDO::__construct(): php_network_getaddresses: getaddrinfo failed: Temporary failure in name resolution

doctrine:database:create [-s|--shard SHARD] [-c|--connection [CONNECTION]] [--if-not-exists]

ERROR: Service 'www' failed to build : The command '/bin/sh -c php bin/console doctrine:database:create --if-not-exists' returned a non-zero code: 1

project structure :

project
    bin/
    config/
    src/
    docker/
        apache/
            vhost.conf
        php/
            Dockerfile
    src/
    docker-compose.yml

docker-compose.yml:

version: "3.9"

services:
    db:
        image: mariadb
        container_name: db_app
        restart: always
        volumes:
            - app-db:/var/lib/mysql
        environment:
            MYSQL_USER: root
            MYSQL_PASSWORD: pass
            MYSQL_ROOT_PASSWORD: pass
        ports:
            - 1212:3306
        expose:
            - 1212
        networks:
            - dev

    www:
        depends_on:
            - db
        container_name: www_app
        ports:
            - 8686:80
        build:
            context: .
            dockerfile: docker/php/Dockerfile
        restart: always
        networks:
            - dev

networks:
    dev:

volumes:
    app-db:

In my .env file:

DATABASE_URL="mysql://root:pass@db_app:3306/super-app"

Dockerfile :

FROM php:7.4-apache

RUN a2enmod rewrite

RUN apt-get update && apt-get install -y zlib1g-dev g++ git libicu-dev zip libzip-dev zip \
    && docker-php-ext-install intl opcache pdo pdo_mysql \
    && pecl install apcu \
    && docker-php-ext-enable apcu \
    && docker-php-ext-configure zip \
    && docker-php-ext-install zip

# source + conf apache
COPY . /var/www
COPY ./docker/apache/vhosts.conf /etc/apache2/sites-enabled

WORKDIR /var/www

# Composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
RUN composer install --prefer-dist --no-progress --no-interaction

# doctrine
RUN php bin/console doctrine:database:create --if-not-exists

If I don't write this line: "RUN php bin/console doctrine:database:create --if-not-exists", docker is ok and I can enter in my container and type mannually this command, but I want to do this in my Dockerfile.

1

1 Answers

0
votes

The issue here is that RUN commands in your Dockerfile are executed at the time of the www image build, which happens before either of the containers are launched and networked by docker-compose. What you want to utilise is CMD, which is the command the container executes when you launch the built image. A typical way to do this could be to create an init shell script to call from the Dockerfile and put any start-up commands you need in it.

Create this in docker/php/init.sh and chmod 744 to make it executable:

#!/bin/bash

bin/console doctrine:database:create --if-not-exists
apache2-foreground

Then in your Dockerfile replace:

# doctrine
RUN php bin/console doctrine:database:create --if-not-exists

With:

# init
CMD ["/var/www/docker/php/init.sh"]

See this article for further details about the differences between RUN and CMD - https://www.ctl.io/developers/blog/post/dockerfile-entrypoint-vs-cmd/