2015-06-07 23:07:20 -04:00
|
|
|
<!--[metadata]>
|
|
|
|
+++
|
2016-01-23 23:36:40 -05:00
|
|
|
aliases = ["/introduction/understanding-docker/"]
|
2015-06-07 23:07:20 -04:00
|
|
|
title = "Understand the architecture"
|
|
|
|
description = "Docker explained in depth"
|
|
|
|
keywords = ["docker, introduction, documentation, about, technology, understanding"]
|
|
|
|
[menu.main]
|
2016-01-23 23:36:40 -05:00
|
|
|
parent = "engine_use"
|
|
|
|
weight = -82
|
2015-06-07 23:07:20 -04:00
|
|
|
+++
|
|
|
|
<![end-metadata]-->
|
2014-04-15 02:01:25 -04:00
|
|
|
|
2015-06-07 23:07:20 -04:00
|
|
|
# Understand the architecture
|
2014-04-15 02:01:25 -04:00
|
|
|
|
2014-06-07 18:20:43 -04:00
|
|
|
Docker is an open platform for developing, shipping, and running applications.
|
|
|
|
Docker is designed to deliver your applications faster. With Docker you can
|
2015-11-20 21:39:46 -05:00
|
|
|
separate your applications from your infrastructure and treat your
|
2014-06-07 18:20:43 -04:00
|
|
|
infrastructure like a managed application. Docker helps you ship code faster,
|
|
|
|
test faster, deploy faster, and shorten the cycle between writing code and
|
|
|
|
running code.
|
|
|
|
|
2015-12-07 15:29:09 -05:00
|
|
|
Docker does this by combining kernel containerization features with workflows
|
|
|
|
and tooling that help you manage and deploy your applications.
|
2014-06-07 18:20:43 -04:00
|
|
|
|
|
|
|
At its core, Docker provides a way to run almost any application securely
|
|
|
|
isolated in a container. The isolation and security allow you to run many
|
|
|
|
containers simultaneously on your host. The lightweight nature of containers,
|
|
|
|
which run without the extra load of a hypervisor, means you can get more out of
|
|
|
|
your hardware.
|
|
|
|
|
2015-12-07 15:29:09 -05:00
|
|
|
Surrounding the container is tooling and a platform which can help you in
|
|
|
|
several ways:
|
2014-06-07 18:20:43 -04:00
|
|
|
|
2016-02-27 21:30:32 -05:00
|
|
|
* Get your applications (and supporting components) into Docker containers
|
|
|
|
* Distribute and ship those containers to your teams for further development
|
2014-06-07 18:20:43 -04:00
|
|
|
and testing
|
2016-02-27 21:30:32 -05:00
|
|
|
* Deploy those applications to your production environment,
|
|
|
|
whether it is in a local data center or the Cloud
|
2014-04-15 02:01:25 -04:00
|
|
|
|
2014-05-18 12:52:41 -04:00
|
|
|
## What can I use Docker for?
|
2014-04-15 02:01:25 -04:00
|
|
|
|
2014-06-07 18:20:43 -04:00
|
|
|
*Faster delivery of your applications*
|
2014-04-15 02:01:25 -04:00
|
|
|
|
2014-05-18 12:52:41 -04:00
|
|
|
Docker is perfect for helping you with the development lifecycle. Docker
|
2014-06-07 18:20:43 -04:00
|
|
|
allows your developers to develop on local containers that contain your
|
|
|
|
applications and services. It can then integrate into a continuous integration and
|
|
|
|
deployment workflow.
|
2014-04-15 02:01:25 -04:00
|
|
|
|
2014-06-07 18:20:43 -04:00
|
|
|
For example, your developers write code locally and share their development stack via
|
|
|
|
Docker with their colleagues. When they are ready, they push their code and the
|
|
|
|
stack they are developing onto a test environment and execute any required
|
|
|
|
tests. From the testing environment, you can then push the Docker images into
|
|
|
|
production and deploy your code.
|
2014-04-15 02:01:25 -04:00
|
|
|
|
2014-06-07 18:20:43 -04:00
|
|
|
*Deploying and scaling more easily*
|
2014-04-15 02:01:25 -04:00
|
|
|
|
2014-06-07 18:20:43 -04:00
|
|
|
Docker's container-based platform allows for highly portable workloads. Docker
|
|
|
|
containers can run on a developer's local host, on physical or virtual machines
|
|
|
|
in a data center, or in the Cloud.
|
2014-04-15 02:01:25 -04:00
|
|
|
|
2014-06-07 18:20:43 -04:00
|
|
|
Docker's portability and lightweight nature also make dynamically managing
|
|
|
|
workloads easy. You can use Docker to quickly scale up or tear down applications
|
|
|
|
and services. Docker's speed means that scaling can be near real time.
|
2014-04-15 02:01:25 -04:00
|
|
|
|
2014-07-03 17:02:40 -04:00
|
|
|
*Achieving higher density and running more workloads*
|
2014-04-15 02:01:25 -04:00
|
|
|
|
2014-06-07 18:20:43 -04:00
|
|
|
Docker is lightweight and fast. It provides a viable, cost-effective alternative
|
|
|
|
to hypervisor-based virtual machines. This is especially useful in high density
|
|
|
|
environments: for example, building your own Cloud or Platform-as-a-Service. But
|
|
|
|
it is also useful for small and medium deployments where you want to get more
|
|
|
|
out of the resources you have.
|
2014-04-15 02:01:25 -04:00
|
|
|
|
2014-05-18 12:52:41 -04:00
|
|
|
## What are the major Docker components?
|
|
|
|
Docker has two major components:
|
2014-04-15 02:01:25 -04:00
|
|
|
|
2014-06-07 18:20:43 -04:00
|
|
|
|
2016-02-27 21:30:32 -05:00
|
|
|
* Docker Engine: the open source containerization platform.
|
2014-06-01 16:48:04 -04:00
|
|
|
* [Docker Hub](https://hub.docker.com): our Software-as-a-Service
|
2014-05-18 12:52:41 -04:00
|
|
|
platform for sharing and managing Docker containers.
|
2014-04-15 02:01:25 -04:00
|
|
|
|
|
|
|
|
2015-03-03 16:58:35 -05:00
|
|
|
> **Note:** Docker is licensed under the open source Apache 2.0 license.
|
2014-04-15 02:01:25 -04:00
|
|
|
|
2014-06-07 18:20:43 -04:00
|
|
|
## What is Docker's architecture?
|
|
|
|
Docker uses a client-server architecture. The Docker *client* talks to the
|
|
|
|
Docker *daemon*, which does the heavy lifting of building, running, and
|
|
|
|
distributing your Docker containers. Both the Docker client and the daemon *can*
|
|
|
|
run on the same system, or you can connect a Docker client to a remote Docker
|
2014-07-08 01:50:57 -04:00
|
|
|
daemon. The Docker client and daemon communicate via sockets or through a
|
2014-06-07 18:20:43 -04:00
|
|
|
RESTful API.
|
2014-04-15 02:01:25 -04:00
|
|
|
|
2016-01-23 23:36:40 -05:00
|
|
|
![Docker Architecture Diagram](article-img/architecture.svg)
|
2014-04-15 02:01:25 -04:00
|
|
|
|
2014-05-18 12:52:41 -04:00
|
|
|
### The Docker daemon
|
2014-06-07 18:20:43 -04:00
|
|
|
As shown in the diagram above, the Docker daemon runs on a host machine. The
|
|
|
|
user does not directly interact with the daemon, but instead through the Docker
|
|
|
|
client.
|
2014-04-15 02:01:25 -04:00
|
|
|
|
2016-01-23 23:36:40 -05:00
|
|
|
### The Docker client
|
2014-05-18 12:52:41 -04:00
|
|
|
The Docker client, in the form of the `docker` binary, is the primary user
|
2014-06-07 18:20:43 -04:00
|
|
|
interface to Docker. It accepts commands from the user and communicates back and
|
|
|
|
forth with a Docker daemon.
|
2014-04-15 02:01:25 -04:00
|
|
|
|
2016-01-23 23:36:40 -05:00
|
|
|
### Inside Docker
|
2016-02-27 21:30:32 -05:00
|
|
|
To understand Docker's internals, you need to know about three resources:
|
2014-04-15 02:01:25 -04:00
|
|
|
|
2016-02-27 21:30:32 -05:00
|
|
|
* Docker images
|
|
|
|
* Docker registries
|
|
|
|
* Docker containers
|
2014-04-15 02:01:25 -04:00
|
|
|
|
2014-05-18 12:52:41 -04:00
|
|
|
#### Docker images
|
2014-04-15 02:01:25 -04:00
|
|
|
|
2014-06-07 18:20:43 -04:00
|
|
|
A Docker image is a read-only template. For example, an image could contain an Ubuntu
|
|
|
|
operating system with Apache and your web application installed. Images are used to create
|
|
|
|
Docker containers. Docker provides a simple way to build new images or update existing
|
|
|
|
images, or you can download Docker images that other people have already created.
|
|
|
|
Docker images are the **build** component of Docker.
|
2014-04-15 02:01:25 -04:00
|
|
|
|
2015-04-21 11:50:09 -04:00
|
|
|
#### Docker registries
|
2015-08-10 14:02:35 -04:00
|
|
|
Docker registries hold images. These are public or private stores from which you
|
|
|
|
upload or download images. The public Docker registry is provided with the
|
|
|
|
[Docker Hub](http://hub.docker.com). It serves a huge collection of existing
|
|
|
|
images for your use. These can be images you create yourself or you can use
|
|
|
|
images that others have previously created. Docker registries are the
|
2014-06-07 18:20:43 -04:00
|
|
|
**distribution** component of Docker.
|
2016-02-27 21:30:32 -05:00
|
|
|
For more information, go to [Docker Registry](https://docs.docker.com/registry/overview/) and
|
|
|
|
[Docker Trusted Registry](https://docs.docker.com/docker-trusted-registry/overview/).
|
2014-06-07 18:20:43 -04:00
|
|
|
|
2015-06-23 08:17:20 -04:00
|
|
|
#### Docker containers
|
2014-06-07 18:20:43 -04:00
|
|
|
Docker containers are similar to a directory. A Docker container holds everything that
|
|
|
|
is needed for an application to run. Each container is created from a Docker
|
|
|
|
image. Docker containers can be run, started, stopped, moved, and deleted. Each
|
|
|
|
container is an isolated and secure application platform. Docker containers are the
|
|
|
|
**run** component of Docker.
|
|
|
|
|
2016-01-23 23:36:40 -05:00
|
|
|
### How does a Docker image work?
|
2014-06-07 18:20:43 -04:00
|
|
|
We've already seen that Docker images are read-only templates from which Docker
|
|
|
|
containers are launched. Each image consists of a series of layers. Docker
|
|
|
|
makes use of [union file systems](http://en.wikipedia.org/wiki/UnionFS) to
|
|
|
|
combine these layers into a single image. Union file systems allow files and
|
|
|
|
directories of separate file systems, known as branches, to be transparently
|
|
|
|
overlaid, forming a single coherent file system.
|
|
|
|
|
|
|
|
One of the reasons Docker is so lightweight is because of these layers. When you
|
|
|
|
change a Docker image—for example, update an application to a new version— a new layer
|
|
|
|
gets built. Thus, rather than replacing the whole image or entirely
|
|
|
|
rebuilding, as you may do with a virtual machine, only that layer is added or
|
|
|
|
updated. Now you don't need to distribute a whole new image, just the update,
|
|
|
|
making distributing Docker images faster and simpler.
|
|
|
|
|
|
|
|
Every image starts from a base image, for example `ubuntu`, a base Ubuntu image,
|
|
|
|
or `fedora`, a base Fedora image. You can also use images of your own as the
|
|
|
|
basis for a new image, for example if you have a base Apache image you could use
|
|
|
|
this as the base of all your web application images.
|
|
|
|
|
2016-02-27 21:30:32 -05:00
|
|
|
> **Note:** [Docker Hub](https://hub.docker.com) is a public registry and stores
|
|
|
|
images.
|
2014-08-18 15:31:37 -04:00
|
|
|
|
2014-06-07 18:20:43 -04:00
|
|
|
Docker images are then built from these base images using a simple, descriptive
|
|
|
|
set of steps we call *instructions*. Each instruction creates a new layer in our
|
|
|
|
image. Instructions include actions like:
|
|
|
|
|
2016-02-27 21:30:32 -05:00
|
|
|
* Run a command
|
|
|
|
* Add a file or directory
|
|
|
|
* Create an environment variable
|
|
|
|
* What process to run when launching a container from this image
|
2014-06-07 18:20:43 -04:00
|
|
|
|
2016-02-27 21:30:32 -05:00
|
|
|
These instructions are stored in a file called a `Dockerfile`. A `Dockerfile` is
|
|
|
|
a text based script that contains instructions and commands for building the image
|
|
|
|
from the base image. Docker reads this `Dockerfile` when you request a build of
|
|
|
|
an image, executes the instructions, and returns a final image.
|
2014-05-18 12:52:41 -04:00
|
|
|
|
|
|
|
### How does a Docker registry work?
|
2014-06-07 18:20:43 -04:00
|
|
|
The Docker registry is the store for your Docker images. Once you build a Docker
|
2016-02-27 21:30:32 -05:00
|
|
|
image you can *push* it to a public registry such as [Docker Hub](https://hub.docker.com)
|
|
|
|
or to your own registry running behind your firewall.
|
2014-05-18 12:52:41 -04:00
|
|
|
|
2014-06-07 18:20:43 -04:00
|
|
|
Using the Docker client, you can search for already published images and then
|
|
|
|
pull them down to your Docker host to build containers from them.
|
2014-05-18 12:52:41 -04:00
|
|
|
|
2014-07-01 20:30:25 -04:00
|
|
|
[Docker Hub](https://hub.docker.com) provides both public and private storage
|
2014-06-07 18:20:43 -04:00
|
|
|
for images. Public storage is searchable and can be downloaded by anyone.
|
|
|
|
Private storage is excluded from search results and only you and your users can
|
|
|
|
pull images down and use them to build containers. You can [sign up for a storage plan
|
2016-05-25 17:01:13 -04:00
|
|
|
here](https://www.docker.com/pricing).
|
2014-05-18 12:52:41 -04:00
|
|
|
|
|
|
|
### How does a container work?
|
2014-06-07 18:20:43 -04:00
|
|
|
A container consists of an operating system, user-added files, and meta-data. As
|
|
|
|
we've seen, each container is built from an image. That image tells Docker
|
|
|
|
what the container holds, what process to run when the container is launched, and
|
|
|
|
a variety of other configuration data. The Docker image is read-only. When
|
|
|
|
Docker runs a container from an image, it adds a read-write layer on top of the
|
|
|
|
image (using a union file system as we saw earlier) in which your application can
|
|
|
|
then run.
|
2014-05-18 12:52:41 -04:00
|
|
|
|
|
|
|
### What happens when you run a container?
|
2014-06-07 18:20:43 -04:00
|
|
|
Either by using the `docker` binary or via the API, the Docker client tells the Docker
|
|
|
|
daemon to run a container.
|
2014-05-18 12:52:41 -04:00
|
|
|
|
2015-03-26 14:12:37 -04:00
|
|
|
$ docker run -i -t ubuntu /bin/bash
|
2014-05-18 12:52:41 -04:00
|
|
|
|
2016-02-27 21:30:32 -05:00
|
|
|
The Docker Engine client is launched using the `docker` binary with the `run` option
|
|
|
|
running a new container. The bare minimum the Docker client needs to tell the
|
|
|
|
Docker daemon to run the container is:
|
2014-05-18 12:52:41 -04:00
|
|
|
|
2016-02-27 21:30:32 -05:00
|
|
|
* What Docker image to build the container from, for example, `ubuntu`
|
2014-05-18 12:52:41 -04:00
|
|
|
* The command you want to run inside the container when it is launched,
|
2016-02-27 21:30:32 -05:00
|
|
|
for example,`/bin/bash`
|
2014-06-07 18:20:43 -04:00
|
|
|
|
|
|
|
So what happens under the hood when we run this command?
|
|
|
|
|
2016-02-27 21:30:32 -05:00
|
|
|
In order, Docker Engine does the following:
|
2014-06-07 18:20:43 -04:00
|
|
|
|
2016-02-27 21:30:32 -05:00
|
|
|
- **Pulls the `ubuntu` image:** Docker Engine checks for the presence of the `ubuntu`
|
|
|
|
image. If the image already exists, then Docker Engine uses it for the new container.
|
|
|
|
If it doesn't exist locally on the host, then Docker Engine pulls it from
|
2016-04-25 17:16:49 -04:00
|
|
|
[Docker Hub](https://hub.docker.com).
|
2016-02-27 21:30:32 -05:00
|
|
|
- **Creates a new container:** Once Docker Engine has the image, it uses it to create a
|
2016-01-23 23:36:40 -05:00
|
|
|
container.
|
|
|
|
- **Allocates a filesystem and mounts a read-write _layer_:** The container is created in
|
2014-06-07 18:20:43 -04:00
|
|
|
the file system and a read-write layer is added to the image.
|
2016-01-23 23:36:40 -05:00
|
|
|
- **Allocates a network / bridge interface:** Creates a network interface that allows the
|
|
|
|
Docker container to talk to the local host.
|
|
|
|
- **Sets up an IP address:** Finds and attaches an available IP address from a pool.
|
|
|
|
- **Executes a process that you specify:** Runs your application, and;
|
|
|
|
- **Captures and provides application output:** Connects and logs standard input, outputs
|
2014-06-07 18:20:43 -04:00
|
|
|
and errors for you to see how your application is running.
|
|
|
|
|
2016-02-27 21:30:32 -05:00
|
|
|
You now have a running container! Now you can manage your container, interact with
|
2014-06-07 18:20:43 -04:00
|
|
|
your application and then, when finished, stop and remove your container.
|
2014-05-18 12:52:41 -04:00
|
|
|
|
|
|
|
## The underlying technology
|
2015-08-25 12:36:51 -04:00
|
|
|
Docker is written in Go and makes use of several kernel features to
|
2016-01-23 23:36:40 -05:00
|
|
|
deliver the functionality we've seen.
|
2014-05-18 12:52:41 -04:00
|
|
|
|
|
|
|
### Namespaces
|
2014-06-07 18:20:43 -04:00
|
|
|
Docker takes advantage of a technology called `namespaces` to provide the
|
|
|
|
isolated workspace we call the *container*. When you run a container, Docker
|
|
|
|
creates a set of *namespaces* for that container.
|
2014-05-18 12:52:41 -04:00
|
|
|
|
2014-06-07 18:20:43 -04:00
|
|
|
This provides a layer of isolation: each aspect of a container runs in its own
|
|
|
|
namespace and does not have access outside it.
|
2014-05-18 12:52:41 -04:00
|
|
|
|
2016-02-27 21:30:32 -05:00
|
|
|
Some of the namespaces that Docker Engine uses on Linux are:
|
2014-05-18 12:52:41 -04:00
|
|
|
|
2016-02-27 21:30:32 -05:00
|
|
|
- **The `pid` namespace:** Process isolation (PID: Process ID).
|
|
|
|
- **The `net` namespace:** Managing network interfaces (NET:
|
2016-01-23 23:36:40 -05:00
|
|
|
Networking).
|
2016-02-27 21:30:32 -05:00
|
|
|
- **The `ipc` namespace:** Managing access to IPC
|
2016-01-23 23:36:40 -05:00
|
|
|
resources (IPC: InterProcess Communication).
|
2016-02-27 21:30:32 -05:00
|
|
|
- **The `mnt` namespace:** Managing mount-points (MNT: Mount).
|
|
|
|
- **The `uts` namespace:** Isolating kernel and version identifiers. (UTS: Unix
|
2014-06-07 18:20:43 -04:00
|
|
|
Timesharing System).
|
2014-05-18 12:52:41 -04:00
|
|
|
|
|
|
|
### Control groups
|
2016-02-27 21:30:32 -05:00
|
|
|
Docker Engine on Linux also makes use of another technology called `cgroups` or control groups.
|
2014-06-07 18:20:43 -04:00
|
|
|
A key to running applications in isolation is to have them only use the
|
|
|
|
resources you want. This ensures containers are good multi-tenant citizens on a
|
2016-02-27 21:30:32 -05:00
|
|
|
host. Control groups allow Docker Engine to share available hardware resources to
|
2014-06-07 18:20:43 -04:00
|
|
|
containers and, if required, set up limits and constraints. For example,
|
|
|
|
limiting the memory available to a specific container.
|
2014-05-18 12:52:41 -04:00
|
|
|
|
|
|
|
### Union file systems
|
2014-06-07 18:20:43 -04:00
|
|
|
Union file systems, or UnionFS, are file systems that operate by creating layers,
|
2016-02-27 21:30:32 -05:00
|
|
|
making them very lightweight and fast. Docker Engine uses union file systems to provide
|
|
|
|
the building blocks for containers. Docker Engine can make use of several union file system variants
|
2014-06-07 18:20:43 -04:00
|
|
|
including: AUFS, btrfs, vfs, and DeviceMapper.
|
|
|
|
|
2016-01-23 23:36:40 -05:00
|
|
|
### Container format
|
2016-02-27 21:30:32 -05:00
|
|
|
Docker Engine combines these components into a wrapper we call a container format. The
|
2016-01-14 02:22:55 -05:00
|
|
|
default container format is called `libcontainer`. In the future, Docker may
|
|
|
|
support other container formats, for example, by integrating with BSD Jails
|
|
|
|
or Solaris Zones.
|
2014-05-18 12:52:41 -04:00
|
|
|
|
|
|
|
## Next steps
|
2016-02-27 21:30:32 -05:00
|
|
|
Read about [Installing Docker Engine](installation/index.md#installation).
|
|
|
|
Learn about the [Docker Engine User Guide](userguide/index.md).
|