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 install -r" command using a mounted volume in runtime.
This is my dockerfile:
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
CMD ["pip install -r /root/sourceCode/requirements.txt"]
Having that dockerfile I build the image:
sudo docker build -t test .
And finally I try to attach my new requirements using this command:
sudo docker run -v $(pwd)/sourceCode:/root/sourceCode -it test /bin/bash
My local folder "sourceCode" has inside a valid requirements.txt file (it contains only one line with the value "gunicorn"). When I get the prompt I can see that the requirements file is there, but if I execute a pip freeze command the gunicorn package is not listed.
Why the requirements.txt file is been attached correctly but the pip command is not working properly?
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:
I've added as first command the execution of a shell script that has the following content:
And finally I build and run the image using these commands:
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:Furthermore, the container have created a freeze.txt file inside the mounted volume that contains all the pip packages installed, including the desired gunicorn:
Now I've other problems with the permissions of the new created file, but that will be probably in a new post.
Thank you!
You may change the last statement i.e.,
CMD
to below.--specify absolute path of pip location in below statement
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:
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.
Here is simple example to demonstrate the same:
Dockerfile
requirements.txt
Build the image with pip packages Hope you know to place Dockerfile, requirements.txt file in fresh directory.
Now run the image If you are not passing any external command, then container takes command from
CMD
which is just shows the list ofpip
packages. Here in this case,selenium
.So, the above shows that package is installed successfully.
Hope this is helpful.
TLDR
pip
command isn't running because you are telling Docker to run/bin/bash
instead.Longer explanation
The default
ENTRYPOINT
for a container is/bin/sh -c
. You don't override that in the Dockerfile, so that remains. The defaultCMD
instruction is probably nothing. You do override that in your Dockerfile. When you run (ignore the volume for brevity)what actually executes inside the container is
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)
what actually executes inside the container is
the
CMD
arguments you specified in your Dockerfile get overridden by theCOMMAND
you specify in the command line. Recall thatdocker run
command takes this formFurther reading
This answer has a really to the point explanation of what
CMD
andENTRYPOINT
instructions doThis blog post on the difference between
ENTRYPOINT
andCMD
instructions that's worth reading.