I am currently in the root of my project.
My goal is to try to run my front-end (front.py) and my back-end(API.py) in a docker container using the command
sudo docker-compose -f project/docker/docker-compose.yml up -d
Unfortunately, it stops when it comes time to ADD ./requirements.txt / in the Dockerfile and cant seem to find it.
enter Building api-service-track-1
Sending build context to Docker daemon 3.072kB
Step 1/5 : FROM python:3
---> a42e2a4f3833
Step 2/5 : ADD ./requirements.txt /
ADD failed: file not found in build context or excluded by .dockerignore: stat requirements.txt: file does not exist
ERROR: Service 'api-service-track-1' failed to build : Build failed
requirements.txt contains
Flask==1.1.2
Flask-SQLAlchemy
Flask-Bcrypt
bleach
and this is my Dockerfile
FROM python:3
ADD ./requirements.txt /
RUN pip3 install -r requirements.txt
WORKDIR /mnt/app/
CMD ["./run.sh", "./project/codeAPI/API.py", "./project/codeAPI/front.py"]
edit:
@DavidMaze Here’s the code of my docker-compose.yml
version: '3'
services:
api-service-track-1:
build:
context: ./
dockerfile: ./Dockerfile
image: img_track1
volumes:
- ./:/mnt/app
ports:
- 5551:5551
edit 2:
This is the error I get after trying to launch docker.
Starting docker_api-service-track-1_1 ... error
ERROR: for docker_api-service-track-1_1 Cannot start service api-service-track-1: failed to create shim: OCI runtime create failed: container_linux.go:380: starting container process caused: exec: "./run.sh": stat ./run.sh: no such file or directory: unknown
ERROR: for api-service-track-1 Cannot start service api-service-track-1: failed to create shim: OCI runtime create failed: container_linux.go:380: starting container process caused: exec: "./run.sh": stat ./run.sh: no such file or directory: unknown
ERROR: Encountered errors while bringing up the project.
Does that mean I need to also change my run.sh directory?
edit 3:
run.sh
#!/bin/bash
export PYTHONPATH=$PWD/project/
echo python path is:
echo $PYTHONPATH
echo running the following python program
echo $@
python3 "$1"
python3 "$2"
3
Answers
The context of the Dockerfile is relative to the current working directory of the Dockerfile.
Can you try moving
requirements.txt
to the./projects/docker
sub-directory? or change the ADD command in your Dockerfile toADD ../../requirements.txt /
Root Cause of the issue
Your context (as directory) does NOT include the ADDed file. This is the main issue.
Details
there are 3 golden rules related to each others:
docker-compose.yaml
ADD (or
COPY`) 1st argument is a path relative to that contextADD (or
COPY`) 1st argument is a path must be INSIDE the context folder (Whether directly, or in the context’s child directories)Let’s apply these 3 rules:
Your
docker-compose.yaml
is inside./project/docker
& your context is./
.=> Your context is
./project/docker
ADD ./requirements.txt ...
& context./project/docker
.=> Means Your
requirements.txt
path is./project/docker/requirements.txt
which is wrong, because it’s exist in../../requirements.txt
relatively to the context (./project/docker
)now let’s say you fix the issue by putting the right path of requirements.txt :
Solution
project/docker/docker-compose.yaml
project/docker/Dockerfile
If you want to keep the current directory(from where you are running the command) as the relative path then give filepath of Dockerfile using flag -f and "." will represent the context.