Docker

Introduction

by

Marek Goldmann

10th Dec 2013

Docker

Docker?

A project to create and manage containers.

Containers?

Yes, a lightweight operating system virtualization.

How this compares to a virtual machine?

It's completely different.

Comparison

Virtual Machine (KVM, VMware)Container (LXC, OpenVZ)
HardwareSimulatedUses it (almost) directly
Supported OS'esAlmost anyOnly Linux
SpaceUser spaceKernel space
SeparationFullControl Groups (cgroups)
Startup timeSeconds to minutesMiliseconds
ScalabilityA fewSky is the limit (thousands)
Custom kernelYesNo
Enterprise features (live migration, etc)YesNo
Ease of creationModerateEasy
Time consumption of creationHighLow
SizeHUGESmall

Who's behind Docker?

dotCloud* and a powerful community.

* dotCloud, Inc. was recently renamed to Docker, Inc.

Speaking of community...

  • GitHub
    • 501 watches
    • 6948 stars
    • 847 forks
    • 406 open issues
    • 69 pull requests
  • Over 200 contributors
    • avg 13 commits / day
    • 92% are external
  • Many meetups all over the world

Achieved after 10 months since first commit to GitHub

Glossary

  • Image: a package with filesystem and data
  • Container: a running image
  • Registry: place where images are hosted/shared

Main concepts

  • An image can be linked to a parent image
  • Image not having a parent is called base image
  • Container has a state, whereas image doesn't
  • You can launch multiple containers from an image

Docker components

Manager

Running, stopping, attaching, etc... containers

Builder

Creating images

Image registry

Storage for images

Creating Docker images

It's simple! Use Dockerfiles.

Dockerfile

A plain text file with instructions that automate building the image.

Dockerfile example


# Base on the Fedora 20 image
FROM mattdm/fedora

# Upgrade the image
RUN yum -y update

# Install java
RUN yum -y install java-1.7.0-openjdk-devel wget

# Get WildFly
RUN wget http://download.jboss.org/wildfly/8.0.0.Beta1/wildfly-8.0.0.Beta1.tar.gz -O /opt/wildfly.tar.gz
RUN tar -xf /opt/wildfly.tar.gz -C /opt

# Run WildFly after container boot
ENTRYPOINT /opt/wildfly-8.0.0.Beta1/bin/standalone.sh -c standalone-ha.xml -b 0.0.0.0
					

This should be saved as Dockerfile

Basic instructions

  • FROM: the image we're basing on / extending
  • RUN: The command to execute at the build time
  • ENTRYPOINT: The command to be executed after the container boot

But there is a lot more!

Building the image

docker build .

Building the image


$ docker build .
Uploading context 10240 bytes
Step 1 : FROM mattdm/fedora

[SNIP]

Step 4 : RUN wget -q http://download.jboss.org/wildfly/8.0.0.Beta1/wildfly-8.0.0.Beta1.tar.gz -O /opt/wildfly.tar.gz
 ---> Running in 41467b97b423
 ---> 112766c23202
Step 5 : RUN tar -xf /opt/wildfly.tar.gz -C /opt
 ---> Running in d07d0c5c3dab
 ---> c53f9f4ceb1e
Step 6 : ENTRYPOINT /opt/wildfly-8.0.0.Beta1/bin/standalone.sh -c standalone-ha.xml -b 0.0.0.0
 ---> Running in a9b2cecf4db5
 ---> 5894688e4f2e
Successfully built 5894688e4f2e
          

We've built image 5894688e4f2e

Modifyng the image

  1. Change the Dockerfile
  2. Execute the build command once again

Let's talk about speed


$ time docker build .
Uploading context 10240 bytes
Step 1 : FROM mattdm/fedora
 ---> 0f3e92b4e94d

[SNIP]

Step 4 : RUN wget -q http://download.jboss.org/wildfly/8.0.0.Beta1/wildfly-8.0.0.Beta1.tar.gz -O /opt/wildfly.tar.gz
 ---> Using cache
 ---> 2ad6655478dd
Step 5 : RUN tar -xf /opt/wildfly.tar.gz -C /opt
 ---> Using cache
 ---> c0f63f06e9ec
Step 6 : RUN touch /etc/something
 ---> Running in 8426dd5570af
 ---> 7ef016a00cde
Step 7 : ENTRYPOINT /opt/wildfly-8.0.0.Beta1/bin/standalone.sh -c standalone-ha.xml -b 0.0.0.0
 ---> Running in 60bfa9d3a746
 ---> e757e0ea05d7
Successfully built e757e0ea05d7

real  0m0.307s
user  0m0.000s
sys 0m0.018s
          

We've built a new image e757e0ea05d7

Yes, 300ms!

How does it work

  1. Prepare to execute an instruction
    • Was the instruction executed before?
      • yes - use the previous result, go to #3
      • no - execute the instruction, go to #2
  2. Was the instruction executed without error?
    • yes - commit, go to #3
    • no - fail the build
  3. Is this the end of instructions?
    • yes - commit, finish!
    • no - go to #1

Let's talk about size


$ docker images

REPOSITORY          TAG                 ID                  CREATED             SIZE
<none>              <none>              e757e0ea05d7        37 minutes ago      12.29 kB (virtual 1.056 GB)
<none>              <none>              5894688e4f2e        46 minutes ago      12.29 kB (virtual 1.056 GB)
					

Yes, 12.29kB!

Image repositories

  1. Private: for example hosted inside a company
  2. Public: index.docker.io

Docker CLI commands

docker pull

Pull image or repository to the registry

docker push

Push image from registry to a repository

docker run

Run a command in a new container based on the specified image

docker stop

Stop a container

docker start

Start a stopped container

docker rm

Remove a stopped container

docker rmi

Remove an image from registry

OK, great, but...

Use-cases?

  • Want your own PaaS?
  • Automated testing and CI
  • Deploying scalable apps
  • Application delivery platform

PaaS

It's (fairly) easy to create a PaaS with Docker

There is even a tutorial to create one in 5 minutes...

OpenShift

https://github.com/openshift/openshift-pep/blob/master/openshift-pep-010-docker-cartridges.md

Continuous Integration

  1. You commit
  2. CI builds the app
  3. CI creates an image with the new app
  4. CI starts a container from new image
  5. Integration tests are executed on a real application
  6. Profit!

Scalable apps

  • Requirement: prepare your app to be easily scalable
  • Starting a container is fast and cheap
  • Extending a (for example WildFly) cluster is easy
  • Profit!

Delivery platform

Build once - run anywhere!

  • Downloading is easy and fast
  • Upgrading images is easy too

Profit!

Where to start?

yum install docker-io

http://docs.docker.io/en/latest/use/basics/

Thanks!

Attributions:

  • https://www.docker.io/