0

My question was closed so I am posting this again because ... The other answers have not helped because killing the process that seems to own port 5000 does not help. Plus I can use port 5000 without Docker.**

Why can't Docker use port 5000 when npm can?

I am trying to run a docker container on macOS. It works when I run npm start but not when I run it as a container. It also seems to work if I switch to port 5001.

> docker run -p 5000:5000 app-flask ERRO[0000] error waiting for
> container: context canceled  docker: Error response from daemon: Ports
> are not available: exposing port TCP 0.0.0.0:5000 -> 0.0.0.0:0: listen
> tcp 0.0.0.0:5000: bind: address already in use.

I don't know why it thinks port 5000 is in use since it works outside of docker. I tried restarting but that didn't help. I also tried killing the process but the process that seems to own port 5000 just restarts. But I am confused here because localhost:5000 works just fine if I use npm start.

➜  app-flask git:(implementation) ✗ lsof -i:5000
COMMAND   PID          USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
ControlCe 745 <me>    7u  IPv4 0x3d5df920d91eb581      0t0  TCP *:commplex-main (LISTEN)
ControlCe 745 <me>    8u  IPv6 0x3d5df925a4daac49      0t0  TCP *:commplex-main (LISTEN)
➜  app-flask git:(<me>) ✗ kill 745
➜  app-flask git:(<me>) ✗ lsof -i:5000
COMMAND     PID          USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
ControlCe 10675 <me>    7u  IPv4 0x3d5df920d9d8e1c1      0t0  TCP *:commplex-main (LISTEN)
ControlCe 10675 <me>    8u  IPv6 0x3d5df925a1176c49      0t0  TCP *:commplex-main (LISTEN)

Here is my Dockerfile:

# syntax=docker/dockerfile:1

FROM python:3.8-slim-buster

WORKDIR /app

COPY requirements.txt requirements.txt
RUN pip3 install -r requirements.txt

COPY . .

EXPOSE 5000

CMD [ "python3", "-m" , "flask", "run", "--host=0.0.0.0"]

Additional info. docker ps output:

➜ docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS
PORTS NAMES

➜ docker run -p 5000:5000 app-flask docker: Error response from daemon: Ports are not available: exposing port TCP 0.0.0.0:5000 -> 0.0.0.0:0: listen tcp 0.0.0.0:5000: bind: address already in use. ERRO[0000] error waiting for container: context canceled

➜ docker ps -a CONTAINER ID IMAGE
COMMAND CREATED STATUS PORTS NAMES 6d813f5c4487 app-flask "python3 -m flask ru…" 9 seconds ago
Created keen_mirzakhani

8
  • What does docker ps -a show? Commented Sep 22, 2023 at 12:40
  • Your lsof output seems to clearly show that something on your host (ControlCe) is listening on port 5000. That seems to be the your problem. Commented Sep 22, 2023 at 12:42
  • ➜ docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES ➜ docker run -p 5000:5000 app-flask docker: Error response from daemon: Ports are not available: exposing port TCP 0.0.0.0:5000 -> 0.0.0.0:0: listen tcp 0.0.0.0:5000: bind: address already in use. ERRO[0000] error waiting for container: context canceled ➜ docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 6d813f5c4487 app-flask "python3 -m flask ru…" 9 seconds ago Created keen_mirzakhani Commented Sep 22, 2023 at 12:44
  • @HansKilian Sorry, I can't make my comment multiline. Basically docker ps -a output is empty before I start the container. Commented Sep 22, 2023 at 12:46
  • You could edit your post and add the information if you want better formatting Commented Sep 22, 2023 at 12:47

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.