Muhammad Ahmed
Muhammad Ahmed

Reputation: 400

Docker .env port numbers are not being updated correctly

I have a docker compose file which contains a part

env_file:
  - .env
environment:
  - POSTGRES_DB=${POSTGRES_SCHEMA}
  - POSTGRES_USER=${POSTGRES_USER}
  - POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
ports:
  - "${HOST_PORT}:5432"

In my .env file, I have

HOST_PORT=5431
CONTAINER_PORT=5431

Ideally what should happen is, when I run the container using docker compose, the port number should be 0.0.0.0:5431->5432/tcp, because I'm trying to map 5431 host port to 5432, but it doesn't happen. Whatever I change the HOST_PORT to, in the .env file, it always shows 0.0.0.0:5432->5432/tcp. I'm new to docker so confused over this.

Upvotes: 0

Views: 22

Answers (1)

os11k
os11k

Reputation: 1358

Not sure why it doesn't work for you, but all logic and config file on your side seems correct. Seems you have somewhere typo or other error. Please put all config files, but not just code snippet, so I can take a look further.

Here how it worked for me, as you can see here port is mapped correctly:

CONTAINER ID   IMAGE                                                 COMMAND                  CREATED         STATUS                   PORTS
                   NAMES
30d96608a0bc   postgres:14-alpine                                    "docker-entrypoint.s…"   4 seconds ago   Up 3 seconds             0.0.0.0:5431->5432/tcp, [::]:5431->5432/tcp   test-postgres-1

here is my docker-compose.yml:

version: '3.9'

services:
  postgres:
    image: postgres:14-alpine
    ports:
      - "${HOST_PORT}:5432"
    volumes:
      - ~/apps/postgres:/var/lib/postgresql/data
    environment:
      - POSTGRES_PASSWORD=S3cret
      - POSTGRES_USER=citizix_user
      - POSTGRES_DB=citizix_db

here is my .env file what is located in same directory where compose file:

HOST_PORT=5431

Upvotes: 0

Related Questions