0

I made a FastAPI API service with routes per "endpoint sort", which works fine running it locally through the terminal using "uvicorn main:app --reload". Running it using Docker-compose works fine as well. (see screenshot for file structure)

enter image description here

Whenever i try to build an image and do "docker run myimage" it starts up and displays the address: http://0.0.0.0:8000. Sending requests to it however doesn't seem to work for some reason. Why does "Docker-compose" work and "Docker Run" not?

I need a docker image that i can deploy on Google Cloud run. I'm relatively new to Docker so this seems like complete magic to me. Would love to understand what i did wrong here.

my Dockerfile:

FROM python:3.8.10
COPY ./app /app
RUN pip install --no-cache-dir --upgrade -r /app/requirements.txt
RUN apt-get update
RUN apt-get --yes install libsndfile1-dev

EXPOSE 8000
CMD uvicorn app.main:app --host 0.0.0.0 --port 8000

my Docker-compose file:

version: '3.3'
services:
  app:
    build: .
    command: uvicorn app.main:app --host 0.0.0.0 --port 8000
    ports:
      - "8000:8000"

my main.py:

from fastapi import FastAPI
import uvicorn
import os
from app.routes.api import router as api_router

app = FastAPI()
app.include_router(api_router)

@app.get('/')
def index():
   return {'message': 'Everything online'}
M.vankekeren
  • 115
  • 1
  • 6
  • What happens when trying to connect to the address http://localhost:8000 on that host where you run "Docker Run"? – Wild Zyzop Nov 03 '22 at 16:10
  • Does this answer your question? [FastAPI app running locally but not in Docker container](https://stackoverflow.com/questions/62351462/fastapi-app-running-locally-but-not-in-docker-container) – Chris Nov 03 '22 at 16:13

1 Answers1

1

I see that you've included the port mapping 8000:8000 in your docker-compose.yml file. Did you also include the port mapping when running the container with docker run? For example, you'll want something like docker run -p 8000:8000 <your_image_name> ... in order to map port 8000 of the container to port 8000 of the host machine.

Ryan Cahill
  • 662
  • 3
  • 10