A Simple Introduction to Docker II

What is Docker?

  • Docker is a container implementation written in Go language.
  • It's the most popular container implementations nowadays.
  • It has the largest developer community out there, which means you can find the best documentations here.

We now use docker as a example to illustrate how container works.

Typical Use Cases

  • Development Environments Configuration
  • Build Microservices
  • Distributed System
    • Docker Swarm

      Recently, Docker 1.9 was released. And a feature named Docker Swarm got out of beta and is ready for production usages. We will give it a brief introduction here.

    • Swarm in general
      • Turns a set of Docker Engines into a single pool of resources
      • Supports the Docker RESTful API (99%)
      • Resource Management (CPU, Memory, Networking)
      • Advanced scheduling with constraints and affinities
      • Multiple Discovery Backends (hub, etcd, consul, zookeeper)
      • TLS: Encryption and Authentication
      • Multi Tenancy / Leader Election
    • Usages
      • Build a overlay for a pool of Docker Engines
      • Combine thest Docker Engines into a single virtual Engine
      • Use this engine as a normal Docker engine

        Since they have almost the same API

      • Provide distributed computing or data storage
      • Swarm is ready for running production apps

        In Docker's tests, Swarm can run with 1,000 nodes and 30,000 containers on EC2 and it keeps on scheduling containers in less than half a second.

How popular is Docker?

First of all, let's see how popular is docker nowadays.

A Quick Peek at Real Docker Adoption

Here are the results of a survey from a monitoring startup Datadog.1

  • Real Docker Adoption Is Up 5x in One Year
  • Docker Now Runs on 6% of the Hosts We Monitor
  • Larger Companies Are the Early Adopters
  • 2/3 of Companies That Try Docker Adopt It
  • Users Triple the Number of Containers They Use within 5 Months
  • The Most Widely Used Images Are Registry, NGINX, and Redis
  • Docker Hosts Often Run Four Containers at a Time
  • VMs Live 4x Longer Than Containers

From this survey, we can know that docker is growing really fast in the last year. And it's widely used to provide web services.

Real World Applications

  • Used widely in productions
    • Google

      Indeed, few of you know it, but most of you have been using containers for years. Google has its own open-source, container technology lmctfy (Let Me Contain That For You). Anytime you use some of Google functionality — Search, Gmail, Google Docs, whatever — you're issued a new container.2

    • Baidu
    • Uber
    • PayPal
  • Paas
  • Personal projects using docker to diliver

Docker can even run on ARM platforms

How to use Docker?

Docker has a really good documentation. You can find almost anything you need to know about how to use Docker at Docker Docs. Here, we will use a example from this doc to show you some basic Docker commands.

Using Docker in China

I suggest to use a docker hub mirror in china to download docker images. Otherwise you can hardly download an image file. You can find some information about the docker hub mirror here.

Download and Run a Image

docker run docker/whalesay cowsay foo bar
 _________
< foo bar >
 ---------
     \
     \
       \
 #        .
 # ## ##       ==
 # ## ## ##      ===
       /""""""""""""""""___/ ===
   ~~~ {~~ ~~~~ ~~~ ~~~~ ~~ ~ /  ===- ~~~
       \______ o          __/
         \    \        __/
           \____\______/

Build a Custom Image

  • Dockerfile Docker can build images automatically by reading the instructions from a Dockerfile, a text file that contains all the commands, in order, needed to build a given image. You can read more about Dockerfile here.
    • Example

      FROM nginx
      # RUN apt-get -y update && apt-get install -y xxx
      COPY *.html /usr/share/nginx/html/
      
    • Build a image using Dockerfile

      docker build -t nginx-test .
      
    • Use this image

      docker run --name simple_html -d -p 80:80 -p 443:443 nginx_test