Add pip requirements to docker image in runtime

后端 未结 3 1969
清歌不尽
清歌不尽 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:31

    Using the concepts that @Rao and @ROMANARMY have explained in their answers, I find out finally a way of doing what I wanted: add extra python requirements to a self-created docker image.

    My new Dockerfile is as follows:

    FROM ubuntu:14.04
    
    RUN apt-get update
    RUN apt-get install -y python-pip python-dev build-essential 
    RUN pip install --upgrade pip
    
    WORKDIR /root
    
    COPY install_req.sh .
    
    CMD ["/bin/bash" , "install_req.sh"]
    

    I've added as first command the execution of a shell script that has the following content:

    #!/bin/bash
    pip install -r /root/sourceCode/requirements.txt
    pip freeze > /root/sourceCode/freeze.txt
    

    And finally I build and run the image using these commands:

    docker build --tag test .
    docker run -itd --name container_test -v $(pwd)/sourceCode:/root/sourceCode test <- without any parameter at the end
    

    As I explained at the beginning of the post, I have in a local folder a folder named sourceCode that contains a valid requirements.txt file with only one line "gunicorn"

    So finally I've the ability of adding some extra requirements (gunicorn package in this example) to a given docker image.

    After building and running my experiment If I check the logs (docker logs container_test) I see something like this:

    Downloading gunicorn-19.6.0-py2.py3-none-any.whl (114kB)
        100% |################################| 122kB 1.1MB/s 
    Installing collected packages: gunicorn
    

    Furthermore, the container have created a freeze.txt file inside the mounted volume that contains all the pip packages installed, including the desired gunicorn:

    chardet==2.0.1
    colorama==0.2.5
    gunicorn==19.6.0
    html5lib==0.999
    requests==2.2.1
    six==1.5.2
    urllib3==1.7.1
    

    Now I've other problems with the permissions of the new created file, but that will be probably in a new post.

    Thank you!

提交回复
热议问题