Search code examples
dockerdocker-composefastapiuvicorn

FastApi runs with DockerFile doesn't run with docker-compose


I have this simple project structure: project structure

I use FastApi as backend and React as frontend.

When I run FastApi DockerFile it works just fine:

FROM python

WORKDIR /app

COPY requirements.txt .

RUN pip install --no-cache-dir -r requirements.txt

COPY ./app .

ENV PYTHONUNBUFFERED=1
ENV PYTHONDONTWRITEBYTECODE=1

EXPOSE 8000

CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]

Checked using curl

But the moment I use docker-compose:

version: "3.9"
services:
  backend:
    container_name: backend
    build: ./FastAPI
    ports:
      - "8000:8000"
    depends_on:
      - redis
    command: uvicorn main:app --host 0.0.0.0 --port 8000 --reload

  frontend:
    container_name: frontend
    build: ./React/react-app
    ports:
      - "3000:3000"
    depends_on:
      - backend

  redis:
    container_name: redis
    image: "redis:alpine"

I get this error:

Error response from daemon: failed to create shim task: OCI runtime create failed: runc create failed: unable to start c ontainer process: exec: "uvicorn": executable file not found in $PATH: unknown docker-compose process finished with exit code 1


Solution

  • try

    1st delete your "backend" local image

    docker rmi -f {imageid}
    

    docker-compose.yml

    services:
      backend:
        container_name: backend
        build:
          context: ./FastAPI
          dockerfile: ./Dockerfile
        ports:
          - "8000:8000"
    

    requirements.txt

    fastapi==0.95.*
    uvicorn[standard]==0.21.*
    

    enter image description here

    enter image description here