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
Your
docker build
command is run from thebackend/
directory. It might make more sense to run it from the project root.If your project is structured like this (from your screenshot):
Then perhaps it might make more sense to formulate your
Dockerfile
like this:You would then build the image from the project root:
This would be a good approach if you are going to have multiple
Dockerfile
in the project (perhaps you will have afrontend/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:Either approach will get you around the problem of trying to copy files from outside of the build context.
Here’s the working example
Project structure
Dockerfile
Build & Run
Test out
Notes
backend.fastapi_app:app
-p 8000:8000
I hope this helps!