docker file pip install locally

267 Views Asked by At

this is my docker file:

WORKDIR /scan

RUN pip install pymongo
RUN pip install netmiko
RUN pip install pyats[full]
ENV Testbed=
ENV arr=
# ENV device=
# ENV id=

COPY . /scan
ENTRYPOINT ["python3", "main.py"]

I want to pip install this particular packages locally Like from a tar files. I want to build the image without any internet, so that's why I need it to be locally so that the pip will take them from local and not from internet. I just want to clarify that when I pip install requirements it will go anyway to the internet, I already try it.

2

There are 2 best solutions below

3
Piotr Żak On BEST ANSWER

You need base image in dockerfiles:

# Set the base image
FROM python:3.8

# Set the working directory
WORKDIR /scan

# Copy the local tar files into the image
COPY your_package1.tar.gz your_package2.tar.gz /tmp/

# Install packages from local tar files
RUN pip install /tmp/your_package1.tar.gz && \
    pip install /tmp/your_package2.tar.gz

# Set environment variables if needed
ENV Testbed=
ENV arr=

# Copy the rest of your application files
COPY . /scan

# Specify the entry point
ENTRYPOINT ["python3", "main.py"]

For running any commands in the process of building docker image (keyword RUN):

# Install system packages using apt-get
RUN apt-get update && \
    apt-get install -y openssh-server apache2 supervisor
0
Jeril On

Build you docker images from a internet PC:

docker build –t myimage:0.1 .

Save the built image:

docker save -o <path for generated tar file> <image name>

In you case:

docker save -o c:/myimage_0-1.tar myimage:0.1

Then copy your image to a new system (which has no internet access) with regular file transfer tools such as cp, scp, or rsync (preferred for big files). After that you will have to load the image into Docker:

docker load -i <path to image tar file>

Then you can use the run command to start the image.