skip to Main Content

I’m trying to Dockerize my FastApi app, but it crashes with this error right after I run the command:

docker-compose -f local.yml up -d

Can anyone help me, please?

Dockerfile:

FROM python:3.6.11-alpine3.11
ARG MYSQL_SERVER
ARG POSTGRES_SERVER
ENV ENVTYPE=local
ENV PYTHONUNBUFFERED 1
ENV APP_HOME=/home/app/web
RUN mkdir -p $APP_HOME
WORKDIR $APP_HOME

RUN apk update && apk add --no-cache bash
ADD /compose/scripts.sh $APP_HOME
ADD /requirements/$ENVTYPE.txt $APP_HOME
RUN chmod +x scripts.sh

RUN ./scripts.sh
RUN pip install -r /home/app/web/$ENVTYPE.txt; mkdir /log;

COPY /src/ $APP_HOME
CMD ["uvicorn", "app.main:app", "--reload", "--host", "0.0.0.0", "--port", "8080"]

local.yml file:

version: '3.7'
services:
  nginx:
    env_file: .env
    build: 
      context: .
      dockerfile: ./compose/local/nginx.Dockerfile
    restart: always
    ports:
       - "${EX_PORT_NGINX:-8030}:80"
    volumes:
       - ./nginx/site.conf:/etc/nginx/conf.d/default.conf
  core:
    env_file: .env
    build: 
      context: .
      dockerfile: ./compose/local/core.Dockerfile
      args:
        MYSQL_SERVER: ${MYSQL_SERVER:-}
        POSTGRES_SERVER: ${POSTGRES_SERVER:-}
    restart: always
    volumes:
       - ./src:/home/app/web/
    logging:
       driver: "json-file"
       options:
          max-size: "5m"
          max-file: "10"

Error:

Cannot start service core: failed to create shim: OCI runtime create failed: container_linux.go:380: starting container process caused: exec: "uvicorn": executable file not found in $PATH: unknown

3

Answers


  1. Add to Dockerfile,
    ENV PATH /home/${USERNAME}/.local/bin:${PATH},
    before
    RUN pip install -r /home/app/web/$ENVTYPE.txt; mkdir /log;,
    by replacing ${USERNAME} with the container user.

    If you don’t know the current user, add RUN echo $(python3 -m site --user-base) somewhere in the Dockerfile. Then copy the output of that echo to replace /home/${USERNAME}/.local.

    Login or Signup to reply.
  2. In my case I add commands poetry run and it’s works.

    services:
     api:
    
      ...
    
      command: [
        "poetry", "run",
        "uvicorn",
        "app:main",
        "--port", "5000"
      ]
    
    Login or Signup to reply.
  3. You need to pip install fastapi and uvicorn:

    FROM python:latest
    
    WORKDIR /app
    
    COPY requirements.txt ./
    
    RUN pip install --no-cache-dir --upgrade -r requirements.txt
    RUN pip install fastapi uvicorn
    
    COPY main.py ./
    
    CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "80"]
    

    Or include in requirements.txt:

    fastapi>=0.68.0,<0.69.0
    pydantic>=1.8.0,<2.0.0
    uvicorn>=0.15.0,<0.16.0
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search