Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Unable to requests FastAPI running in docker-compose

I have a Dockerfile which exposes an API on port 8000:

# ..

EXPOSE 8000
ENV PYTHONPATH="src/."
CMD ["gunicorn", "-b :8000", "-k", "uvicorn.workers.UvicornWorker", "fingerprinter.api.server:app"]

It’s just a simple FastAPI server with a simple endpoint:

@app.get("/health")
def health():
    return "OK"

This is the relevant part of the docker-compose.yaml:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

version: "3.7"

services:

  fprint-api:
    container_name: fprint-api-v2
    image: "fprint-api:v0.0.1"
    depends_on:
      - fprint-db
      - fprint-svc

    network_mode: "host"
    extra_hosts:
      - "host.docker.internal:host-gateway"
    expose:
      - "8000"

    build:
      context: ../.
      dockerfile: docker/Dockerfile.fprint-api

However, I am not able to reach the endpoints.

>Solution :

Expose in Dockerfile does not really publish the port Dockerfile – EXPOSE. It is more like a
‘documentation’ for a reader of Dockerfile, it shows that the port is intended to be published.

In docker-compose.yml you map port from docker container to host system by using port, Docker compose – ports. In docker-compose.yml keyword expose exposes port only for the linked services, it does not publish the port to the host machine Docker compose – expose

So your docker-compose.yml file should look like this:

version: "3.7"

services:

  fprint-api:
    container_name: fprint-api-v2
    image: "fprint-api:v0.0.1"
    depends_on:
      - fprint-db
      - fprint-svc

    # network_mode: "host"
    extra_hosts:
      - "host.docker.internal:host-gateway"
    ports:
      - "8000:8000"

    build:
      context: ../.
      dockerfile: docker/Dockerfile.fprint-api
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading