Terry : Docker

Docker - The Linux Container Engine

What is Docker?

Docker is an open-source engine that automates the deployment of any application as a lightweight, portable, self-sufficient container that will run virtually anywhere.

Docker containers can encapsulate any payload, and will run consistently on and between virtually any server. The same container that a developer builds and tests on a laptop will run at scale, in production*, on VMs, bare-metal servers, OpenStack clusters, public instances, or combinations of the above.

Common use cases for Docker include

  • Automating the packaging and deployment of applications
  • Creation of lightweight, private PAAS environments
  • Automated testing and continuous integration/deployment
  • Deploying and scaling web apps, databases and backend services

docker, the Linux Container Runtime, runs Unix processes with strong guarantees of isolation across servers. Your software runs repeatably everywhere because its Container includes any dependencies.

docker runs three ways

  • as a daemon to manage LXC containers on your Linux host (sudo docker -d)
  • as a CLI which talks to the daemon’s REST API (docker run ...)
  • as a client of Repositories that let you share what you’ve built (docker pull, docker commit).

Each use of docker is documented here. The features of Docker are currently in active development, so this documentation will change frequently.

It is like Vagrant for LXC - Linux Container.

Benefits

  • Heterogeneous payloads
    Any combination of binaries, libraries, configuration files, scripts, virtualenvs, jars, gems, tarballs, you name it. No more juggling between domain-specific tools. Docker can deploy and run them all.
  • Any server (Linux)
    Docker can run on any x64 machine with a modern linux kernel - whether it's a laptop, a bare metal server or a VM. This makes it perfect for multi-cloud deployments.
  • Isolation
    Docker isolates processes from each other and from the underlying host, using lightweight containers.
  • Repeatability
    Because each container is isolated in its own filesystem, they behave the same regardless of where, when, and alongside what they run.

Under the hood

Under the hood, Docker is built on the following components:

  • The cgroups and namespacing capabilities of the Linux kernel;
  • aufs - a powerful union filesystem with copy-on-write capabilities;
  • The Go programming language;
  • lxc - a set of convenience scripts to simplify the creation of linux containers.

Notable features

Filesystem isolation: each process container runs in a completely separate root filesystem.

Resource isolation: system resources like cpu and memory can be allocated differently to each process container, using cgroups.

Network isolation: each process container runs in its own network namespace, with a virtual interface and IP address of its own.

Copy-on-write: root filesystems are created using copy-on-write, which makes deployment extremeley fast, memory-cheap and disk-cheap.

Logging: the standard streams (stdout/stderr/stdin) of each process container is collected and logged for real-time or batch retrieval.

Change management: changes to a container's filesystem can be committed into a new image and re-used to create more containers. No templating or manual configuration required.

Interactive shell: docker can allocate a pseudo-tty and attach to the standard input of any container, for example to run a throwaway interactive shell.

Getting Started

Installation

Ubuntu

NOTE: For Ubuntu 12.04, to avoid a bug install the Linux 3.8 kernel (linux-image-lts-raring or linux-current-generic).

1. add Docker official APT Repository - Preferred

# Add the Docker repository key to your local keychain
# using apt-key finger you can check the fingerprint matches 36A1 D786 9245 C895 0F96 6E92 D857 6A8B A88D 21E9
sudo wget https://get.docker.io/gpg -O- | apt-key add -

# Add the Docker repository to your apt sources list.
sudo echo "deb https://get.docker.io/ubuntu docker main" > /etc/apt/sources.list.d/docker.list

# Update your sources
sudo apt-get update

# Install, you will see another warning that the package cannot be authenticated. Confirm install.
sudo apt-get install lxc-docker 

Deprecated - Add the PPA

sudo add-apt-repository ppa:dotcloud/lxc-docker

2. Install

sudo apt-get update && apt-get install lxc-docker

3. Run it (download the base 'ubuntu' container and run bash inside it while setting up an interactive shell)

docker run -i -t ubuntu /bin/bash

Ctrl-D or type exit to quit.

Runtime data directory => /var/lib/docker

Docker With Vagrant

Fetch the docker sources (this includes the Vagrantfile for machine setup) and spin it up

# checkout docker source
git clone https://github.com/dotcloud/docker.git
 
# change directory
cd docker
 
vagrant up

Vagrant will

  • Download the ‘official’ Precise64 base ubuntu virtual machine image from vagrantup.com
  • Boot this image in virtualbox
  • Add the Docker PPA sources to /etc/apt/sources.lst
  • Update your sources
  • Install lxc-docker

You now have a Ubuntu Virtual Machine running with docker pre-installed.

NOTE: Ctrl-D or type exit to quit the shell.

Hello World

This is the most basic example available for using Docker.

Download the base image (named “ubuntu”):

# Download an ubuntu image
sudo docker pull ubuntu

Alternatively to the ubuntu image, you can select busybox, a bare minimal Linux system. The images are retrieved from the Docker repository.

#run a simple echo command, that will echo hello world back to the console over standard out.
sudo docker run ubuntu /bin/echo hello world

Explanation

  • "sudo" - super user do - execute the following commands as user root
  • "docker run" run a command in a new container
  • "ubuntu" is the image we want to run the command inside of.
  • "/bin/echo" is the command we want to run in the container
  • "hello world" is the input for the echo command

Hello World Daemon

This example assumes you have Docker installed and the Ubuntu image already imported with docker pull ubuntu. We will use the Ubuntu image to run a simple hello world daemon that will just print hello world to standard out every second. It will continue to do this until we stop it.

Steps

CONTAINER_ID=$(sudo docker run -d ubuntu /bin/sh -c "while true; do echo hello world; sleep 1; done")

We are going to run a simple hello world daemon in a new container made from the ubuntu image.

  • "docker run -d " run a command in a new container. We pass "-d" so it runs as a daemon.
  • "ubuntu" is the image we want to run the command inside of.
  • "/bin/sh -c"is the command we want to run in the container
  • "while true; do echo hello world; sleep 1; done"is the mini script we want to run, that will just print hello world once a second until we stop it.
  • $CONTAINER_ID the output of the run command will return a container id, we can use in future commands to see what is going on with this process.

Check the logs make sure it is working correctly

sudo docker logs $CONTAINER_ID

"docker logs" This will return the logs for a container

$CONTAINER_ID The Id of the container we want the logs for.

Attach to the container to see the results in realtime

sudo docker attach $CONTAINER_ID

"docker attach" This will allow us to attach to a background process to see what is going on.
$CONTAINER_ID The Id of the container we want to attach too.

Exit from the container attachment by pressing Control-C.

Check the process list to make sure it is running

sudo docker ps 

"docker ps" this shows all running process managed by docker.

Stop the container

sudo docker stop $CONTAINER_ID

"docker stop" This stops a container
$CONTAINER_ID The Id of the container we want to stop.

Reference

https://github.com/dotcloud/docker/

Documentation