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?
2
Answers
You specified port
8000
atSince 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.You’re trying to bind host port 8000 to two different things:
So this tells Compose to try to route host port 8000 to the
app
container, and also to route host port 8000 to theproxy
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 theports:
block from theapp
container. It will still be visible from other containers in the same Compose file viahttp://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.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.