Home » Php » unable to run phpunit tests on api rest served by a docker with php-fpm and nginx

unable to run phpunit tests on api rest served by a docker with php-fpm and nginx

Posted by: admin February 25, 2020 Leave a comment

Questions:

I have a docker-compose.yml file where i configure following service :

  • nginx (web) -> server_php_web_1
  • php-fpm (php) -> server_php_php_1
  • mysql (db) -> server_php_db_1

My nginx serve an api rest php over https and vhost is : back-php.dev with associated and working certificates

From outside docker i can curl back-php.dev without problem

there is a directory test where i can run the test when running this command :

docker exec server_php_php_1 php /code/vendor/phpunit/bin /code/tests

But when i m running tests i obtains the following result :

GuzzleHttp\Exception\ConnectException: cURL error 6: Could not resolve host: back-php.dev

I have also tried the following :

docker exec -it server_php_php_1 /bin/bash
curl server_php_web_1

And i can curl successfully the container running nginx, but how can i do if i want to test the following url https://back-php.dev

Here is the structure of docker-compose.yml simplified :

services:
    web:
        image: nginx:latest
        ports:
            - "80:80"
            - "443:443"
        networks:
            - code-network
        extra_hosts:
            - "back-php.dev:127.0.0.1"
        links:
            - php

    php:
        build: ./
        networks:
            - code-network
    db:
        image: mariadb:10.1
        ports:
            - "3306:3306"
        environment:
            - MYSQL_ROOT_PASSWORD=root
        networks:
            - code-network

networks:
    code-network:
        driver: bridge

And the dockerfile associated to php :

FROM phpdockerio/php74-fpm

RUN apt update
RUN apt install -y \
        php7.4-pdo \
        php7.4-json \
        php7.4-curl \
        php7.4-mbstring \
        php7.4-common \
        php7.4-mysql

RUN echo "ENV_NAME=dev" >> /etc/php/7.4/cli/php.ini
How to&Answers:

extra_hosts:
– “back-php.dev:127.0.0.1”

Create hostname mapping “inside containers” not outside(host).


So if you want to access you app from “outside”(host) you’ll need to create a new entry on /etc/hosts and add

<container_ip> back-php.dev

To find your container ip do

docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' container_name_or_id

Update1:

php:
  ...
  links:
    - back-php.dev:web