Hands on Docker

Jed Cunningham - Thousand Oaks Perl Mongers

November 18, 2015

package an application with all of its dependencies

Docker Engine

  • "lightweight and powerful open source container virtualization technology"
  • Based on libcontainer, which uses cgroups and namespacing

Virtual Machines

VM (source docker.org)

Source: What is Docker?


Docker (source docker.org)

Source: What is Docker?

Docker Hub

  • Registry hosted by Docker
  • Has official images maintained by author OR community

Docker architecture

  • Client-Server
  • Daemon can be remote
Docker architecture (source docker.org)

Source: Understanding Docker


  • Images - Read-only containing OS and some software
  • Dockerfile - Instructions on how to build an image
  • Containers - Isolated instance of an image

First Container

$ docker run --rm -ti debian

Official Perl Image

  • 5.22 and 5.20
  • Debian Based
  • 656 MB
  • Comes with cpanm

First Image

          FROM perl:5.22
          RUN cpanm Mojolicious
$ docker build -t mojolicious .

Random Generator

Simple web app that generates n random characters

Source code


          FROM mojolicious
          COPY random-generator /app/random-generator
          CMD ["/usr/local/bin/hypnotoad", "-f", "/app/random-generator"]
$ docker build -t random-generator .

Lets run it

$ docker run --rm -p 8080:8080 random-generator


Simple web app that uses file storage

Source code


          FROM perl:5.22

          COPY cpanfile /app/cpanfile
          COPY script/mojopaste /app/mojopaste

          RUN cpanm --installdeps /app

          ENV PASTE_DIR /app/pastes

          CMD ["/app/mojopaste", "daemon"]
$ docker build -t mojopaste .

Lets run it

$ docker run -p 3000:3000 --name mypaste -d mojopaste

Data Volumes

We want to persist our data outside the container.
Our options include:

  • Volume
  • Host directory
  • Data Volume Container


$ docker run -v /app/pastes -p 3000:3000 \
--name mypaste -d mojopaste
$ docker inspect mypaste

Easy mode

$ docker run -v $(pwd)/pastes:/app/pastes -p 3000:3000 \
--name mypaste2 -d mojopaste

Data Volume Container

$ docker run -v /app/pastes --name paste-data debian /bin/true
$ docker run --volumes-from paste-data -p 3000:3000 \
--name mypaste3 -d mojopaste

Linking containers

Chat app

Source code

*No Dockerfile*

Start postgres container

$ docker run --name chat-postgres -e POSTGRES_PASSWORD=mysecretpassword \
-d postgres

Start app container

$ docker run --rm -ti --link chat-postgres:postgres \
-v $(pwd):/app -p 3000:3000 perl bash 

Next, install dependencies and start the app:

$ cpanm Mojo::Pg
$ morbo /app/chat.pl

Image portability

  • Self hosted registry
  • save/load with tars