Skip to content

Commit

Permalink
Add docker files to build Caffe Docker images. These can be used to p…
Browse files Browse the repository at this point in the history
…rovide a devlopment environment,

or provide Caffe runtime images that can be used as executables.
  • Loading branch information
Evan Lezar committed Jan 25, 2016
1 parent b086cc3 commit 6546ca7
Show file tree
Hide file tree
Showing 7 changed files with 277 additions and 0 deletions.
13 changes: 13 additions & 0 deletions docker/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

all:

.PHONY: cpu_runtime gpu_runtime devel

cpu_runtime:
docker build -t caffe:runtime runtime/cpu

gpu_runtime:
docker build -t caffe:runtime runtime/gpu

devel:
docker build -t caffe:devel devel
73 changes: 73 additions & 0 deletions docker/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# Caffe runtime Dockerfiles.

The `runtime` subfolder contains docker files for generating both CPU and GPU runtimes for Caffe. The images can be built using make, or by running:

```
docker build -t caffe runtime/gpu
```
for example.

Note that the GPU runtime requires a CUDA 7.5 capable driver to be installed on the system and [nvidia-docker|https://github.com/NVIDIA/nvidia-docker] for running the Docker containers.

# Running Caffe using the docker image

In order to test the Caffe image, run:
```
docker run -ti caffe --version
```
which should show a message like:
```
libdc1394 error: Failed to initialize libdc1394
caffe version 1.0.0-rc3
```

In order to get the most out of the caffe image, some more advanced `docker run` could be used. For example, running:
```
docker run -ti -v $(pwd):/workspace caffe train --solver=example_solver.prototxt
```
will train a network defined in the `example_solver.prototxt` file in the current directory (`$(pwd)` is maped to the container volume '/workspace' using the `-v` Docker flag).

Note that docker runs all commands as root by default, and thus any output files (e.g. snapshots) generated will be owned by the root user. In order to ensure that the current user is used instead, the following command can be used:
```
docker run -ti -v $(pwd):/workspace -u $(id -u):$(id -g) caffe train --solver=example_solver.prototxt
```
where the `-u` Docker command line option runs the commands in the container as the specified user, and the shell command `id` is used to determine the user and group ID of the current user.


# Caffe development Dockerfile.

The files contained here allow for the Docker images to be built which contain
the development environment for Caffe.

In order to use GPU computing with docker, nvidia-docker (https://github.com/NVIDIA/nvidia-docker) is recommended. The Docker image uses the NVIDIA CUDA
image as a starting point to allow for GPU accelleration within Docker.

# Usage

First ensure that the docker image is built:

```make docker_devel```

This will create a docker image with the tag ```caffe:devel``` which can be
used with Docker as per usual.

A utility script is also provided to start a container based on this image.
This container can be used to build and run caffe.

To use the script run:

```./start_caffe_docker.sh bash```

This should show the following output:

```
Using nvidia-docker
[ NVIDIA ] =INFO= Driver version: 352.63
[ NVIDIA ] =INFO= CUDA image version: 7.5
elezar@caffe_devel:~/caffe$
```
Where this the NVIDIA docker wrapper is used in this case, and ```elezar``` is
the username of the user running docker.

The caffe source folder is mounted as a volume in the container at ```~/caffe```.
26 changes: 26 additions & 0 deletions docker/devel/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
FROM nvidia/cuda:7.5-cudnn4-devel
MAINTAINER team-sl@zalando.de

RUN apt-get update \
&& DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \
cmake \
git \
libatlas-base-dev \
libatlas-dev \
libboost-all-dev \
libgflags-dev \
libgoogle-glog-dev \
libhdf5-serial-dev \
libleveldb-dev \
liblmdb-dev \
libopenblas-dev \
libopencv-dev \
libprotobuf-dev \
libsnappy-dev \
protobuf-compiler \
python-numpy \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*


CMD bash
61 changes: 61 additions & 0 deletions docker/runtime/cpu/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
FROM ubuntu:14.04
MAINTAINER team-sl@zalando.de

# Set up the Caffe build dependencies.
RUN apt-get update \
&& DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \
cmake \
cmake-curses-gui \
git \
ca-certificates \
git \
bc \
build-essential \
libatlas-base-dev \
libatlas-dev \
libboost-all-dev \
libgflags-dev \
libgoogle-glog-dev \
libhdf5-dev \
libleveldb-dev \
liblmdb-dev \
libopenblas-dev \
libopencv-dev \
libprotobuf-dev \
libsnappy-dev \
protobuf-compiler \
python-dev \
python-numpy \
python-pip \
python-protobuf \
python-skimage \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*

# Clone Caffe repo and move into it
RUN cd /opt && git clone https://github.com/BVLC/caffe.git && cd caffe && \
# Copy Makefile
cp Makefile.config.example Makefile.config && \
# Enable CUDNN:
# sed -i 's/# USE_CUDNN/USE_CUDNN/g' Makefile.config && \
# Enable CPU-only
sed -i 's/# CPU_ONLY/CPU_ONLY/g' Makefile.config && \
# # Make
make -j"$(nproc)" all && \
make -j"$(nproc)" pycaffe


# Add to Python path
ENV CAFFE_ROOT=/opt/caffe

ENV PYTHONPATH=$CAFFE_ROOT/python:$PYTHONPATH
ENV PATH=$CAFFE_ROOT/build/tools:$PATH

# Set /workspace as working directory
WORKDIR /workspace

# Set the entrypoint
ENTRYPOINT ["caffe"]

# And the default command line options.
CMD ["--help"]
61 changes: 61 additions & 0 deletions docker/runtime/gpu/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
FROM nvidia/cuda:7.5-cudnn4-devel
MAINTAINER team-sl@zalando.de

# Set up the Caffe build dependencies.
RUN apt-get update \
&& DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \
cmake \
cmake-curses-gui \
git \
ca-certificates \
git \
bc \
build-essential \
libatlas-base-dev \
libatlas-dev \
libboost-all-dev \
libgflags-dev \
libgoogle-glog-dev \
libhdf5-dev \
libleveldb-dev \
liblmdb-dev \
libopenblas-dev \
libopencv-dev \
libprotobuf-dev \
libsnappy-dev \
protobuf-compiler \
python-dev \
python-numpy \
python-pip \
python-protobuf \
python-skimage \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*

# Clone Caffe repo and move into it
RUN cd /opt && git clone https://github.com/BVLC/caffe.git && cd caffe && \
# Copy Makefile
cp Makefile.config.example Makefile.config && \
# Enable CUDNN:
sed -i 's/# USE_CUDNN/USE_CUDNN/g' Makefile.config && \
# Enable CPU-only
# sed -i 's/# CPU_ONLY/CPU_ONLY/g' Makefile.config && \
# Make
make -j"$(nproc)" all && \
make -j"$(nproc)" pycaffe


# Add to Python path
ENV CAFFE_ROOT=/opt/caffe

ENV PYTHONPATH=$CAFFE_ROOT/python:$PYTHONPATH
ENV PATH=$CAFFE_ROOT/build/tools:$PATH

# Set /workspace as working directory
WORKDIR /workspace

# Set the entrypoint
ENTRYPOINT ["caffe"]

# And the default command line options.
CMD ["--help"]
30 changes: 30 additions & 0 deletions docker/start_caffe_docker.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/bin/bash
# A simple script to setup a Caffe build environment. This also ensures that
# the correct user and group are created in the docker container so that caffe
# is not built as root.

### VARIABLES
WHERE_IS_NVIDIA_DOCKER=$(which nvidia-docker)
if [ $? -eq 0 ]
then
echo "Using nvidia-docker"
DOCKER=${DOCKER:-"nvidia-docker"}
else
echo "Using standard docker"
DOCKER=${DOCKER:-"docker"}
fi

DOCKER_IMAGE='caffe:devel'
DOCKER_HOST='caffe_devel'
GROUP_ID=$(id -g)
USER_ID=$(id -u)

CAFFE_ROOT=$(pwd)/..

eval ${DOCKER} run \
-ti \
-h $DOCKER_HOST \
-v $CAFFE_ROOT:$CAFFE_ROOT \
-w $CAFFE_ROOT \
-u $USER_ID:$GROUP_ID \
$DOCKER_IMAGE bash
13 changes: 13 additions & 0 deletions examples/mnist/train_lenet_docker.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env sh
if [ $GPU -ne 1 ]
then
DOCKER_CMD=docker
else
DOCKER_CMD=nvidia-docker
fi

$DOCKER_CMD run --rm -ti \
-u $(id -u):$(id -g) \
-v $(pwd):/workspace \
-w /workspace \
caffe:runtime train --solver=examples/mnist/lenet_solver.prototxt $*

0 comments on commit 6546ca7

Please sign in to comment.