Home > Enterprise >  How to force docker-compose to use specific port
How to force docker-compose to use specific port

Time:01-11

Hey I don't know if the title is right, but I'm working on my project. Tools that I use are symfony and docker. When I start pgsql database on my other machines, they're using port 5432, but I just installed fresh linux on my new computer and it uses port 49153 it took me quiet some time to figure out that the port was the problem. Same thing happens if I start new project don't change anything and pg database still runs on port 49153, it's a bit annoying while working on few machines. So is it possible to force docker to setup database on port 5432 for all of my future projects or everytime I have to change port in .env file?

My docker-compose.yml

services:
  database:
    image: postgres:${POSTGRES_VERSION:-14}-alpine
    environment:
      POSTGRES_DB: ${POSTGRES_DB:-app}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-!ChangeMe!}
      POSTGRES_USER: ${POSTGRES_USER:-app}
    volumes:
      - db-data:/var/lib/postgresql/data:rw

.env file

DATABASE_URL="postgresql://app:[email protected]:5432/app?serverVersion=14&charset=utf8"

CodePudding user response:

For exemple if you want to map your pgsql database :
From the local docker container port 5432
To your local env 5432 port.

Add

 ports:
    - "5432:5432"

I forgot which one is the container and which one is the local but you will find out pretty easier now.

services:
  database:
    image: postgres:${POSTGRES_VERSION:-14}-alpine
    environment:
      POSTGRES_DB: ${POSTGRES_DB:-app}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-!ChangeMe!}
      POSTGRES_USER: ${POSTGRES_USER:-app}
    volumes:
      - db-data:/var/lib/postgresql/data:rw
    ports:
        - "5432:5432"
  • Related