Introduction to Docker

Objectives

  • Why we need Docker 
  • Install Docker
  • Run a container
  • Manage your images and containers
  • Create a Docker image
  • Share your Docker image

What is Docker

  •  Software that helps us isolate a computing environment
  • Called a "kernel level virtualization system"
     

Kernel

Applications

Hardware

Docker (fast)

Virtual Machines (slow)

Install Docker now

1. Create a DockerHub account (hub.docker.com)

2. Docker Desktop for Mac

    Docker Desktop for Windows

    Docker for Linux

Why isolate a computing environment?

  • For the good of science
  • For yourself
  • For your friends
  • For the cloud
  • For your future

Isolate a computing environment for reproducibility

  • Our software environments are complex
    • make it easy for others to redo the analysis for your paper
  • Imagine a world where you:
    • Read a paper, want to build on it
    • You easily run their entire analysis with their data
    • You run it with your data
    • You build on it, improve it, and discover something new

Isolate a computing environment to work efficiently

  • You could redo the analysis for your paper, years later
  • Free to experiment:
    • Installing other software doesn't break other software on your computer
  • Save time!

Isolate a computing environment to collaborate

  • Avoid "Well, this works on my computer"
  • Share environment with your collaborator
  • Even works on Windows!
  • Save time

Isolate a computing environment to use the cloud

  • Our datasets are getting bigger, analysis more complex
  • Sometimes local hardware doesn't cut it
  • Cloud machines are blank slates: you need to set up an environment anyway

Isolate a computing environment to get a job

  • Great for your resume!
  • Science needs Docker (maybe not everyone knows it)
  • Industry knows they need it

Objectives

  • Why we need Docker 
  • Install Docker
  • Run a container
  • Manage your images and containers
  • Create a Docker image
  • Share your Docker image

Intro to

Images & Containers

  • Image: a blueprint of an environment
  • Container: an instance of an image

 

you can have many running containers of an image

Pull a Docker image

$ docker pull hello-world

in your terminal

Run a Docker image

$ docker run hello-world

in your terminal

Run a Docker image

$ docker run -it ubuntu bash

in your terminal

give us an interactive terminal

name of image

commandto run

Run a Docker image

$ docker run ubuntu echo 'hello world'

in your terminal

name of image

commandto run

inputs

Objectives

  • Why we need Docker 
  • Install Docker
  • Run a container
  • Manage your images & containers
  • Create a Docker image
  • Share your Docker image

Manage your images and containers

$ docker ps

in your terminal

Shows your currently running containers

Manage your images and containers

$ docker ps -a

in your terminal

Shows your currently running and stopped containers

$ docker start -i <container_name> 

to restart (interactively) a stopped container

Manage your images and containers

$ docker stop <container_id/name>

in your terminal

Stops a running container

$ docker rm <container_id/name> 

to remove a stopped container

List your images

$ docker images

in your terminal

Shows the images stored on your computer.

 

Pay attention to size!

List your images

$ docker rmi <image_id>

in your terminal

removes an image

Commands so far

$ docker pull <image_name>
$ docker run -it <image_name> <commands> <args>
$ docker ps -a
$ docker images
$ docker stop/rm <container_name/id>
$ docker rmi <image_name/id>

Container management trick

$ docker run -it --rm <image_name> <commands> <args>

when the container stops, automatically delete it

Objectives

  • Why we need Docker 
  • Install Docker
  • Run a container
  • Manage your images and containers
  • Create a Docker image
  • Share your Docker image

Build an image

$ mkdir myProject

in your terminal, create a new folder

create a file named Dockerfile in that folder and open it with a text editor

Dockerfile

FROM python:3.6

 

RUN pip install jupyterlab

in your text editor

Build your image

$ docker build -t <image_name> .

in your terminal, in your folder

$ docker build -t my_image .

Run your container

in your terminal, in your folder

$  docker run -p 8888:8888 -ti --rm my_image jupyter lab --ip 0.0.0.0 --port=8888 --allow-root

then go http://127.0.0.1:8888/lab in your web browser

adding your local filesystem

in your terminal, in your folder

$ docker run -p 8888:8888 -ti --rm -v $PWD:/myworkspace

-w /myworkspace my_image:latest jupyter lab --ip 0.0.0.0 --port=8888 --allow-root

-v mounts a directory from you local computer to a location in the container

-w sets the working directory

 

Exercise

Install dipy to your Docker image

$ pip install dipy

Hint: normally we'd do:

Adding files to your image

In your Dockerfile

FROM python:3.6

 

RUN pip install jupyterlab

RUN pip install dipy

 

ADD ./myscript.py /myscript.py

ENTRYPOINT ["python", "/myscript.py"]

print('hello human')

Create a file called myscript.py

Objectives

  • Why we need Docker 
  • Install Docker
  • Run a container
  • Manage your containers and images
  • Create a Docker image
  • Share your Docker image

Sharing your image on Dockerhub

$ docker login

$ docker build -t <username>/<image_name>:<version> .

$ docker push <username>/<image_name>:<version>

Things to note:

Neuroimaging + Docker

  • Instead of building your neuroimaging docker image from scratch, take a shortcut:

 

https://github.com/kaczmarj/neurodocker​

 

generates Docker and Singularity images!

More Resources

Conclusions

  • Docker takes time and practice
    • Still, its worth doing! For Science!
  • We only scratched the surface today
  • There are many resources and shortcuts we can take
  • Will be very useful during the hackweek and beyond!

Questions?

Introduction to Docker

By Anisha Keshavan

Introduction to Docker

  • 2,545