Add pip requirements to docker image in runtime

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

    TLDR

    pip command isn't running because you are telling Docker to run /bin/bash instead.

    docker run -v $(pwd)/sourceCode:/root/sourceCode -it test /bin/bash
                                                                  ^
                                                                 here
    

    Longer explanation

    The default ENTRYPOINT for a container is /bin/sh -c. You don't override that in the Dockerfile, so that remains. The default CMD instruction is probably nothing. You do override that in your Dockerfile. When you run (ignore the volume for brevity)

    docker run -it test
    

    what actually executes inside the container is

    /bin/sh -c pip install -r /root/sourceCode/requirements.txt
    

    Pretty straight forward, looks like it will run pip when you start the container.

    Now let's take a look at the command you used to start the container (again, ignoring volumes)

    docker run -v -it test /bin/bash
    

    what actually executes inside the container is

    /bin/sh -c /bin/bash
    

    the CMD arguments you specified in your Dockerfile get overridden by the COMMAND you specify in the command line. Recall that docker run command takes this form

    docker run [OPTIONS] IMAGE[:TAG|@DIGEST] [COMMAND] [ARG...]
    

    Further reading

    1. This answer has a really to the point explanation of what CMD and ENTRYPOINT instructions do

      The ENTRYPOINT specifies a command that will always be executed when the container starts.

      The CMD specifies arguments that will be fed to the ENTRYPOINT.

    2. This blog post on the difference between ENTRYPOINT and CMD instructions that's worth reading.

提交回复
热议问题