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

ioiofadhil's avatar

Laravel Docker Ports Meaning?

Hi guys, Im new in Docker. I tried set up my laravel application using a Docker. What im not understand is the "ports:" part in docker-compose.yml. So i tried to setup my MongoDB like this :

  mongodb:
    image : mongo
    container_name: mongodb
    volumes:
    - ./docker/mongodb:/data/db
    ports:
    - 27017:27018
    restart: unless-stopped

But, if i set my laravel mongodb .env files port to 27018, It wont connect. But if i set it to 27017, it connects. What is wrong here? Do i miss understand the port meaning?

DB_CONNECTION=mongodb
DB_HOST=host.docker.internal
DB_PORT=27017
DB_DATABASE=laravel
DB_USERNAME=
DB_PASSWORD=
0 likes
2 replies
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

I dont know what port mongodb runs on but I can explain how docker works.

ports:
    - 27017:27018

Here you are exposing port 27017 to your local system. That means that you can connect to the database from your OS using localhost:27017. The port after the colon is the local port (27018) on the container

So it just links 27017 external with 27018 internal

It does not change anything inside docker, so you still need to use the default (27018 I assume) port when using laravel

The great thing about this is that you can expose the database to you local system without getting port conflicts

ioiofadhil's avatar

@Sinnbeck so it doesnt matter with the .env files laravel. I see. Now i understand. Thank you. Another day saved by Mr. Sinnbeck

Please or to participate in this conversation.