2

I'm new to Docker, and I'm trying to dockerize my Django application. My compose file looks like the following:

docker-compose.yml

version: '3'

services:
  web:
    build:
      context: .
      dockerfile: Dockerfile
    env_file:
      - csgo.env
    volumes:
      - .:/code
    ports:
      - 8000:8000
    depends_on:
      - db

  db:
    image: postgres:13
    restart: always
    # Optional: Map the container port to a host port to be able to connect with a local db client 
    ports:
      - 5432:5432
    env_file:
      - csgo.env
    environment:
      - POSTGRES_PASSWORD=db_password
      - DB_NAME=db_name
      - DB_USER=db_user
      - DB_PASSWORD=db_password
      - DB_HOST=localhost
    volumes:
      - ./db/psql-init/db.sql:/docker-entrypoint-initdb.d/db.sql
      - postgres_data:/var/lib/postgresql/data/

volumes:
  postgres_data:

I try to docker-compose up but this error occurs. I wonder what could be the problem? Why it says the PORT is already in use? Error

[+] Running 4/4
 ⠿ Network csgo_default         Created                                                                                               0.0s
 ⠿ Volume "csgo_postgres_data"  Created                                                                                               0.0s
 ⠿ Container csgo-db-1          Created                                                                                               0.1s
 ⠿ Container csgo-web-1         Created                                                                                               0.1s
Attaching to csgo-db-1, csgo-web-1
Error response from daemon: Ports are not available:
 exposing port TCP 0.0.0.0:5432 -> 0.0.0.0:0: listen tcp 0.0.0.0:5432:
 bind: address already in use
Docker
  • 159
  • 1
  • 2
  • 10
  • Most probably because the port is already in use. There are ways to check what process is using a specific port ([Windows](https://dzone.com/articles/how-to-check-which-process-is-using-port-8080-or-a), [Linux](https://www.tecmint.com/find-out-which-process-listening-on-a-particular-port/)). On the host, check what process is using the port, stop that process and try to deploy the docker stack again. – Turing85 Sep 11 '22 at 16:10
  • Seems like you have a postgres service running on your machine. For ubuntu, try `sudo service postgresql stop`. Or, if you need the database running for some other reasion, move the mapped port of the container to another port, e.g. `15432`. – Turing85 Sep 11 '22 at 16:21

1 Answers1

2

Solved: Listed all ports of postgres (Mac):

sudo lsof -i -P | grep LISTEN | grep :5432
postgres   9785              postgres    7u  IPv6 0xd0180dc2abc35a25      0t0    TCP *:5432 (LISTEN)
postgres   9785              postgres    8u  IPv4 0xd0180dc778b0fe35      0t0    TCP *:5432 (LISTEN)

Killed all these processes (How do I close an open port from the terminal on the Mac?)

Docker
  • 159
  • 1
  • 2
  • 10