Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add Python libraries to Docker image

Tags:

python

docker

Today I started working with Docker. So please bear with me. I'm not even sure if the title makes sense. I just installed Tensorflow using Docker and wanted to run a script. However, I got the following error saying that Matplotlib is not installed.

Traceback (most recent call last):
File "tf_mlp_v3.py", line 3, in <module>
import matplotlib.pyplot as plt
ModuleNotFoundError: No module named 'matplotlib'

I used the following command to install Tensorflow

docker pull tensorflow/tensorflow:latest-gpu-jupyter

How can I now add other python libraries such as Matplotlib to that image?

like image 722
Gilfoyle Avatar asked Oct 01 '19 19:10

Gilfoyle


People also ask

Can I install packages in Docker container?

To install any packages, you first need to update the OS. Step 3: After you have updated the Docker Container, you can now install the Firefox and Vim packages inside it. You can now easily use these packages through the bash itself.

Does a Docker image contain libraries?

A Docker image contains application code, libraries, tools, dependencies and other files needed to make an application run.

How to create Docker image with Python application?

Dockerfile contains instructions to prepare Docker image with our Python Application. Following is the content of Dockerfile. FROM python COPY . /src CMD ["python", "/src/PythonExample.py"] 4. Build Docker Image Run the following command in Terminal, from python-application directory, to create Docker Image with Python Application.

How do I add additional libraries to a dockerfile?

If you need to add extra libraries, and if you want to include the install of these libraries in a build process, you probably need a Dockerfile . For instance, if you want to install requests, a minimalist Dockerfile could be as follows: Create such a file in myproject/, then cd in myproject/ and simply run docker build .

How to create a python script in Docker Ubuntu?

After installing the python you can create a python script and run easily. But one thing you should note that any editor is not available in docker ubuntu container thus you have to first install it using the apt-get install command. After install lets create a run.py file and then write the line print (“Hello Data Science Learner”) and run it.

How to maintain a docker container with Python?

This comes very handy when you are using a python app such as django or flask and you want to maintain your docker container using the same python script that you use for the application. To use the python library API for docker, you need to install a package called docker−py. You can do so using the following pip command.


2 Answers

To customize an image you generally want to create a new one using the existing image as a base. In Docker it is extremely common to create custom images when existing ones don't quite do what you want. By basing your images off public ones you can add your own customizations without having to repeat (or even know) what the base image does.

  1. Add the necessary steps to a new Dockerfile.

    FROM tensorflow/tensorflow:latest-gpu-jupyter
    
    RUN <extra install steps>
    COPY <extra files>
    

    RUN and COPY are examples of instructions you might use. RUN will run a command of your choosing such as RUN pip install matplotlib. COPY is used to add new files from your machine to the image, such as a configuration file.

  2. Build and tag the new image. Give it a new name of your choosing. I'll call it my-customized-tensorflow, but you can name it anything you like.

    Assuming the Dockerfile is in the current directory, run docker build:

    $ docker build -t my-customized-tensorflow .
    
  3. Now you can use my-customized-tensorflow as you would any other image.

    $ docker run my-customized-tensorflow
    
like image 106
John Kugelman Avatar answered Oct 16 '22 23:10

John Kugelman


Add this to your Dockerfile after pulling the image:

RUN python -m pip install matplotlib
like image 28
jfleach Avatar answered Oct 17 '22 01:10

jfleach