0

I have a Dockerfile with EXPOSE 8000 in it. The Docker Compose file that runs it has:

services:
    api:
        image: <local image>
        ports:
            - "8000:8000"

When I exec into the container, the api is running and I can curl it. However, I cannot reach it from the host.

Sam Dillard
  • 670
  • 1
  • 5
  • 18
  • What's running inside the container? Is it listening only on the 127.0.0.1 container-private localhost address? See for example [Deploying a minimal flask app in docker - server connection issues](https://stackoverflow.com/questions/30323224/deploying-a-minimal-flask-app-in-docker-server-connection-issues) – David Maze Jul 13 '21 at 17:16
  • It's listening on `localhost:8000` yeah. Do I need to do some kind of port forwarding? I thought that's what `EXPOSE` did? – Sam Dillard Jul 13 '21 at 17:25
  • @DavidMaze it's a Python HTTP server – Sam Dillard Jul 13 '21 at 17:26
  • `EXPOSE` only adds metadata to the image to indicate what ports the container "should" be listening on when running, but it's just that (metadata). It's still needed for the container's process to be listening on the port. If the process is listening on localhost/127.0.0.1, it can only be accessed from within the container itself. To make it accessible from outside the container, use `0.0.0.0`. The port needs to be mapped (`-p 8000:8000` in your case) to make it publicly accessible from outside the Docker Host – thaJeztah Jul 13 '21 at 17:33
  • @thaJeztah Yes that's what the `ports` field is doing in the docker-compose file. The container is like this: `0.0.0.0:8000->8000/tcp ` – Sam Dillard Jul 13 '21 at 17:34
  • And does your Python app listen on port `8000`? It may listen on `80`. – Saeed Jul 13 '21 at 18:01
  • @Saeed it does, yeah. When I run it on the host machine directly, navigating to `http://localhost:8000` successfully opens the app. – Sam Dillard Jul 13 '21 at 18:13
  • It's using FastAPI. Maybe there's something here? ``` INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit) INFO: 127.0.0.1:45506 - "GET / HTTP/1.1" 200 OK```. It listens on 8000 but maybe Docker has an issue with that randomly generated port? – Sam Dillard Jul 13 '21 at 18:15
  • 1
    "Running on 127.0.0.1:..." means it won't be accessible from outside the container. It looks like it supports a `--host 0.0.0.0` to make it accessible from everywhere ([Not able to access running server on docker in uvicorn](https://stackoverflow.com/questions/60299144/not-able-to-access-running-server-on-docker-in-uvicorn)). – David Maze Jul 13 '21 at 18:19
  • @DavidMaze that was it -- thanks! – Sam Dillard Jul 13 '21 at 19:16

0 Answers0