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

codex_dev's avatar

docker-compose sail network

I'm using Laravel Sail, and this is my Docker Compose file. I need to connect Laravel to an external network, but I want the database to be accessible only within the container for the Laravel application because my local ports for the PostgreSQL database are conflicting. When I build and run this with sail artisan migrate, I get an error stating that it cannot connect to the database.

This is mu local .env for database:

DB_CONNECTION=pgsql
DB_HOST=pgsql
DB_PORT=5432
DB_DATABASE=social-login
DB_USERNAME=admin
DB_PASSWORD=password

When I change the local port in docker-compose, Laravel cannot connect to the database.

0 likes
5 replies
CodingArchaeo's avatar

I haven't tested this, but I hope this is nevertheless helpful:

Do you still need to connect from your host machine to the DB? If not, then you should drop the pgsql.ports directive from the docker-compose.yml.

The DB_PORT in the .env file should remain 5432 regardless of the local port in docker-compose.

The only other piece of advice I have, would be to go into your into your laravel.test container (using docker exec) and try different things to connect to the pgsql container from there.

codex_dev's avatar

yes I still need to be connected to the database

codex_dev's avatar

I managed to fix it. You can close this

CodingArchaeo's avatar

That's great! Out of curiosity: How did you fix it? what did the issue turn out to be?

technofreaks's avatar

change driver: bridge to driver: host.

networks:
  socialLoginSail:
    driver: host
  docker-alpine_kontentino:
    external: true

Please or to participate in this conversation.