Christian O.
Christian O.

Reputation: 498

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?

Upvotes: 3

Views: 3902

Answers (2)

Mostafa Wael
Mostafa Wael

Reputation: 3848

From my experience, I always find that creating a generic Docker image and installing your requirements to it is a lot better. I know that you the original question asks for using Tensorflow Docker image but I will leave this answer for reference. Here is a simple Dockerfile ready to use:

# Base image, you can change it as you want
FROM python:3.10-slim-buster

# Install necessary system dependencies
RUN apt-get update \
    && apt-get install -y --no-install-recommends \
        build-essential \
        libblas3 \
        liblapack3 \
        libopenblas-dev \
        liblapack-dev \
        libatlas-base-dev \
        gfortran \
    && rm -rf /var/lib/apt/lists/*

# Install Python dependencies
COPY requirements.txt /app/requirements.txt
WORKDIR /app
RUN pip install --no-cache-dir --upgrade pip \
    && pip install --no-cache-dir -r requirements.txt \
    && rm -rf /root/.cache/pip

You can build the image using: docker build -t my_image .

Upvotes: 1

anemyte
anemyte

Reputation: 20286

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.

Upvotes: 5

Related Questions