github.com/feiyang21687/docker@v1.5.0/docs/sources/introduction/understanding-docker.md (about)

     1  page_title: Understanding Docker
     2  page_description: Docker explained in depth
     3  page_keywords: docker, introduction, documentation, about, technology, understanding
     4  
     5  # Understanding Docker
     6  **What is Docker?**
     7  
     8  Docker is an open platform for developing, shipping, and running applications.
     9  Docker is designed to deliver your applications faster. With Docker you can
    10  separate your applications from your infrastructure AND treat your
    11  infrastructure like a managed application. Docker helps you ship code faster,
    12  test faster, deploy faster, and shorten the cycle between writing code and
    13  running code.
    14  
    15  Docker does this by combining a lightweight container virtualization platform
    16  with workflows and tooling that help you manage and deploy your applications.
    17  
    18  At its core, Docker provides a way to run almost any application securely
    19  isolated in a container. The isolation and security allow you to run many
    20  containers simultaneously on your host. The lightweight nature of containers,
    21  which run without the extra load of a hypervisor, means you can get more out of
    22  your hardware.
    23  
    24  Surrounding the container virtualization are tooling and a platform which can
    25  help you in several ways:
    26  
    27  * getting your applications (and supporting components) into Docker containers
    28  * distributing and shipping those containers to your teams for further development
    29  and testing
    30  * deploying those applications to your production environment,
    31   whether it be in a local data center or the Cloud.
    32  
    33  ## What can I use Docker for?
    34  
    35  *Faster delivery of your applications*
    36  
    37  Docker is perfect for helping you with the development lifecycle. Docker
    38  allows your developers to develop on local containers that contain your
    39  applications and services. It can then integrate into a continuous integration and
    40  deployment workflow.
    41  
    42  For example, your developers write code locally and share their development stack via
    43  Docker with their colleagues. When they are ready, they push their code and the
    44  stack they are developing onto a test environment and execute any required
    45  tests. From the testing environment, you can then push the Docker images into
    46  production and deploy your code.
    47  
    48  *Deploying and scaling more easily*
    49  
    50  Docker's container-based platform allows for highly portable workloads. Docker
    51  containers can run on a developer's local host, on physical or virtual machines
    52  in a data center, or in the Cloud.
    53  
    54  Docker's portability and lightweight nature also make dynamically managing
    55  workloads easy. You can use Docker to quickly scale up or tear down applications
    56  and services. Docker's speed means that scaling can be near real time.
    57  
    58  *Achieving higher density and running more workloads*
    59  
    60  Docker is lightweight and fast. It provides a viable, cost-effective alternative
    61  to hypervisor-based virtual machines. This is especially useful in high density
    62  environments: for example, building your own Cloud or Platform-as-a-Service. But
    63  it is also useful for small and medium deployments where you want to get more
    64  out of the resources you have.
    65  
    66  ## What are the major Docker components?
    67  Docker has two major components:
    68  
    69  
    70  * Docker: the open source container virtualization platform.
    71  * [Docker Hub](https://hub.docker.com): our Software-as-a-Service
    72    platform for sharing and managing Docker containers.
    73  
    74  
    75  **Note:** Docker is licensed under the open source Apache 2.0 license.
    76  
    77  ## What is Docker's architecture?
    78  Docker uses a client-server architecture. The Docker *client* talks to the
    79  Docker *daemon*, which does the heavy lifting of building, running, and
    80  distributing your Docker containers. Both the Docker client and the daemon *can*
    81  run on the same system, or you can connect a Docker client to a remote Docker
    82  daemon. The Docker client and daemon communicate via sockets or through a
    83  RESTful API.
    84  
    85  ![Docker Architecture Diagram](/article-img/architecture.svg)
    86  
    87  ### The Docker daemon
    88  As shown in the diagram above, the Docker daemon runs on a host machine. The
    89  user does not directly interact with the daemon, but instead through the Docker
    90  client.
    91  
    92  ### The Docker client 
    93  The Docker client, in the form of the `docker` binary, is the primary user
    94  interface to Docker. It accepts commands from the user and communicates back and
    95  forth with a Docker daemon.
    96  
    97  ### Inside Docker 
    98  To understand Docker's internals, you need to know about three components:
    99  
   100  * Docker images. 
   101  * Docker registries. 
   102  * Docker containers.
   103  
   104  #### Docker images
   105  
   106  A Docker image is a read-only template. For example, an image could contain an Ubuntu
   107  operating system with Apache and your web application installed. Images are used to create
   108  Docker containers. Docker provides a simple way to build new images or update existing
   109  images, or you can download Docker images that other people have already created.
   110  Docker images are the **build** component of Docker.
   111  
   112  #### Docker Registries
   113  Docker registries hold images. These are public or private stores from which you upload
   114  or download images. The public Docker registry is called
   115  [Docker Hub](http://hub.docker.com). It provides a huge collection of existing
   116  images for your use. These can be images you create yourself or you
   117  can use images that others have previously created. Docker registries are the 
   118  **distribution** component of Docker.
   119  
   120  ####Docker containers
   121  Docker containers are similar to a directory. A Docker container holds everything that
   122  is needed for an application to run. Each container is created from a Docker
   123  image. Docker containers can be run, started, stopped, moved, and deleted. Each
   124  container is an isolated and secure application platform. Docker containers are the
   125   **run** component of Docker.
   126  
   127  ##So how does Docker work? 
   128  So far, we've learned that:
   129  
   130  1. You can build Docker images that hold your applications.
   131  2. You can create Docker containers from those Docker images to run your
   132     applications.
   133  3. You can share those Docker images via
   134     [Docker Hub](https://hub.docker.com) or your own registry.
   135  
   136  Let's look at how these elements combine together to make Docker work.
   137  
   138  ### How does a Docker Image work? 
   139  We've already seen that Docker images are read-only templates from which Docker
   140  containers are launched. Each image consists of a series of layers. Docker
   141  makes use of [union file systems](http://en.wikipedia.org/wiki/UnionFS) to
   142  combine these layers into a single image. Union file systems allow files and
   143  directories of separate file systems, known as branches, to be transparently
   144  overlaid, forming a single coherent file system.
   145  
   146  One of the reasons Docker is so lightweight is because of these layers. When you
   147  change a Docker image—for example, update an application to a new version— a new layer
   148  gets built. Thus, rather than replacing the whole image or entirely
   149  rebuilding, as you may do with a virtual machine, only that layer is added or
   150  updated. Now you don't need to distribute a whole new image, just the update,
   151  making distributing Docker images faster and simpler.
   152  
   153  Every image starts from a base image, for example `ubuntu`, a base Ubuntu image,
   154  or `fedora`, a base Fedora image. You can also use images of your own as the
   155  basis for a new image, for example if you have a base Apache image you could use
   156  this as the base of all your web application images.
   157  
   158  > **Note:** Docker usually gets these base images from
   159  > [Docker Hub](https://hub.docker.com).
   160  
   161  Docker images are then built from these base images using a simple, descriptive
   162  set of steps we call *instructions*. Each instruction creates a new layer in our
   163  image. Instructions include actions like:
   164  
   165  * Run a command. 
   166  * Add a file or directory. 
   167  * Create an environment variable.
   168  * What process to run when launching a container from this image.
   169  
   170  These instructions are stored in a file called a `Dockerfile`. Docker reads this
   171  `Dockerfile` when you request a build of an image, executes the instructions, and
   172  returns a final image.
   173  
   174  ### How does a Docker registry work?
   175  The Docker registry is the store for your Docker images. Once you build a Docker
   176  image you can *push* it to a public registry [Docker Hub](https://hub.docker.com) or to 
   177  your own registry running behind your firewall.
   178  
   179  Using the Docker client, you can search for already published images and then
   180  pull them down to your Docker host to build containers from them.
   181  
   182  [Docker Hub](https://hub.docker.com) provides both public and private storage
   183  for images. Public storage is searchable and can be downloaded by anyone.
   184  Private storage is excluded from search results and only you and your users can
   185  pull images down and use them to build containers. You can [sign up for a storage plan
   186  here](https://hub.docker.com/plans).
   187  
   188  ### How does a container work?
   189  A container consists of an operating system, user-added files, and meta-data. As
   190  we've seen, each container is built from an image. That image tells Docker
   191  what the container holds, what process to run when the container is launched, and
   192  a variety of other configuration data. The Docker image is read-only. When
   193  Docker runs a container from an image, it adds a read-write layer on top of the
   194  image (using a union file system as we saw earlier) in which your application can
   195  then run.
   196  
   197  ### What happens when you run a container?
   198  Either by using the `docker` binary or via the API, the Docker client tells the Docker
   199  daemon to run a container.
   200  
   201      $ sudo docker run -i -t ubuntu /bin/bash
   202  
   203  Let's break down this command. The Docker client is launched using the `docker`
   204  binary with the `run` option telling it to launch a new container. The bare
   205  minimum the Docker client needs to tell the Docker daemon to run the container
   206  is:
   207  
   208  * What Docker image to build the container from, here `ubuntu`, a base Ubuntu
   209  image; 
   210  * The command you want to run inside the container when it is launched,
   211  here `/bin/bash`, to start the Bash shell inside the new container.
   212  
   213  So what happens under the hood when we run this command?
   214  
   215  In order, Docker does the following:
   216  
   217  - **Pulls the `ubuntu` image:** Docker checks for the presence of the `ubuntu`
   218  image and, if it doesn't exist locally on the host, then Docker downloads it from
   219  [Docker Hub](https://hub.docker.com). If the image already exists, then Docker
   220  uses it for the new container. 
   221  - **Creates a new container:** Once Docker has the image, it uses it to create a
   222  container. 
   223  - **Allocates a filesystem and mounts a read-write _layer_:** The container is created in 
   224  the file system and a read-write layer is added to the image.
   225  - **Allocates a network / bridge interface:** Creates a network interface that allows the 
   226  Docker container to talk to the local host. 
   227  - **Sets up an IP address:** Finds and attaches an available IP address from a pool. 
   228  - **Executes a process that you specify:** Runs your application, and; 
   229  - **Captures and provides application output:** Connects and logs standard input, outputs 
   230  and errors for you to see how your application is running.
   231  
   232  You now have a running container! From here you can manage your container, interact with
   233  your application and then, when finished, stop and remove your container.
   234  
   235  ## The underlying technology
   236  Docker is written in Go and makes use of several Linux kernel features to
   237  deliver the functionality we've seen.
   238  
   239  ### Namespaces
   240  Docker takes advantage of a technology called `namespaces` to provide the
   241  isolated workspace we call the *container*.  When you run a container, Docker
   242  creates a set of *namespaces* for that container.
   243  
   244  This provides a layer of isolation: each aspect of a container runs in its own
   245  namespace and does not have access outside it.
   246  
   247  Some of the namespaces that Docker uses are:
   248  
   249   - **The `pid` namespace:** Used for process isolation (PID: Process ID). 
   250   - **The `net` namespace:** Used for managing network interfaces (NET:
   251   Networking). 
   252   - **The `ipc` namespace:** Used for managing access to IPC
   253   resources (IPC: InterProcess Communication). 
   254   - **The `mnt` namespace:** Used for managing mount-points (MNT: Mount). 
   255   - **The `uts` namespace:** Used for isolating kernel and version identifiers. (UTS: Unix
   256  Timesharing System).
   257  
   258  ### Control groups
   259  Docker also makes use of another technology called `cgroups` or control groups.
   260  A key to running applications in isolation is to have them only use the
   261  resources you want. This ensures containers are good multi-tenant citizens on a
   262  host. Control groups allow Docker to share available hardware resources to
   263  containers and, if required, set up limits and constraints. For example,
   264  limiting the memory available to a specific container.
   265  
   266  ### Union file systems
   267  Union file systems, or UnionFS, are file systems that operate by creating layers,
   268  making them very lightweight and fast. Docker uses union file systems to provide
   269  the building blocks for containers. Docker can make use of several union file system variants
   270  including: AUFS, btrfs, vfs, and DeviceMapper.
   271  
   272  ### Container format 
   273  Docker combines these components into a wrapper we call a container format. The
   274  default container format is called `libcontainer`. Docker also supports
   275  traditional Linux containers using [LXC](https://linuxcontainers.org/). In the 
   276  future, Docker may support other container formats, for example, by integrating with
   277  BSD Jails or Solaris Zones.
   278  
   279  ## Next steps
   280  ### Installing Docker
   281  Visit the [installation section](/installation/#installation).
   282  
   283  ### The Docker User Guide
   284  [Learn Docker in depth](/userguide/).
   285  
   286