Getting Started with Docker


Docker logoWritten by Andrew Colin Kissa

Introduction
We’re happy to announce the availability of Docker support on our VPS installations.
In this blog post, we aim to introduce you to Docker one of
the most exciting and powerful open-source projects that has sprung up in the recent
years.

In a nutshell, Docker offers you the tools to package everything that forms an
application, allowing you to deploy the application effortless across systems and
machines both virtual and physical.

Just as Java was write once run anywhere, Docker allows you to setup once and
deploy anywhere.

The Docker project uses mostly existing Linux functionality mainly, LXC (Linux containers), device-mapper, aufs (a union file system).

Docker uses

https://registry.hub.docker.com/  lets you browse popular docker images.  The most popular ones are ready-to-use development environments: a tomcat, mysql, memcached, android software development, ssh-able base centos and ubuntu images.

These Docker images make docker very attractive for developers wanting to have a quick play with new technologies.

Docker is also a great option for developers wanting to build an easily deployable application.  For example, see the wordpress docker below!

How it works

The main parts of the Docker system are:

  • Docker daemon – This manages the LXC containers on a host
  • Docker client – This is used to interact with the Docker daemon
  • Docker Index – This is a repository of Docker images

The above tools are used to create the Docker applications which consist of:

  • Docker containers – These are directories containing everything needed to run
  • an application.
  • Dockerfiles – Scripts for automating the building of Docker applications
  • Docker images – Snapshots of Docker applications or Base Operating Systems

Features

So why is every one raving about Docker ? The short answer is:

Docker is light weight, allows you to port applications across systems and
hardware with ease while containing those applications and running them in
their own secure sandboxed environments

Ease of use

The whole process of porting applications is based on Docker containers which
as just simple directories that can be copied across to different systems or
machines.

Light weight

Docker reuses mostly existing kernel functionality and Linux libraries, simply
glueing the existing tools. Docker makes use of LXC technology which provides
for virtualisation without the overhead of other virtualisation technologies.

Security

Using LXC Docker is able to contain applications limiting resources, not allowing
access beyond their own file-system as well as process isolation.

Portability

By using Docker containers, applications are able to work anywhere they are
deployed to.

Incremental development

Because it is filesystem based it is easy to take snapshots, perform rollbacks
similar to using a version control system

A lot more can be said about Docker, to get the full low down refer to the
Docker documentation

Installing

To get started with using Docker on your Rimu VPS, you need to change your
kernel to 3.12.27 or later which will be ‘Docker’-enabled.

Ubuntu

Docker is supported on the following versions of Ubuntu:

  • Ubuntu Trusty 14.04 (LTS) (64-bit)
  • Ubuntu Precise 12.04 (LTS) (64-bit)

Installation

apt-get update
apt-get install docker.io lxc
ln -sf /usr/bin/docker.io /usr/local/bin/docker
sed -i '$acomplete -F _docker docker' /etc/bash_completion.d/docker.io

Centos

Docker is supported on the following versions of Centos:

  • CentOS-7 (64-bit)

Installation

yum install docker
yum install docker-io lxc
service docker start
chkconfig docker on

Debian

Docker would need to be manually installed on Debian 7.0 Wheezy (64-bit)

Installation

Follow the Ubuntu 12.04 instructions per http://docs.docker.com/installation/ubuntulinux/#ubuntu-precise-1204-lts-64-bit

Using with Docker

All interaction with the Docker system is via the *docker* command this
command needs to be run as a privileged user or using sudo.

Available commands

To see the list of all available docker commands run:

docker

This should return:

Usage: docker [OPTIONS] COMMAND [arg...]
 -H=[unix:///var/run/docker.sock]: tcp://host:port to bind/connect to or unix://path/to/socket to use

A self-sufficient runtime for linux containers.

Commands:
    attach    Attach to a running container
    build     Build an image from a Dockerfile
    commit    Create a new image from a container's changes
    cp        Copy files/folders from a container's filesystem to the host path
    diff      Inspect changes on a container's filesystem
    events    Get real time events from the server
    export    Stream the contents of a container as a tar archive
    history   Show the history of an image
    images    List images
    import    Create a new filesystem image from the contents of a tarball
    info      Display system-wide information
    inspect   Return low-level information on a container
    kill      Kill a running container
    load      Load an image from a tar archive
    login     Register or log in to a Docker registry server
    logout    Log out from a Docker registry server
    logs      Fetch the logs of a container
    port      Lookup the public-facing port that is NAT-ed to PRIVATE_PORT
    pause     Pause all processes within a container
    ps        List containers
    pull      Pull an image or a repository from a Docker registry server
    push      Push an image or a repository to a Docker registry server
    restart   Restart a running container
    rm        Remove one or more containers
    rmi       Remove one or more images
    run       Run a command in a new container
    save      Save an image to a tar archive
    search    Search for an image on the Docker Hub
    start     Start a stopped container
    stop      Stop a running container
    tag       Tag an image into a repository
    top       Lookup the running processes of a container
    unpause   Unpause a paused container
    version   Show the Docker version information
    wait      Block until a container stops, then print its exit code

Search for images

Searching for a docker image:

docker search ubuntu

Downloading an image:

To download an image from a Docker index

docker pull ubuntu

Listing images:

To list all the images on your system

docker images

Listing Docker containers

To list all running Docker containers

docker ps

To list all running and non-running Docker containers

docker ps -l

Starting Docker containers

To start a Docker container

docker run [image name] [command to run]
docker run ubuntu /bin/bash

Stopping a Docker container

To stop a Docker container

docker stop [container ID]
docker stop c4ff7513909d

Example Docker Application

The CentOS Project provides a number of sample Dockerfiles which you may use
either as templates or as examples to teach yourself Docker. These Dockerfiles
are available on github

For this example i will be using the WordPress Application for Centos6.

To begin we need to clone the git repository by running the following:

cd /usr/local/src
git clone https://github.com/CentOS/CentOS-Dockerfiles.git

Change to the directory containing the Dockerfile for Centos6

cd CentOS-Dockerfiles/wordpress/centos6/

Build the Docker image, replace ** with your username

docker build -rm -t /wordpress:centos6 .

The above command will run several commands in the DockerFile to setup
a working WordPress site for you, to see the commands run look at the
Dockerfile inside the directory or online

Now that you have a Docker image you need to run the image:

CID=$(docker run -d -p 80 -p 22 /wordpress:centos6)

The variable $CID contains the image’s ID which we use later, now that
our image is running lets get the mysql and wordpress passwords by looking
at the logs.

echo "$(docker logs $CID | grep password)"

Now we can find the port mapped on the host to the port 80 in the application
by running the following command.

docker port $CID 80

In my case the port is 49156 so i can now open the browser and connect to the
host IP address on that port, On connection i get the wordpress install page
i can now fill in the form and finalise the setup.

Thats it you have WordPress running in no time