skip to Main Content

Here is the directory structure of my project:Directory tree

From the backend folder, I want to create a Docker image that loads the setup.py and requirements.txt files which are in the parent folder. Additionally, I want to load the src/ folder which is also in the parent folder. Here is the content of setup.py: https://github.com/ahmedaao/design-autonomous-car/blob/master/setup.py
and here is the content of fastapi_app.py: https://github.com/ahmedaao/design-autonomous-car/blob/master/backend_app.py

Here is Dockerfile:

FROM python:3.10.12

WORKDIR /app

COPY ./fastapi_app.py /app/
COPY ./../requirements.txt /app/
COPY ./../src/ /app/src
COPY ./../setup.py /app/

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

EXPOSE 8000

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

When I try to build the image with: sudo docker build -t test .
I have this output: Output from docker build

Maybe you can help to solve this problem. Thanks

You can view the Dockerfile I tested above.

2

Answers


  1. Your docker build command is run from the backend/ directory. It might make more sense to run it from the project root.

    If your project is structured like this (from your screenshot):

    ├── backend
    │   ├── Dockerfile
    │   └── fastapi_app.py
    ├── requirements.txt
    ├── setup.py
    └── src
        ├── __init__.py
        └── metrics.py
    

    Then perhaps it might make more sense to formulate your Dockerfile like this:

    FROM python:3.10.12
    
    WORKDIR /app
    
    COPY backend/fastapi_app.py /app/
    COPY requirements.txt /app/
    COPY src/ /app/src
    COPY setup.py /app/
    
    RUN pip install --no-cache-dir -r requirements.txt
    
    EXPOSE 8000
    
    CMD ["uvicorn", "fastapi_app:app", "--host", "0.0.0.0", "--port", "8000"]
    

    You would then build the image from the project root:

    docker build -f backend/Dockerfile -t test .
    

    This would be a good approach if you are going to have multiple Dockerfile in the project (perhaps you will have a frontend/Dockerfile as well?).

    Alternatively, if there will just be a single image, you could move the Dockerfile to the project root and then your build command would simplify to:

    docker build -t test .
    

    Either approach will get you around the problem of trying to copy files from outside of the build context.

    Login or Signup to reply.
  2. Here’s the working example

    Project structure

    .
    ├── backend
    │   ├── Dockerfile
    │   └── fastapi_app.py
    ├── requirements.txt
    ├── setup.py
    └── src
        ├── __init__.py
        └── metrics.py
    

    Dockerfile

    FROM python:3.10.12
    
    WORKDIR /app
    
    COPY . .
    
    RUN pip install -r requirements.txt
    
    EXPOSE 8000
    
    CMD ["uvicorn", "backend.fastapi_app:app", "--host", "0.0.0.0", "--port", "8000"]
    

    Build & Run

    FYI: If you intend to build & run from inside the backend
    directory. Then you should build with the command bash docker build -f Dockerfile -t fastapi_test_v2 ../ everything remains the
    same!

    # Build the app: From the project root
    docker build -f backend/Dockerfile -t fastapi_test .
    
    # Build the app: from backend dir
    bash docker build -f Dockerfile -t fastapi_test_v2 ../
    
    # Run the container: 
    docker run -it -p 8000:8000 fastapi_test
    

    Test out

    $ curl -X GET http://localhost:8000       
    {"status":"OK","message":"Pong"}
    

    Notes

    • Make sure to build the image from the project root
    • Set the correct Flask App reference in Dockerfile in the CMD. For my case, backend.fastapi_app:app
    • Run the container by mapping the host vs container port. In my case, I have used -p 8000:8000

    I hope this helps!

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search