What is the Docker Docker introductory tutorial first article _docker

Source: Internet
Author: User
Tags docker ps docker run

Docker is a newborn thing, and the concept is similar to virtualization. There are a lot of things about Docker getting started online. However, this paper discusses the characteristics, characteristics and principles of Docker, also introduces the installation test process with Chinese characteristics, and also talks about the community ecology and dockerfile of Docker, and uses Dockerfile to build a nginx environment.

Origin

Heard Docker a few months ago, but there has been no time to study, some time before the azure free trial, hurriedly experiment, but card in the Ubuntu basic image download (due to the special reasons of the domestic network), so also stranded, here to share experience and experience.

Docker Introduction

I think in simple terms, Docker is an application execution container, similar to the concept of virtual machines. But the difference with virtualization technology is the following points:

1. Virtualization technology relies on physical CPUs and memory and is hardware-level, while Docker is built on the operating system, leveraging the containerization technology of the operating system, so Docker can even run on virtual machines.
2. Virtualization systems are generally referred to as operating system mirroring, more complex, called "system", and Docker open source and lightweight, called "containers", a single container suitable for deployment of a small number of applications, such as the deployment of a redis, a memcached.
3. Traditional virtualization technology uses snapshots to save state, while Docker is not only more lightweight and low-cost in saving state, but also introduces a similar source control mechanism that records the snapshot history version of the container, and the switching cost is low.
4. Traditional virtualization technologies are complex to build systems and require a lot of human resources, and Docker can build the entire container through Dockfile, restarting and building fast. More importantly, Dockfile can be written manually so that application developers can guide the system environment and dependencies by publishing Dockfile, which is good for continuous delivery.
5.Dockerfile can create a new container based on an already built container mirror. Dockerfile can be shared and downloaded through the community, which is conducive to the promotion of this technology.

The main features of Docker are as follows (excerpt from Docker: Consistent automated software Deployment):

File system Isolation: Each process container runs in a completely separate root file system.
Resource Isolation: You can use Cgroup to assign different system resources, such as CPUs and memory, to each process container.
network isolation: Each process container runs in its own network namespace, with its own virtual interface and IP address.
Write-time replication: Create a root file system using write-time replication, which makes deployment extremely fast and saves memory and hard disk space.
Log Record: Docker will collect and record the standard stream (Stdout/stderr/stdin) for each process container for real-time retrieval or bulk retrieval.
Change Management: container file system changes can be committed to new images and reused to create more containers. You do not need to use a template or manual configuration.
Interactive Shell: Docker can assign a virtual terminal and associate it to any container's standard input, such as running a one-time interactive shell.
At present Docker is in the development stage, the official does not suggest to use in the production environment. In addition, Docker is based on Ubuntu, so the official recommendation is to install it on Ubuntu's operating system, which is currently only installed on Linux.

Getting started with the use of Docker

Docker The official Installation guide is very detailed. However, due to the special circumstances of the country, I am here based on this installation guide, gives the introduction process for domestic developers. Based on the Ubuntu Precise 12.04 (LTS) (64-bit), this version is recommended, and other versions refer to the official installation manual.

1, the installation of Docker
First, Docker officials say it's best to run more than 3.8 of the Linux kernel, so kernel upgrades are needed first

# Install the backported kernel
sudo apt-get update
sudo apt-get install linux-headers-generic-lts-raring

# reboot
sudo reboot

Add key for Docker warehouse:

Copy Code code as follows:
sudo apt-key adv--keyserver keyserver.ubuntu.com--recv-keys 36a1d7869245c8950f966e92d8576a8ba88d21e9

Add mirrors, here directly with the Russian Yandex

sudo sh-c "echo deb Http://mirror.yandex.ru/mirrors/docker/docker main >/etc/apt/sources.list.d/docker.list"
sudo apt-get update


Install Docker

sudo apt-get install Lxc-docker

Generally, the above steps will not have any problems.

2, Docker image download

Docker running requires at least one Ubuntu base mirror, which will be downloaded automatically when you first try it out, but it's basically not going to work from home, and you need to use a proxy. This is a proxy provided by the Docker Chinese community. Provenance 1, provenance 2.

Modify the/etc/default/docker file to uncomment the Http_proxy section:

# If You need Docker to use an HTTP proxy, it can also is specified here.
Export http_proxy=http://192.241.209.203:8384/
After the author test, the above agent is not very stable, may use more people.

Available proxy addresses (in continuous focus)

http://192.241.209.203:8384

Then, you can execute the following command, the test executes, because the agent is not very stable, may need to try more than a few times (calm):

sudo docker run-i-T Ubuntu/bin/bash

The screenshot below is my successful pull finish and test Hello World success (probably tried 3-4 times):


Community and Ecology of Docker

Warehouses and mirrors

The assessment of an open source technology largely requires attention to its community and ecology. The ecology of Docker is carried out by pushing and pulling specific "mirror image" packages. You can search for the desired mirror from Docker index. You can also search by the following command:

sudo docker search

Mirroring can be thought of as a container system containing some software, such as Ubuntu, an official base image, many of which are based on this mirror "derivative" that contains the basic Ubuntu system. Hipache, for example, is an official mirror container that is run to support HTTP and WebSocket proxy services, which are based on Ubuntu itself.

Pull the mirror through the pull to download the mirror to the local, for example

sudo docker pull Hipache

See what mirrors are now available through images:

sudo docker images

REPOSITORY   TAG     IMAGE ID   CREATED    VIRTUAL SIZE
ubuntu    13.10    9F676BD305A4  6 weeks ago   178 mb
ubuntu    Saucy    9f676bd305a4  6 weeks ago   178 MB
Ubuntu    13.04    eb601b8965b8  6 weeks ago   166.5 MB
ubuntu    raring    Eb601b8965b8  6 weeks ago   166.5 mb
ubuntu    12.10    5ac751e8d623  6 weeks ago   161 MB
Ubuntu    quantal    5ac751e8d623  6 weeks ago   161 MB
ubuntu    10.04    9cc9ea5ea540  6 weeks ago   180.8 mb
ubuntu    Lucid    9cc9ea5ea540  6 weeks ago   180.8 mb
ubuntu    12.04    9cd978db300e  6 weeks ago   204.4 MB
ubuntu    latest    9cd978db300e  6 weeks ago   204.4 MB
ubuntu    precise    9cd978db300e  6 weeks ago   204.4 MB

The above are all mirrors, are from the Ubuntu warehouse (General warehouse name should be username/repository format, if it is directly to Repository as a warehouse name, refers to the official release of the warehouse). When we pull Ubuntu, we actually pull down the mirrors in the warehouse. Each mirror has a unique image ID and an easy to remember tag that identifies a mirror by the first few or repository:tag of the image ID.

Dockerfile and Dockerfile to build nginx containers
In addition to pull, mirroring can be obtained by compiling, where "compiling" refers to a build behavior that builds a mirror by either manually writing or obtaining dockerfile from the GitHub. You can think of Dockerfile as a script that executes every time the container is started. Generally in dockerfile, you need to write the installation scripts and configuration scripts for the underlying software. The following dockerfile is an example:

#
Ubuntu Dockerfile # # https://github.com/dockerfile/ubuntu # #

Pull base image.
From ubuntu:12.10

# Update OS.
Run echo "Deb Http://archive.ubuntu.com/ubuntu quantal Main Universe Multiverse" >/etc/apt/sources.list
Run Apt-get update
RUN apt-get upgrade-y

# Install basic packages.
Run Apt-get install-y software-properties-common
run apt-get install-y curl git htop unzip vim wget

# Add file S.
Add ROOT/.BASHRC/ROOT/.BASHRC
Add root/.gitconfig/root/.gitconfig
add root/scripts/root/scripts

# Set working directory.
ENV home/root
Workdir/root

The from directive indicates that this build requires a mirror image of the 12.10 tag of the Ubuntu warehouse, which is automatically downloaded if there is no local image. Mirroring is actually a good result of compiling. To this dockerfile, a lot of common software is installed on the basis of original Ubuntu.

The official Docker has Dockerfile tutorials: https://www.docker.com/what-docker

Practice

First make sure you have completed the installation steps above and pull to the base mirror ubuntu:12.10.

Now we use the above dockerfile to build this warehouse (Dockerfile is actually the GitHub Dockerfile project's basic warehouse Dockerfile/ubuntu, The following commands are built directly from the GitHub download Dockerfile.

Copy Code code as follows:
sudo docker build-t= "Dockerfile/ubuntu" Github.com/dockerfile/ubuntu

The following is the output of the last build success:

...
Processing triggers for Ureadahead
 ... ---> 0a4392cf8e2d step
6:add ROOT/.BASHRC/ROOT/.BASHRC
 ---> b0e86f348c09 step
7:add Root/.gitconfi G/root/.gitconfig
 ---> e2a9c001d457 step
8:add root/scripts/root/scripts
 ---> 678FEBABDBDC
Step 9:env home/root
 ---> Running into c4afef311cf1
 ---> eaa3ae3277a8 step
10:workdir/root
 - --> Running in D50c273c75b8
 ---> c9ecf5bc3227
successfully built c9ecf5bc3227
removing Intermediate container 1a3d1f794c49
Removing intermediate container 9f72df8abb63 removing intermediate
Container 5694d1e3e77e
Removing intermediate container 6a184821f89c removing intermediate container
8195BD05FC36
Removing intermediate container d50c273c75b8 removing intermediate container 70de07353ecf
removing intermediate container 73e3f6204613
Removing intermediate container 5dd948415981 removing intermediate container c4afef311cf1

One more warehouse will be added at this time:

sudo docker images
REPOSITORY   TAG     IMAGE ID   CREATED    VIRTUAL SIZE
Dockerfile/ubuntu    c9ecf5bc3227  About a minute ago 294.2 MB
...

Now we can build Dockerfile/nginx (of course, you can directly pull this mirror from the start)

sudo docker build-t= "Dockerfile/nginx" Github.com/dockerfile/nginx

When you're done, you'll see a compiled mirror:

sudo docker images
REPOSITORY   TAG     IMAGE ID   CREATED    VIRTUAL SIZE
Dockerfile/nginx Latest    68508350c656 about  a minute ago 308.3 MB
dockerfile/ubuntu latest    c9ecf5bc3227  16 Minutes ago  294.2 MB
...

Now it's time to see the real effect! Run the container with the following command:

sudo docker run-d-P 80:80 Dockerfile/nginx

This command runs the container in a daemon manner, viewing the running container by the following command:

sudo docker PS
CONTAINER ID  IMAGE      COMMAND    CREATED    STATUS    PORTS    NAMES
98c474a7dd6a  dockerfile/nginx:latest nginx    6 seconds ago up  6 seconds  0.0.0.0:80->80/tcp trusting_hawking

Visit the 80 port of your host, you can see the Welcome page of Nginx! At this point, let's take a look at the native process sudo ps-ef:

Root  1428 952 0 15:19?  00:00:00 Nginx:master process Nginx
root  1429 417 0 15:19?  00:00:00/SBIN/UDEVD--daemon
Www-data 1441 1428 0 15:19?  00:00:00 Nginx:worker Process
Www-data 1442 1428 0 15:19?  00:00:00 Nginx:worker Process
www-data 1443 1428 0 15:19?  00:00:00 Nginx:worker Process
Www-data 1444 1428 0 15:19?  00:00:00 Nginx:worker Process

Seems to be a little close to the nature of things! The nginx process is actually on this machine, which means that the execution of the program in the container is still using the native operating system, the container does not build the operating system itself, but relies on the native operating system to work in some isolated way. This is the essential difference between Docker and virtual machines.

You can map the directory of this machine to this "Nginx container" as follows. There should be a Nginx profile fragment under the <sites-enabled-dir> directory

Copy Code code as follows:
Docker run-d-P 80:80-v <sites-enabled-dir>:/etc/nginx/sites-enabled-v <log-dir>:/var/log/nginx Dockerfile/nginx

PS: This step I did not succeed, the log path can be map, but sites-enable-dir in the configuration is not always. Continue the diagnostics.
Sharing of mirrors and distribution of dockfile
You can share your mirrors and the dockfile you use to build your community:

Docker index is an official mirror directory that can get a lot of precompiled mirrors from inside
Dockerfile Project a repository collection of managed Dockerfile

Principle
overall Docker's core technology is as follows:

Name space
Aufs (advanced multi layered Unification filesystem)
Cgroup

Because I talents, interested friends can expand reading, will certainly have a more profound understanding of Docker.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.