Monday, September 28, 2015

Docker for Bioinformatics and Genetics - Part 2

Docker Tutorial Part 2: Basci Commands and Running Rstudio Server with Docker

1. Basic docker commands

  • docker ps shows curretently running container
  • docker ps -a
    This command will show all containers available in the system. You will see there are quite a number of containers accuumlated. 
psytky03@ubuntu:~$ docker ps -a 
CONTAINER ID        IMAGE                   COMMAND                 
73af7edafa08        psytky03/eigandplink    "plink --file data/to"  
5d37644676f5        psytky03/eigandplink    "plink --file data/to"  
f85336ec937f        psytky03/eigandplink    "eigenstrat"            
0bd8bff7af23        psytky03/eigandplink    "bash"                  
08f6a6f41583        psytky03/eigandplink    "bash"                  
c3e1e1645e1a        psytky03/eigandplink    "plink --help"          
cc592b883668        psytky03/eigandplink    "plink -v"              
f9873e947389        ubuntu                  "bash"                  
43c5be566760        hello-world             "/hello"               
*docker rm CONTAINER_ID to delete unused constainer
psytky03@ubuntu:~$ docker rm 73af7edafa08 
  • docker run -rm to automatically delete the container after it exited 
psytky03@ubuntu:~$ docker run -ti --rm psytky03/eigandplink /bin/bash
root@44914cfcfd57:/# exit

#Now let's try to delete this container with id 44914cfcfd57

psytky03@ubuntu:~$ docker rm 44914cfcfd57
Error response from daemon: no such id: 44914cfcfd57
Error: failed to remove containers: [44914cfcfd57]
  • docker images to show all images in the system
psytky03@ubuntu:~$ docker images
REPOSITORY             TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
psytky03/eigandplink   latest              6fd94c3b6e6e        38 hours ago        392.7 MB
ubuntu                 latest              91e54dfb1179        5 weeks ago         188.4 MB
hello-world            latest              af340544ed62        7 weeks ago         960 B
  • docker rmi IMAGE_ID to delete the image. Noticed that if a container is created fromt the image, need to either delete the container or force delete.
psytky03@ubuntu:~$ docker rmi af340544ed62
Error response from daemon: Conflict, cannot delete af340544ed62 because the container 43c5be566760 is using it, use -f to force
Error: failed to remove images: [af340544ed62]

psytky03@ubuntu:~$ docker rm 43c5be566760

psytky03@ubuntu:~$ docker rmi af340544ed62
Untagged: hello-world:latest
Deleted: af340544ed62de0680f441c71fa1a80cb084678fed42bae393e543faea3a572c
Deleted: 535020c3e8add9d6bb06e5ac15a261e73d9b213d62fb2c14d752b8e189b2b912
  • docker pull to fetch the image from the dockerhub. 

2. Pull Rstudio Server image from DockerHub

The Rstudio server image can be fetched from Dockerhub at this link rocker/rstudio.

Since rocker/rstudio is not avilable in this linux system now, if we use docker run rocker/rstudio, docker will by default look for this image in dockerhub and download it this image can be found. 
Here we use docker pull to fetch it first
docker pull rocker/rstudio
psytky03@ubuntu:~$ docker pull rocker/rstudio
Using default tag: latest
latest: Pulling from rocker/rstudio

acdec9ec413b: Pull complete 
37cbf6c3413f: Pull complete 
7fc523e9982e: Pull complete 
359b980a72ed: Pull complete 
01c4f8f74e82: Pull complete 
46ac2c9f1a72: Pull complete 
2c218622f9d2: Pull complete 
a01f119d835b: Pull complete 
a5688ed074d1: Pull complete 
1e631da06610: Pull complete 
27866ec542bc: Pull complete 
ac88bcc53e01: Pull complete 
061029545a15: Pull complete 
be5e2022c62b: Pull complete 
776ce8e1ace5: Pull complete 
5608458f8ff4: Pull complete 
0948d1536ce1: Pull complete 
88b0e66f47b7: Pull complete 
62c92c006919: Pull complete 
20c2eeb79e8b: Pull complete 
7b226db5341a: Pull complete 
88c331104d7a: Pull complete 
eafb6b5866c1: Pull complete 
e19a7c84d5f1: Pull complete 
Digest: sha256:488f7c427a970b1d11424e8c8a01269e132d41ee1913a6bfadb97c4105fc2719
Status: Downloaded newer image for rocker/rstudio:latest
Confirm it with docker images
psytky03@ubuntu:~$ docker images
REPOSITORY             TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
psytky03/eigandplink   latest              6fd94c3b6e6e        39 hours ago        392.7 MB
rocker/rstudio         latest              e19a7c84d5f1        4 days ago          1.343 GB
ubuntu                 latest              91e54dfb1179        5 weeks ago         188.4 MB

3. Run Rstudio Server

Here we use docker run -d -p 8888:8787 rocker/rstudio to create a container and execute it.
The -d indicate we run it in background mode like a web application.

The -p tells docker how we are going to map port between the host and container. Rstudio Server use 8787 as its default port and here we map it to 8888 in the host. 
psytky03@ubuntu:~$ docker run -d -p 8888:8787 rocker/rstudio
Have a check of the host Ip address at eth0, my IP is
Type the address in the web browser of the Mac and you will see this login window
log in the system
The username and password are both rstudio
Now let's install ggplot2 package
ggplot(diamonds, aes(clarity, fill=cut)) + geom_bar()
awesome, seems everything is running great.
Now let's stop this container using docker stopcommand, but first we need to get the container ID with docker ps
psytky03@ubuntu:~$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             
0d1a38402d5e        rocker/rstudio      "/usr/bin/supervisord"   20 minutes ago      

psytky03@ubuntu:~$ docker stop 0d1a38402d5e
Start it again
psytky03@ubuntu:~$ docker start 0d1a38402d5e
Everything is back again. If we want have the ggplot2 library installed in the image file. we need to use the commit command. 
To add the container into the image with docker commit command
psytky03@ubuntu:~$ docker commit -m "add ggplot2" 0d1a38402d5e rocker/rstudio
It is also possible to show all the intermediate layer information of this image. Similar to snapshot of virtual machine, we can run container from any state by telling docker the image ID. 
psytky03@ubuntu:~$  docker history rocker/rstudio
IMAGE               CREATED             CREATED BY                                    
28024380329a        2 minutes ago       /usr/bin/supervisord -c /etc/supervisor/conf. 
e19a7c84d5f1        4 days ago          /bin/sh -c #(nop) CMD ["/usr/bin/supervisord" 
eafb6b5866c1        4 days ago          /bin/sh -c #(nop) EXPOSE 8787/tcp             
88c331104d7a        4 days ago          /bin/sh -c mkdir -p /var/log/supervisor   &&  
7b226db5341a        4 days ago          /bin/sh -c #(nop) COPY file:8294dfdc8c50662db 
20c2eeb79e8b        4 days ago          /bin/sh -c #(nop) COPY file:df4e748b71577b0ec 
62c92c006919        4 days ago          /bin/sh -c #(nop) COPY file:674055700f4c6fbee 
88b0e66f47b7        4 days ago          /bin/sh -c usermod -l rstudio docker   && use 
0948d1536ce1        4 days ago          /bin/sh -c echo '\n\n# Configure httr to perf 
5608458f8ff4        4 days ago          /bin/sh -c rm -rf /var/lib/apt/lists/   && ap 
776ce8e1ace5        4 days ago          /bin/sh -c #(nop) ENV LANG=en_US.UTF-8        
be5e2022c62b        4 days ago          /bin/sh -c #(nop) ENV PATH=/usr/lib/rstudio-s 
061029545a15        4 days ago          /bin/sh -c #(nop) MAINTAINER "Carl Boettiger  
ac88bcc53e01        2 weeks ago         /bin/sh -c #(nop) CMD ["R"]                   
27866ec542bc        2 weeks ago         /bin/sh -c apt-get update                          
1e631da06610        2 weeks ago         /bin/sh -c #(nop) ENV R_BASE_VERSION=3.2.2    
a5688ed074d1        2 weeks ago         /bin/sh -c echo "deb 
a01f119d835b        2 weeks ago         /bin/sh -c #(nop) ENV LANG=en_US.UTF-8        
2c218622f9d2        2 weeks ago         /bin/sh -c #(nop) ENV LC_ALL=en_US.UTF-8      
46ac2c9f1a72        2 weeks ago         /bin/sh -c echo "en_US.UTF-8 UTF-8" >> /etc/l 
01c4f8f74e82        2 weeks ago         /bin/sh -c apt-get update                          
359b980a72ed        2 weeks ago         /bin/sh -c useradd docker                          
7fc523e9982e        2 weeks ago         /bin/sh -c #(nop) MAINTAINER "Carl Boettiger  
37cbf6c3413f        2 weeks ago         /bin/sh -c #(nop) CMD ["/bin/bash"]           
acdec9ec413b        2 weeks ago         /bin/sh -c #(nop) ADD file:9101a1e6d928e77435