Home > Blockchain >  Cannot access to my PGSQL Docker container with PHP container
Cannot access to my PGSQL Docker container with PHP container

Time:03-26

I'm setting up a CI using Docker using Github workflows for my Symfony project. I use for this an image of PHP8.1-fpm and Pgsql-13-alpine.

The docker-compose.test.yaml :

version: '3.3'
services:
    phptest:
        build: ./devops/docker/tests/docker/php
        volumes:
            - .:/var/www:delegated
        links:
            - dbtest
        networks:
            - test

    dbtest:
        image: postgres:${POSTGRES_VERSION:-13}-alpine
        environment:
            POSTGRES_DB: test
            POSTGRES_PASSWORD: test
            POSTGRES_USER: test
        ports:
            - 5431:5432
        networks:
            - test

networks:
    test:

The .env.test:

# define your env variables for the test env here
KERNEL_CLASS='App\Kernel'
APP_SECRET='$ecretf0rt3st'
SYMFONY_DEPRECATIONS_HELPER=999999
PANTHER_APP_ENV=panther
DATABASE_URL=postgresql://test:test@dbtest:5431/test?serverVersion=13&charset=utf8

The PHP container works fine for the others tests and pdo_pgsql is installed inside my Dockerfile.

My Makefile looks like this :

DOCKER_COMPOSE = docker-compose
DOCKER_TEST = $(DOCKER_COMPOSE) -f docker-compose.test.yaml run --rm

.PHONY: test
test: ##@ Execute les tests
    $(DOCKER_TEST) phptest bin/console doctrine:schema:validate --skip-sync --env=test
    $(DOCKER_TEST) phptest bin/console doctrine:schema:update --force --env=test

When I tried to update the database schema I have this error :

An exception occurred in the driver: SQLSTATE[08006] [7] could not connect to server: Connection refused  
Is the server running on host "dbtest" (192.168.208.2) and accepting TCP/IP connections on port 5431?                                                                          

I tried to change the .env.test with 127.0.0.1 instead dbtest too but same error.

I tried a loot of fix by different topics I found without success. Someone has an idea ?

Thanks by advance

CodePudding user response:

You are mapping port 5431 on your host machine to the 5432 port on the dbtest container. But phptest is connecting to dbtest directly, so you should use the 5432 port instead.

DATABASE_URL=postgresql://test:test@dbtest:5432/test?serverVersion=13&charset=utf8
  • Related