Search code examples
pythondockertensorflowkeras

Using Keras in a Tensorflow Docker Image?


I have a sentiment classifier build with Keras that I want to run using my GPU. As Tensorflows GPU support page recommends, I have installed Docker and downloaded a Tensorflow Docker image.

Now when I try to run my code on one of the Tensorflow Images, I get error codes when trying to import stuff like Keras or Pandas.

I am a bit of a newbie when it comes to Docker but as I understand it, the images simply don't have those libraries installed. So what do I do if I want to use additional besides Tensorflow or whatever else is installed on the image? How do I add these to the image?


Solution

  • Option 1: Add packages to the container:

    docker exec <container_name> pip install ...
    

    The downside is that you will have to repeat this every time you recreate the container.

    Option 2: Create your own image, using tensorflow image as a base

    Create a file named Dockerfile:

    FROM tensorflow/tensorflow:latest-gpu-jupyter  # change if necessary
    RUN pip install ...
    # Visit https://docs.docker.com/engine/reference/builder/ for format reference
    

    Then build an image from it:

    cd /directory/with/the/Dockerfile
    docker build -t my-tf-image .
    

    Then run using your own image:

    docker run --gpus all -d -v /some/data:/data my-tf-image
    

    I also recommend using docker-compose for dev environment so that you don't have to remember all these commands. You can create a docker-compose.yml and describe the container using YAML format. Then you can just docker-compose build to build and docker-compose up to run.