diff --git a/docker/Makefile b/docker/Makefile new file mode 100644 index 00000000000..e78231f25f2 --- /dev/null +++ b/docker/Makefile @@ -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 \ No newline at end of file diff --git a/docker/README.md b/docker/README.md new file mode 100644 index 00000000000..ac30e6c7800 --- /dev/null +++ b/docker/README.md @@ -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```. \ No newline at end of file diff --git a/docker/devel/Dockerfile b/docker/devel/Dockerfile new file mode 100644 index 00000000000..63d1b6734c8 --- /dev/null +++ b/docker/devel/Dockerfile @@ -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 \ No newline at end of file diff --git a/docker/runtime/cpu/Dockerfile b/docker/runtime/cpu/Dockerfile new file mode 100644 index 00000000000..3e9e3285ef0 --- /dev/null +++ b/docker/runtime/cpu/Dockerfile @@ -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"] \ No newline at end of file diff --git a/docker/runtime/gpu/Dockerfile b/docker/runtime/gpu/Dockerfile new file mode 100644 index 00000000000..5cdc5769f43 --- /dev/null +++ b/docker/runtime/gpu/Dockerfile @@ -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"] \ No newline at end of file diff --git a/docker/start_caffe_docker.sh b/docker/start_caffe_docker.sh new file mode 100755 index 00000000000..cba9bd4ab0e --- /dev/null +++ b/docker/start_caffe_docker.sh @@ -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 \ No newline at end of file diff --git a/examples/mnist/train_lenet_docker.sh b/examples/mnist/train_lenet_docker.sh new file mode 100755 index 00000000000..2b7f76b0f19 --- /dev/null +++ b/examples/mnist/train_lenet_docker.sh @@ -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 $*