Skip to content

Instantly share code, notes, and snippets.

@rgbkrk
Last active September 23, 2017 01:53
Show Gist options
  • Save rgbkrk/8050502 to your computer and use it in GitHub Desktop.
Save rgbkrk/8050502 to your computer and use it in GitHub Desktop.
nbviewer in Docker
# Using the Ubuntu image
FROM ubuntu
# Make sure apt is up to date
RUN echo "deb http://archive.ubuntu.com/ubuntu precise main universe" > /etc/apt/sources.list
RUN apt-get update
RUN apt-get upgrade -y
# Not essential, but wise to set the lang
RUN apt-get install -y language-pack-en
ENV LANGUAGE en_US.UTF-8
ENV LANG en_US.UTF-8
ENV LC_ALL en_US.UTF-8
RUN locale-gen en_US.UTF-8
RUN dpkg-reconfigure locales
# Python binary dependencies, developer tools
RUN apt-get install -y python python-dev build-essential wget curl make gcc zlib1g-dev git
# Install setuptools
RUN wget https://bitbucket.org/pypa/setuptools/raw/bootstrap/ez_setup.py -O - | python2.7
# Install pip
RUN curl --show-error --retry 5 https://raw.github.com/pypa/pip/master/contrib/get-pip.py | python2.7
# nbviewer binary dependencies
RUN apt-get install -y libxml2-dev libzmq-dev sqlite3 libsqlite3-dev pandoc libevent-dev libcurl4-gnutls-dev libmemcached-dev
RUN mkdir -p /srv
# ENV variables set here are kind of silly, in that they would have to be edited here to modify the deployed version of nbviewer
ENV NBVIEWER_REPO https://github.com/ipython/nbviewer.git
ENV NBVIEWER_REF master
RUN git clone $NBVIEWER_REPO /srv/nbviewer && cd /srv/nbviewer && git checkout $NBVIEWER_BRANCH
RUN pip install -r /srv/nbviewer/requirements.txt
WORKDIR /srv/nbviewer
EXPOSE 8080
ENV NBVIEWER_THREADS 2
# To change the number of threads use
# docker run -d -p 80:8080 nbviewer -e NBVIEWER_THREADS=4
ENTRYPOINT python -m nbviewer --port=8080 --threads=$NBVIEWER_THREADS

Setup Docker

In case you don't have Docker setup, here's the set of commands that will bootstrap Docker for Ubuntu (Note: please use Ubuntu 13.04+).

sh -c "wget -qO- https://get.docker.io/gpg | apt-key add -"
sudo sh -c "echo deb http://get.docker.io/ubuntu docker main > /etc/apt/sources.list.d/docker.list"
apt-get update
apt-get upgrade -y
apt-get install -y lxc-docker

Put nbviewer into a container now

Lets imagine you downloaded the Dockerfile to the same directory you are currently in

# Build the Dockerfile and tag it as nbviewer
docker build -t nbviewer .

Now let's setup nbviewer so it is being served on port 80 on our host machine, running detached (no stdout).

# Bind nbviewer on port 8080 of the container to TCP port 80 on all available interfaces of the host machine.
# Run the container in detached mode (no stdout)

docker run -d -p 80:8080 nbviewer

If we want nbviewer to freely dump to stdout, we can turn off detached mode.

# Bind nbviewer on port 8080 of the container to TCP port 80 on all available interfaces of the host machine.

docker run -p 80:8080 nbviewer

The issue with this right now though is that the current shell can't be killed with a simple Ctrl-C (on the current Docker release).

To terminate the running instance in another terminal on the same box, show all the running containers

docker ps

then kill the container by its ID

docker kill <container_id>

Example termination

root@docking:~# docker ps
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS              PORTS
b5c81195c9ce        nbviewer:latest     /bin/sh -c python -m   2 minutes ago       Up 2 minutes        0.0.0.0:80->8080/tcp
root@docking:~# docker kill b5
b5
root@docking:~# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment