Home > Enterprise >  Why I can not start proxy service? Bind for 0.0.0.0:8000 failed: port is already allocated
Why I can not start proxy service? Bind for 0.0.0.0:8000 failed: port is already allocated

Time:04-02

This is my docker-compose-proxy.yml

version: "3.7"

services:
  app:
    build:
      context: .
    ports:
      - "8000:8000"
    volumes:
      - ./app:/app
      - static_data:/vol/web

    environment:
      - DB_HOST=db
      - DB_NAME=app
      - DB_USER=postgres
      - DB_PASS=supersecretpassword
      - ALLOWED_HOSTS=127.0.0.1

    depends_on:
      - db

  proxy:
    image : proxy:latest    
    depends_on:
      - app
    ports:
      - "8000:8000"
    volumes:    
      - static_data:/vol/static_data


  db:
    image: postgres:10-alpine
    environment:
      - POSTGRES_DB=app
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=supersecretpassword

volumes:
  static_data: 

I checked the port before I run my command

netstat -ltnp | grep ':8000' 

and port was not occupied. when I go for

docker-compose -f docker-compose-proxy.yml up

I got error

ERROR: for 9bac48e03668_recipe-app-api-devops_proxy_1  Cannot start service proxy: driver failed programming external connectivity on endpoint recipe-app-api-devops_proxy_1 (af5860c135cb37026dcac6ce27151cd4e8448eaddc542d50dcd009c0e24c09fa): Bind for 0.0.0.0:8000 failed: port is already allocated

Why? How to resolve this issue?

CodePudding user response:

You specified port 8000 at

    ports:
      - "8000:8000"

Since this port is already used for something, you get the error that it's already allocated. So, you will need to find out what is using port 8000 and either change the port of your container, stop the other process, or change the other process's port.

CodePudding user response:

You're trying to bind host port 8000 to two different things:

services:
  app:
    ports:
      - "8000:8000"
  proxy:
    ports:
      - "8000:8000"

So this tells Compose to try to route host port 8000 to the app container, and also to route host port 8000 to the proxy container, and it can't do both. That's essentially the error you're getting.

If you want all requests to your system to go through the proxy container, you can just delete the ports: block from the app container. It will still be visible from other containers in the same Compose file via http://app:8000 but it won't be reachable from outside Docker.

If you need both containers to be accessible, you need to change the first ports: number, but not the second, on one or the other of the containers.

ports:
  - '8001:8000' # host port 8001 -> container port 8000

This won't affect connections between containers at all; regardless of what ports: are or aren't present, they will always use the "standard" port number for the container they're trying to connect to.

  • Related