skip to Main Content

I am currently in the root of my project.

enter image description here

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


  1. 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 to ADD ../../requirements.txt /

    Login or Signup to reply.
  2. 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:

    1. Context path is relative to the location of docker-compose.yaml
    2. ADD (or COPY`) 1st argument is a path relative to that context
    3. ADD (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:

    1. Your docker-compose.yaml is inside ./project/docker & your context is ./.

      => Your context is ./project/docker

    2. 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 :

    ADD ../../requirements.txt . 
    
    1. The last fix will not work because even if we put the relative path of that ADDed file, that file is not inside the context.

    Solution

    • The context must be changed to include all COPYed/ADDed files.
    • COPYed/ADDed file paths must keep being relative to the new context.

    project/docker/docker-compose.yaml

    services:
      api-service-track-1:
        build:
          context: ../../ # context must include `requirements.txt`
          dockerfile: ./project/docker/Dockerfile # must be relative to context
    

    project/docker/Dockerfile

    # ./ is Relative to the context
    ADD ./requirements.txt ...
    
    Login or Signup to reply.
  3. 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.

    docker build . -f project/docker/Dockerfile

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