WolfiG
WolfiG

Reputation: 1163

Docker build fails when calling RUN pip install --no-cache-dir -r requirements.txt

Updates:

Initial post

I want to run one of my python scripts in a docker container on a Debian 11 system. This far I came with my Dockerfile:

FROM python:3.12-slim

WORKDIR /usr/local/app

COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt

COPY PythonScript.py ./
CMD ["python", "PythonScript.py --arg1 value1 --..."]

This simple Dockerfile fails to build:

docker build -t myApp .

ends with error:

> [4/5] RUN pip install --no-cache-dir -r requirements.txt:
#5 0.035 Incorrect Usage: flag provided but not defined: -keep
#5 0.035   
#5 0.035 NAME:
#5 0.035    runc run - create and run a container
#5 0.035 
#5 0.035 USAGE:
#5 0.035    runc run [command options] <container-id>
#5 0.035 
#5 0.035 Where "<container-id>" is your name for the instance of the container that you
#5 0.035 are starting. The name you provide for the container instance must be unique on
#5 0.035 your host.
#5 0.035 
#5 0.035 DESCRIPTION:
#5 0.035    The run command creates an instance of a container for a bundle. The bundle
#5 0.035 is a directory with a specification file named "config.json" and a root
#5 0.035 filesystem.
#5 0.035 
#5 0.035 The specification file includes an args parameter. The args parameter is used   
#5 0.035 to specify command(s) that get run when the container is started. To change    the
#5 0.035 command(s) that get executed on start, edit the args parameter of the spec. See
#5 0.035 "runc spec --help" for more explanation.
#5 0.035 
#5 0.035 OPTIONS:
#5 0.035    --bundle value, -b value  path to the root of the bundle directory, defaults to the current directory
#5 0.035    --console-socket value    path to an AF_UNIX socket which will receive a file descriptor referencing the master end of the console's pseudoterminal
#5 0.035    --detach, -d              detach from the container's process
#5 0.035    --pid-file value          specify the file to write the process id to
#5 0.035    --no-subreaper            disable the use of the subreaper used to reap reparented processes
#5 0.035    --no-pivot                do not use pivot root to jail process inside rootfs.  This should be used whenever the rootfs is on top of a ramdisk
#5 0.035    --no-new-keyring          do not create a new session keyring for the container.  This will cause the container to inherit the calling processes session key
#5 0.035    --preserve-fds value      Pass N additional file descriptors to the container (stdio + $LISTEN_FDS + N in total) (default: 0)
#5 0.035    
#5 0.036 flag provided but not defined: -keep
------
process "/bin/sh -c pip install --no-cache-dir -r requirements.txt" did not complete successfully: exit code: 1

As it seems, this has something to do with runc but I don't have a clue where and how.

Upvotes: 0

Views: 155

Answers (1)

Gleichmut
Gleichmut

Reputation: 6989

# Use the official slim image for a smaller footprint
FROM python:3.12-slim

# Set working directory for the application
WORKDIR /usr/local/app

# Copy requirements file and install dependencies
COPY requirements.txt .
RUN pip install --no-cache-dir --upgrade pip && \
    pip install --no-cache-dir -r requirements.txt

# Copy the main Python script to the working directory
COPY PythonScript.py .

# Run the Python script with command-line arguments as needed
CMD ["python", "PythonScript.py", "--arg1", "value1", "--arg2", "value2"]

I build this image without issues by providing stubs for requirements.txt and PythonScript.py

$ docker --version
Docker version 27.3.1, build ce12230
$ docker build -t my_app .
[+] Building 17.3s (10/10) FINISHED                              docker:default
 => [internal] load build definition from Dockerfile                       0.0s
 => => transferring dockerfile: 575B                                       0.0s
 => [internal] load .dockerignore                                          0.0s
 => => transferring context: 2B                                            0.0s
 => [internal] load metadata for docker.io/library/python:3.12-slim        0.4s
 => [1/5] FROM docker.io/library/python:3.12-slim@sha256:032c52613401895a  3.1s
 => => resolve docker.io/library/python:3.12-slim@sha256:032c52613401895a  0.0s
 => => sha256:0dd753b657c966f26a5151f899593efa1f2874d71fabced 250B / 250B  0.3s
 => => sha256:032c52613401895aa3d418a4c563d2d05f993bc3ecc 9.12kB / 9.12kB  0.0s
 => => sha256:311c6c7090a52874c9118ad17dc95cd7d030dd8ec13 1.75kB / 1.75kB  0.0s
 => => sha256:fd162521da09c903c4a095888014bc3ecfab6c6d284 5.32kB / 5.32kB  0.0s
 => => sha256:f281ad68d61216b3c73260807e28f1b53c4b5f3f9e4 3.51MB / 3.51MB  1.1s
 => => sha256:aa77a90b11d9467bf46de202798f3e67b1172caff 13.63MB / 13.63MB  2.3s
 => => extracting sha256:f281ad68d61216b3c73260807e28f1b53c4b5f3f9e4af49a  0.1s
 => => extracting sha256:aa77a90b11d9467bf46de202798f3e67b1172caff28f7a19  0.5s
 => => extracting sha256:0dd753b657c966f26a5151f899593efa1f2874d71fabcedf  0.0s
 => [internal] load build context                                          0.0s
 => => transferring context: 72B                                           0.0s
 => [2/5] WORKDIR /usr/local/app                                           0.3s
 => [3/5] COPY requirements.txt .                                          0.1s
 => [4/5] RUN pip install --no-cache-dir --upgrade pip &&     pip instal  12.6s
 => [5/5] COPY PythonScript.py .                                           0.1s 
 => exporting to image                                                     0.5s
 => => exporting layers                                                    0.5s
 => => writing image sha256:d0570e6168ea5858bcee587580d55b705fbdcf0d2fc88  0.0s
 => => naming to docker.io/library/my_app 

Investigate your requirements.txt for issues.

Upvotes: 0

Related Questions