Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ivan2nn's avatar

Docker Laravel+Postgresql translation to hostname

I am approaching for the first time the Docker universe.

I have this 3 years old working Laravel App (5.2), connected to a Psotgresql db.

The asked some modifications and to bring it to an internal server of their own.

I cannot adapt it to a new Laravel version yet, so I thought it would have been a good idea to deploy in container the 5.2 app in case other people need to work on it.

So far, following some instructions, I ge to this point:

docker-compose.yml:

version: '3'

networks:
    laravel:
        driver: bridge

services:

nginx:
    image: nginx:stable-alpine
    container_name: nginx
    ports:
        - "8080:80"
    volumes:
        - ./src:/var/www/html
        - ./nginx/default.conf:/etc/nginx/conf.d/default.conf
    depends_on: 
        - php
        - psql
    networks:
        - laravel

psql:
    image: postgres:12.7-alpine
    container_name: postgresql
    environment:
        - POSTGRES_HOST_AUTH_METHOD=trust
        - POSTGRES_USER=xxxx
        - POSTGRES_PASSWORD=yyyy
        - POSTGRES_DB=zcreport_ecostarfinal_new
    restart: always
    tty: true
    ports: 
        - "5433:5432"
    volumes:
        - './postgresql:/var/lib/postgresql/data'
    networks:
        - laravel

php:
    build:
        context: ./php
        dockerfile: Dockerfile
    container_name: php
    volumes:
        - ./src:/var/www/html
    ports:
        - "9000:9000"
    networks:
        - laravel

I have the /postgresql folder with (I hope) the data I copied from my local /var/lib/postgresql/12/main folder I have the /src folder whre i put my laravel app

And this is the .env inside /src folder:

DB_CONNECTION=pgsql
DB_HOST=postgresql
DB_PORT=5432
DB_DATABASE=zcreport_ecostarfinal_new
DB_USERNAME=xxxx
DB_PASSWORD=yyyy

Now, when I build and run the docker-compose and check the localhost the site loads but I get a 500 error:

PDOException in Connector.php line 55: SQLSTATE[08006] [7] could not translate host name "psql" to address: Temporary failure in name resolution

I don't know how to solve this problem

Thanks

0 likes
7 replies
ivan2nn's avatar

Ok these are the different pemutations of DB_HOST and DB_PORT:

DB_HOST: localhost
DB_PORT: 5432

-- PDOException with message 'SQLSTATE[08006] [7] could not connect to server: Connection refused
    Is the server running on host "localhost" (127.0.0.1) and accepting
    TCP/IP connections on port 5432?


DB_HOST: localhost
DB_PORT: 5433

-- PDOException with message 'SQLSTATE[08006] [7] could not connect to server: Connection refused
    Is the server running on host "localhost" (127.0.0.1) and accepting
    TCP/IP connections on port 5433?


DB_HOST: postgresql
DB_PORT: 5433 (or 5432)

-- PDOException with message 'SQLSTATE[08006] [7] could not translate host name "postgresql" to address: Temporary failure in name resolution'
ivan2nn's avatar

I also noticed that my postgresl container is always restarting; i don't think it should be doing that

ivan2nn's avatar

Ok,

so I deleted the line:

volumes:
    - './postgresql:/var/lib/postgresql/data'

I set:

DB_HOST: postgresql and DB_PORT to 5432

Now the error is:

			Undefined table: 7 ERROR: relation "species" does not exist

So i don't understand: it seems it's connecting to the DB; but I copied all the file and the subfolders under /var/lib/postgresql/12/main in my local --> inside /postgresql folder that, as shown in the docker-compose.yml, it should be linked to the /var/lib/postgresql/data of the postgres container. I was expecting that I could bring my "already fill" db directly in the container folder.

Maybe that is the error; is there any procedure I can follow to recreate and refill the database I have on my local pc inside the postgres container during docker-compose up?

neilstee's avatar
neilstee
Best Answer
Level 34

I see @ivan2nn although from what I understand volumes don't work that way.

What you could do is delete everything inside ./postgresql folder and after booting up the PostgreSQL then you manually import those data via "docker exec" (you can google it). In that way you correctly import the data not just manually copying folders.

ivan2nn's avatar

You are correct. Yesterday I was able to solve this issue. I dumped the postgres database, and i changed the docker-compose as such:

database:
    build:
        context: ./postgresql
        dockerfile: Dockerfile.db
    container_name: postgresql

and then in the Dockerfile.db:

FROM postgres:12.7-alpine

ADD temp.sql /docker-entrypoint-initdb.d

where temp.sql is the dump file.

And it seems working.

1 like
yogeshgalav's avatar

For me this issue got resolved with laravel env variable

DB_HOST=host.docker.internal
2 likes

Please or to participate in this conversation.