Docker Community Forums

Share and learn in the Docker community.

Building on Docker Hub ignores context path when copying files

I am trying to upload a Django app to Docker Hub. On local machine (Ubuntu 18.04) everything works fine, but on Docker Hub there is an issue that the requirements.txt file cannot be found.

Local machine:

sudo docker-compose build --no-cache

Result (it’s okay):

Step 5/7 : COPY . .
 ---> 5542d55caeae
Step 6/7 : RUN file="$(ls -1 )" && echo $file
 ---> Running in b85a55aa2640
Dockerfile db.sqlite3 hello_django manage.py requirements.txt venv
Removing intermediate container b85a55aa2640
 ---> 532e91546d41
Step 7/7 : RUN pip install -r requirements.txt
 ---> Running in e940ebf96023
Collecting Django==3.2.2....

But, Docker Hub:

    Step 5/7 : COPY . .
---> 852fa937cb0a
Step 6/7 : RUN file="$(ls -1 )" && echo $file
---> Running in 281d9580d608
README.md app config docker-compose.yml
Removing intermediate container 281d9580d608
---> 99eaafb1a55d
Step 7/7 : RUN pip install -r requirements.txt
---> Running in d0e180d83772
e[91mERROR: Could not open requirements file: [Errno 2] No such file or directory: 'requirements.txt'
Removing intermediate container d0e180d83772
The command '/bin/sh -c pip install -r requirements.txt' returned a non-zero code: 1

app/Dockerfile

FROM python:3.8.3-alpine

ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

WORKDIR /code
COPY . .
RUN file="$(ls -1 )" && echo $file
RUN pip install -r requirements.txt

docker-composer.yml

version: '3'
    services:
      web:
          build:
            context: app
            dockerfile: Dockerfile
          volumes:
            - ./app/:/code/
          ports:
            - "8000:8000"
          env_file:
            - ./config/.env.dev
          command: python manage.py runserver 0.0.0.0:8000

Project Structure:
Watch project structure

UPDATE: Docker is buiding from Github. File requirements.txt is in the GitHub repository (app folder), but for some reason during build Docker Hub copies files from project root folder and not the contents of the app folder.

Github: GitHub - sigalglebru/django-on-docker

Is Docker Hub building from GitHub? If yes, is that file even in the GitHub repository?

Also, I guess you saw more differences in the output:

Dockerfile db.sqlite3 hello_django manage.py requirements.txt venv

…versus:

README.md app config docker-compose.yml

Seems it’s a different source folder altogether?

Yes, it is buiding from Github.
File requirements.txt is in the GitHub repository (app folder), but for some reason during build Docker Hub copies files from project root folder and not the contents of the app folder.

I think the following will not help, but just in case.

I’m not building on Docker Hub, but for local builds I simply use build: app which automatically defaults to the Dockerfile in that context folder:

services:
  web:
      build: app

I’d not expect that to make any difference, but who knows?

For more complex setups (like when the Dockerfile is named differently, or is in some subfolder of the context), I happen to use a ./ prefix in context. I think I may never have tried without that ./ prefix, and very likely not required for Docker Hub, but again, who knows:

services:
  web:
      build:
        context: ./app
        dockerfile: Dockerfile

Thank you, but all of your suggestions work only on my local machine.
Docker Hub can’t find this file.

From the Docker Hub documentation:

Set the build context and Dockerfile location

Depending on how the files are arranged in your source code repository, the files required to build your images may not be at the repository root. If that’s the case, you can specify a path where the build looks for the files.

The build context is the path to the files needed for the build, relative to the root of the repository. Enter the path to these files in the Build context field. Enter / to set the build context as the root of the source code repository.

Note
If you delete the default path / from the Build context field and leave it blank, the build system uses the path to the Dockerfile as the build context. However, to avoid confusion we recommend that you specify the complete path.

You can specify the Dockerfile location as a path relative to the build context. If the Dockerfile is at the root of the build context path, leave the Dockerfile path set to / . (If the build context field is blank, set the path to the Dockerfile from the root of the source repository.)

I still feel your setup should work without any tweaks (though I’ve not used builds on Docker Hub), but: any chance you entered some details for the configuration above?

(You may want to edit your question’s title to, say, “Building on Docker Hub ignores context path when copying files”?)

1 Like

Thank you, I found solution, thanks to you

Dockerfile:

FROM python:3.8.3-alpine

ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

WORKDIR /code
COPY ./app .

RUN pip install -r requirements.txt

docker-compose.yml

version: "3.6"
services:
  python:
    restart: always
    build:
      context: .
      dockerfile: docker/Dockerfile
    expose:
      - 8000
    ports:
      - 8000:8000
    command: "python manage.py runserver 0.0.0.0:8000"

I just copied files from./app to the mounted volume, and little changed context, but still don’t understand why it worked fine on the local machine