This is a creation in Article, where the information may have evolved or changed.
Docker-Small Li Fei-like virtualization solution
About Docker
Docker is an open platform for developers, system administrators to create, distribute, and ultimately run applications. It is developed using Google's Go language and is open source on Github. As a lightweight operating system virtualization solution, Docker is now getting more and more attention and use, especially Redhat,ubuntu and Google have announced official support for Docker, which has allowed Docker to embark on the fast lane, attracting a lot of developers.
What are the advantages of Docker versus traditional virtual machine solutions, and take a look at one of the official websites:
Docker is based on LXC and uses containers on the operating system, so it improves the reusability of the operating system and even deploys hundreds of Docker containers on a single physical machine. Of course, because the operation relies on the operating system, Docker itself is limited by the current operating system. Specifically, you can look at a discussion that you know.
Docker Basic Concepts
Docker Image (image)
A docker image is a read-only template used to create a Docker container. The official Docker warehouse provides a very wide range of official images from the operating system to various programming languages, we can create our own images based on these images, or you can use the Docker build command to generate a new image with Dockerfile. Docker images can be posted to public warehouses (such as Docker's officially maintained Docker Hub) or stored in their own private warehouses. You can easily share and deploy apps by creating Docker images.
Docker container (Container)
Docker containers are Docker run instances created from Docker images. The containers on the same system are isolated from each other, each with independent process space and network space. After a container exits, we can create a new Docker image based on that container.
Docker Warehouse (Repository)
Docker repositories are libraries that store various Docker images. For example, the Ubuntu system repository provides different versions of Docker images.
Installing Docker
Docker supports Mac OS and many major Linux operating systems, the official documentation is detailed, and here's just a look at the installation on the Ubuntu14.04 64bit:
curl -sSL https://get.docker.com/ubuntu/ | sudo sh
If you use Docker after the installation is complete, the following error occurs:
Cannot connect to the Docker daemon. Is ' docker-d ' running on this host?
So referring to the content here, we need to execute the following command to install AppArmor:
sudo apt-get install apparmor
Docker Basic Operations
View local Docker Images
sudo docker images
Get Docker image
sudo docker pull ubuntu:14.04
The above command downloads Ubuntu 1404 from the Docker official Repository registry server, which is equivalent to:
sudo docker pull registry.hub.docker.com/ubuntu:14.04
You can see more clearly that the download address is the image of the tag 14.04 in the Ubuntu repository under the registry of the Docker Hub.
Creating mirrors
As mentioned earlier, we can create a new image by modifying an existing mirror, or you can create a new image from scratch by creating commands and configuration files, as described below.
- Create a new image by modifying an existing mirror
# Create a container based on the Ubuntu image of tag 14.04 and run it in the container/bin/bash#-T means to open a pseudo terminal in the container#-I means standard input for connecting to a containerrwang@ubuntu140464:~$sudo docker run-i-T Ubuntu:14.04/bin/bash# Update the software sourceroot@cd2107af89ae:/#Apt-get Update# Install the Python development environmentroot@cd2107af89ae:/#Apt-get install-y build-essential python-dev libevent-dev python-pip Liblzma-dev# Exit Containerroot@cd2107af89ae:/#Exit# View current image informationrwang@ubuntu140464:~$sudo docker images# Create a new image based on the container just nowrwang@ubuntu140464:~$sudo docker commit-m"Ubuntu1404 Python2.7 Env"-A"Xhrwang@gmail.com"Cd2107af89ae UBUNTU-PYTHON27:V1# Once again, look at the local image information and you will find one more of the following information:# REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE# UBUNTU-PYTHON27 v1 a6c20de08034 7 minutes ago 392.7 MB# Create a container based on a newly created imagerwang@ubuntu140464:~$sudo docker run-it Ubuntu-python27:v1/bin/bash# Python development environment is readyroot@ef66d647855b:/#Python# Python 2.7.6 (default, Mar, 22:59:56)# [GCC 4.8.2] on linux2# Type ' help ', ' copyright ', ' credits ' or ' license ' for more information.# >>>
- Create a new image by creating a command and configuration file
The Docker image configuration file content instance is as follows:
# 注释的形式是这样的。# This is a comment# Docker 创建镜像的时候基于哪一个镜像FROM ubuntu:14.04# 镜像维护者的信息MAINTAINER Docker Newbee <newbee@docker.com># 创建过程中执行的命令,每条 RUN 命令就是镜像的一个 layer RUN apt-get -qq updateRUN apt-get -qqy install ruby ruby-devRUN gem install sinatra
Use the following command to build
rwang@ubuntu140464:~$ sudo docker build -t="IMAGENAME:IMAGETAG" DockerfilePath
At the end we can view the list of local mirrors with "Docker images".
Export and load photographed image
Using Docker save, you can place a local image everywhere as a tarball, using the following method:
$ sudo docker imagesREPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZEubuntu 14.04 c4ff7513909d 5 weeks ago 225.4 MB...$sudo docker save -o ubuntu_14.04.tar ubuntu:14.04
Using Docker load, you can import previously exported tarball to the local mirror library using the following method:
$ sudo docker load --input ubuntu_14.04.tar
Or:
$ sudo docker load < ubuntu_14.04.tar
Remove Mirror
Before deleting a mirror, you need to remove all containers based on this image using the following command
$ sudo docker rm ubuntu:14.04
Using Docker RMI, you can remove a mirror from the local mirror library by using the following method:
$ sudo docker rmi ubuntu:14.04
Container
Create a new container and start
When you do not need to interact with the container and only perform an operation, use the following command to create and start a container based on a mirror:
$ sudo docker run ubuntu:14.04 /bin/echo 'Hello world'
If you need to interact with the container, you can have Docker assign a pseudo terminal to the container and bind the standard input of the current shell and container, and we used a similar usage before:
$ sudo docker run -t -i ubuntu:14.04 /bin/bash
It can then be manipulated in the context of the container.
When you use Docker run, the following things happen:
- Checks if there is a specified image locally and does not exist to download from the public repository
- Create and start a container with mirroring
- Assign a file system and mount a layer of read-write layers outside the mirrored layer
- Bridging a virtual interface into a container from the bridge interface configured by the host host
- Configure an IP address from the address pool to the container
- Executing user-specified applications
- Container is terminated when execution is complete
Start a container that has been terminated
$ sudo docker start CONTAINERID
Restarting a running container is useful for a container that is running in the daemon way that is said immediately after
$ sudo docker restart CONTAINERID
Running containers in the background
$ sudo docker run -d ubuntu:14.04 /bin/sh -c "while true; do echo hello world; sleep 1; done"
Use Docker PS to see the container information that is running
If you need to focus on the running state of the background container, you can use Docker logs to view the running log
Terminate container Run
If the shell is connected to a container's standard input, you can use the Exit command or Ctrl + D to terminate the container's operation. For containers running in the background, you can use the Docker Stop command to do the same thing.
Connecting containers
For a background container that has a pseudo terminal open, you can create and run it by using the following command:
$ sudo docker -idt ubuntu:14.04
You can then connect to this container by using the following command:
$ sudo docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES243c32535da7 ubuntu:latest "/bin/bash" 18 seconds ago Up 17 seconds nostalgic_hypatia$sudo docker attach nostalgic_hypatiaroot@243c32535da7:/#
Exporting and loading containers
$ sudo docker PS- Acontainer ID IMAGE COMMAND CREATED STATUS PORTS names7691a814370e ubuntu:14.04 "/bin/bash" hours Exited ago Span class= "O" > ( 0 hours ago test $ sudo docker export 7691a814370e > Ubuntu.tar
$ test/buntu:v1.0$ sudo docker imagesREPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZEtest/ubuntu v1.0 9d37a6082e97 About a minute ago 171.3 MB
The exported container is just a snapshot of the current state of the container and does not contain history and metadata information before the container.
Warehouse
We can upload the publicly allowed images to Docker's official Docker Hub to share, or you can download and install Docker-registry to create a private repository to store your own images, which you can refer to here
Docker Resources
--EOF--
- Android Download List instance →
- ← Climbing Stairs and Fibonacci series
Disclaimer: This article uses the BY-NC-SA protocol for authorization. Reprint please specify transfer from: Docker-small Li Fei solution-like virtualization solutions