3

I want to create a docker image (docker version: 20.10.20)that contains python libraires from a requirement.txt file that contains 50 libraries. Without facing root user permissions how can proceed. Here is the file:

From ubuntu:latest

RUN apt update
RUN apt install python3 -y
WORKDIR /Destop/DS

# COPY requirement.txt ./
# RUN pip install -r requirement.txt
# it contains only pandas==1.5.1

COPY script2.py ./
CMD ["python3", "./script2.py"]

It failed at requiremnt.txt command *error it takes lot of time while creating image. because it ask for root permission.

3
  • How did it fail? Please provede the full error and traceback Commented Nov 14, 2022 at 13:29
  • 1
    as an FYI, you can just use one of the python images instead, see dockerhub Commented Nov 14, 2022 at 13:47
  • You could have used a python:3.7-alpine base image in your Dockerfile FROM python:3.7-alpine to save you from the hassle of writing separate service for python in your docker-compose. You dont need to use an ubuntu base image and the docker-compose could be cleaner that way Commented Nov 14, 2022 at 14:01

1 Answer 1

6

For me the only problem in your Dockerfile is in the line RUN apt install python -y. This is erroring with Package 'python' has no installation candidate.

It is expected since python refers to version 2.x of Python wich is deprecated and no longer present in the default Ubuntu repositories.

Changing your Dockerfile to use Python version 3.x worked fine for me.

FROM ubuntu:latest

RUN apt update
RUN apt install python3 python3-pip -y
WORKDIR /Destop/DS

COPY requirement.txt ./
RUN pip3 install -r requirement.txt

COPY script2.py ./
CMD ["python3", "./script2.py"]

To test I used requirement.txt

pandas==1.5.1

and script2.py

import pandas as pd

print(pd.__version__)

With this building the docker image and running a container from it executed succesfully.

docker build -t myimage .
docker run --rm myimage
Sign up to request clarification or add additional context in comments.

5 Comments

E: Could not open lock file /var/lib/dpkg/lock-frontend - open (13: Permission denied) E: Unable to acquire the dpkg frontend lock (/var/lib/dpkg/lock-frontend), are you root? while running apt install python3 python3-pip -y
Which version of docker are you running? Please include the output of docker info in your original question
Docker version 20.10.20
Same here and no error... What is the ubuntu image you are using? it is 22.04 for me. What OS are you on?
it works now... i made few changes in ubuntu setting with %admin ALL=(ALL) NOPASSWD: ALL , %sudo ALL=(ALL:ALL) NOPASSWD: ALL and changed docker command by adding sudo.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.