Add pip requirements to docker image in runtime

后端 未结 3 1860
清歌不尽
清歌不尽 2021-02-20 17:06

I want to be able to add some extra requirements to an own create docker image. My strategy is build the image from a dockerfile with a CMD command that will execute a \"pip ins

3条回答
  •  甜味超标
    2021-02-20 17:29

    You may change the last statement i.e., CMD to below.

    --specify absolute path of pip location in below statement

    CMD ["/usr/bin/pip", "install", "-r", "/root/sourceCode/requirements.txt"]
    

    UPDATE: adding additional answer based on comments.

    One thing must be noted that, if customized image is needed with additional requirements, that should part of the image rather than doing at run time.

    Using below base image to test:

    docker pull colstrom/python:legacy
    

    So, installing packages should be run using RUN command of Dockerfile.
    And CMD should be used what app you actually wanted to run as a process inside of container.

    Just checking if the base image has any pip packages by running below command and results nothing.

    docker run --rm --name=testpy colstrom/python:legacy /usr/bin/pip freeze
    

    Here is simple example to demonstrate the same:

    Dockerfile

    FROM colstrom/python:legacy
    COPY requirements.txt /requirements.txt
    RUN ["/usr/bin/pip", "install", "-r", "/requirements.txt"]
    CMD ["/usr/bin/pip", "freeze"]
    

    requirements.txt

    selenium
    

    Build the image with pip packages Hope you know to place Dockerfile, requirements.txt file in fresh directory.

    D:\dockers\py1>docker build -t pypiptest .
    Sending build context to Docker daemon 3.072 kB
    Step 1 : FROM colstrom/python:legacy
     ---> 640409fadf3d
    Step 2 : COPY requirements.txt /requirements.txt
     ---> abbe03846376
    Removing intermediate container c883642f06fb
    Step 3 : RUN /usr/bin/pip install -r /requirements.txt
     ---> Running in 1987b5d47171
    Collecting selenium (from -r /requirements.txt (line 1))
      Downloading selenium-3.0.1-py2.py3-none-any.whl (913kB)
    Installing collected packages: selenium
    Successfully installed selenium-3.0.1
     ---> f0bc90e6ac94
    Removing intermediate container 1987b5d47171
    Step 4 : CMD /usr/bin/pip freeze
     ---> Running in 6c3435177a37
     ---> dc1925a4f36d
    Removing intermediate container 6c3435177a37
    Successfully built dc1925a4f36d
    SECURITY WARNING: You are building a Docker image from Windows against a non-Windows Docker host. All files and directories added to build context will have '-rwxr-xr-x' permissions. It is recommended to double check and reset permissions for sensitive files and directories.
    

    Now run the image If you are not passing any external command, then container takes command from CMD which is just shows the list of pip packages. Here in this case, selenium.

    D:\dockers\py1>docker run -itd --name testreq pypiptest
    039972151eedbe388b50b2b4cd16af37b94e6d70febbcb5897ee58ef545b1435
    
    D:\dockers\py1>docker logs testreq
    selenium==3.0.1
    

    So, the above shows that package is installed successfully.

    Hope this is helpful.

提交回复
热议问题