Search code examples
dockerdockerhub

dockerhub build fails requirements.txt not found


I am building a Docker image for some python packages, for which I using requirements.txt to install all python packages by pip.

I have all Dockerfile, requirements.txt, and related python scripts in the subfolder of my GitHub repo.

-> Main repo
---> folder
    --> some python scripts
    --> docker_folder
       --> Dockerfile
       --> requirements.txt
---> some files
---> some files

I have built the image locally which works fine but while configuring autobuild in Docker Hub it throws an error by stating that requirements.txt not found but it found Dockerfile. I have changed the Dockerfile location in the build setting on Docker Hub by:folder/docker_folder/Dockerfile.

I think the main problem lies in the Build configuration setting. Any help? Thanks.

logfile from Dockerhub built:

Cloning into '.'...
Warning: Permanently added the RSA host key for IP address '140.82.114.3' to the list of known hosts.
Reset branch 'master'
Your branch is up-to-date with 'origin/master'.
KernelVersion: 4.4.0-1060-aws
Components: [{u'Version': u'19.03.8', u'Name': u'Engine', u'Details': {u'KernelVersion': u'4.4.0-1060-aws', u'Os': u'linux', u'BuildTime': u'2020-03-11T01:24:30.000000000+00:00', u'ApiVersion': u'1.40', u'MinAPIVersion': u'1.12', u'GitCommit': u'afacb8b7f0', u'Arch': u'amd64', u'Experimental': u'false', u'GoVersion': u'go1.12.17'}}, {u'Version': u'1.2.13', u'Name': u'containerd', u'Details': {u'GitCommit': u'7ad184331fa3e55e52b890ea95e65ba581ae3429'}}, {u'Version': u'1.0.0-rc10', u'Name': u'runc', u'Details': {u'GitCommit': u'dc9208a3303feef5b3839f4323d9beb36df0a9dd'}}, {u'Version': u'0.18.0', u'Name': u'docker-init', u'Details': {u'GitCommit': u'fec3683'}}]
Arch: amd64
BuildTime: 2020-03-11T01:24:30.000000000+00:00
ApiVersion: 1.40
Platform: {u'Name': u'Docker Engine - Community'}
Version: 19.03.8
MinAPIVersion: 1.12
GitCommit: afacb8b7f0
Os: linux
GoVersion: go1.12.17
Starting build of index.docker.io/darsh3/python_drl:latest...
Step 1/5 : FROM python:3.8
---> 8f6adb7689b5
Step 2/5 : WORKDIR ${PWD}/..
---> Running in 516d190b493f
Removing intermediate container 516d190b493f
---> e948c6e6f641
Step 3/5 : COPY requirements.txt ./
COPY failed: stat /var/lib/docker/tmp/docker-builder968608568/requirements.txt: no such file or directory

Solution

  • With the directory structure you show, if you run

    docker build -f folder/docker_folder/Dockerfile "Main repo"
    

    then the source of all COPY commands is relative to the "Main repo" directory.

    FROM python:3.8
    WORKDIR /app
    # Needs to COPY from the directory relative to the `docker build`
    # context directory argument
    COPY folder/docker_folder/requirements.txt .
    RUN pip install -r requirements.txt
    

    You might consider moving the requirements.txt file up to the top level of your repository or perhaps the directory containing the Python scripts, since it's a standard Python packaging file and not Docker-specific. If the only Docker-specific thing you have is the Dockerfile then putting it in the top-level directory is fairly common as well.

    # If the Dockerfile is in the named directory, no -f option is needed
    docker build "Main repo/folder"
    
    # If the file is in the named directory, just use its name without a path
    COPY requirements.txt .