Docker

My guide on building and using docker images.

PYMC3:

The following image I use for all my work involving PYMC3.

The repository with all source files can be found at bitbucket.

Dockerfile:

The Dockerfile lays out how the requirements for the image.

FROM ubuntu

#Set the working directory to /files
WORKDIR /files

#Add files
ADD . /files

#Install any needed packages specified in requirements.txt
RUN apt-get update
RUN apt-get install --assume-yes python3
RUN apt-get install --assume-yes python3-pip libblas-dev

#Add git repos
RUN apt-get install --assume-yes git
RUN git clone https://github.com/ABartels13/HelloJupyterStan.git

#Install requirements
RUN apt-get install --assume-yes build-essential libxml2-dev libcurl4-openssl-dev libssl-dev
RUN pip3 install --trusted-host pypi.python.org -r requirements.txt

# Make port 8888 available to the world outside this container
EXPOSE 8888

#Run a command
CMD ["jupyter", "lab","--ip=0.0.0.0","--port=8888","--allow-root"]

This is saved as Dockerfile in the root of the repository. Features are the folder syncing from a GitHub repository specified. Note that port 8888 is exposed for JupyterHub.

Jupyter Lab is set to be running by the final CMD line.

Pipeline:

The pipeline file tells BitBucket what image to build and where to put it after.

# This is a sample build configuration for Docker.
# Check our guides at https://confluence.atlassian.com/x/O1toN for more examples.
# Only use spaces to indent your .yml configuration.
# -----
# You can specify a custom docker image from Docker Hub as your build environment.
# image: atlassian/default-image:latest

pipelines:
  default:
    - step:
        services:
          - docker
        script: # Modify the commands below to build your repository.
          # Set $DOCKER_HUB_USERNAME and $DOCKER_HUB_PASSWORD as environment variables in repository settings
          - export IMAGE_NAME=alainbartels/pymc3:latest

          # build the Docker image (this will use the Dockerfile in the root of the repo)
          - docker build -t $IMAGE_NAME .
          # authenticate with the Docker Hub registry
          - docker login --username $DOCKER_HUB_USERNAME --password $DOCKER_HUB_PASSWORD
          # push the new Docker image to the Docker registry
          - docker push $IMAGE_NAME

This file is saved as bitbucket-pipelines.yml, uses the default for all variables. Note that DOCKER_HUB_USERNAME and DOCKER_HUB_PASSWORD need to be set from the settings menu

Python Requirements:

The requirements.txt tells pip what packages to install.

jupyterlab
matplotlib
numpy
pymc3
scipy
sklearn
seaborn

 

Running Container:

I can then run the container using the following command:

docker run -p 9000:8888 -v C:\:/files/C alainbartels/pymc3:latest

Note:

  • -p redirects port 8888 from the container to 9000 on my local machine
  • -v mounts my local C drive to /files/C in the container