Docker User Guide (1) - Basic Operation

Docker is an open source engine that automates the deployment of development applications to containers. It was written by the team at Docker and is licensed under the Apache 2.0 open source license. It provides a simple, lightweight way of modeling, makes the development life cycle more efficient and fast, and encourages service-oriented architecture design. The goal of the Docker project is to implement a lightweight operating system virtualization solution. Docker is based on technologies such as Linux Containers (LXC). On the basis of LXC, Docker is further encapsulated, so that users do not need to care about container management, making the operation easier. Operating a Docker container is as easy as operating a fast and lightweight virtual machine.

Features of Docker:

  1. Faster delivery and deployment
  2. more efficient virtualization
  3. Easier migration and scaling
  4. simpler management
  • Performance comparison of container technology and traditional virtual machines

  • Docker and virtual machine construction comparison

A Docker container is essentially a process on the host machine. Docker realizes resource isolation through namespace, realizes resource limitation through cgroups, and realizes efficient file operations through copy-on-write mechanism.

Docker has five namespaces: process, network, mount, host, and shared memory. In order to isolate problematic applications, Docker uses namespaces to isolate processes, create isolated running spaces for processes or process groups, and provide different processes for processes. Namespace view. In this way, each isolated process group acts as a container to the outside world. It should be noted that Docker confuses users into thinking that they are occupying all the resources, but this is not a "virtual machine".

Three Concepts in Docker: Image, Container, Repository

  1. Image: A Docker image is a read-only template that can be used to create Docker containers. Docker provides a very simple mechanism to create an image or update an existing image, and users can even download a ready-made image directly from others to use directly.
    An image is a file structure. Each command in the Dockerfile creates a new hierarchy in the filesystem, the filesystem is built on top of these layers, and the image is built on top of these combined filesystems. The official Docker website has a dedicated page to store all available images at index.docker.io .

  2. Container: A container is a running instance created from an image. It can be started, started, stopped, deleted. Each container is an isolated, secure platform. Think of a container as a simple Linux environment, and Docker uses containers to run applications. The image is read-only, and the container creates a writable layer as the top layer when it starts.

  3. Warehouse: Warehouse is a place for centralized storage of image files. There are often multiple warehouses stored on the warehouse registration server (Registry), each warehouse contains multiple mirrors, and each mirror has a different tag (tag). Currently, the largest public repository is Docker Hub, which houses a huge number of images for users to download.

The Docker repository is used to save our images. When we create our own image, we can use the push command to upload it to a public or private repository, so that the next time we want to use this image on another machine, we only need to start from Just pull it down from the warehouse. The concept of a Docker repository is similar to Git, and a registry server can be understood as a hosting service like GitHub.

1. Install Docker

This experiment environment: Tencent Cloud Server  CentOS 6.7 x86_64

前提条件:
Docker运行对内核要求比较高,因此一般建议直接在Ubuntu这样的平台运行。但作为一个容器标准,Docker也是支持其他如CentOS, Mac OS X, Windows等平台。目前Docker支持以下版本CentOS:

  • CentOS 7(64位)
  • CentOS 6.5(64位)及以后
  • 在运行CentOS 6.5及以后版本时,需要内核版本>=2.6.32-431,因为这些内核包含了运行Docker的一些特定修改。

Docker默认使用AUFS作为存储驱动,但是AUFS并没有被包括在Linux的主线内核中。CentOS中可以使用Device Mapper作为存储驱动,这是在2.6.9内核版本引入的新功能。我们需要先确认是否启用该功能:

CentOS 7

Docker RPM包已经包含在CentOS-Extra仓库中,所以我们可以直接使用Yum安装:

# yum install docker

CentOS 6.6

需要注意的是,CentOS6.6中,已经有一个同名docker的可执行系统程序包。所以Docker RPM包命名为docker-io,我们先卸掉docker。

# yum -y remove docker

第三步 Install Docker-IO

# yum -y install docker-io

这样完成了Docker的安装。

# /etc/init.d/docker start #启动docker
# docker info #查看 docker 基本信息

二. Docker 基本操作

# docker run -d --name mynginx nginx   #启动nginx镜像,没有会自动pull
# docker stop bfd094233f96   #停止一个容器,根据容器 id 进行删除
# docker rm bfd094233f96   #删除一个容器
# docker attach d20f3dc6cd92  #进入一个正在运行的容器

-t 选项让Docker分配一个伪终端(pseudo-tty)并绑定到容器的标准输入上,
-i 则让容器的标准输入保持打开。
–name 使用一个自定义的名字

此命令不太好用,建议使用以下命令进入容器:

[root@localhost docker] docker inspect --format "{{.State.Pid}}" mynginx  #获取容器pid 19769
[root@localhost docker] nsenter --target 19769 --mount --uts --ipc --net --pid  #进入容器(推荐方法)
docker run -d -p 91:80 --name mynginx2 nginx    # -p 指定端口映射,将80映射为host的91

存储镜像:

# docker save -o ubuntu_14.04.tar ubuntu:14.04

载入镜像:

# docker load < ubuntu_14.04.tar  或者使用 
# cat ubuntu.tar |  docker import - test/ubuntu:v1.0

移除本地镜像:

# docker rmi training/sinatra
清理所有未打过标签的本地镜像:

# docker rmi $(docker images -q -f "dangling=true")
其中 -q 和 -f 是 quiet,–filter 的缩写, 完整的命令其实可以写着下面这样,是不是更容易理解一点?

# docker rmi $(docker images --quiet --filter "dangling=true")
注: 容器是否会长久运行,是和docker run指定的命令有关,和 -d 参数无关。

要获取容器的输出信息,可以通过 docker logs 命令。
# docker logs [container ID or NAMES]

删除容器:
# docker rm 默认并不会删除运行中的容器

有关容器和镜像的底层信息:
# docker inspect container/image

可以查看:
容器实例的IP地址
端口绑定列表
特定端口映射的搜索
收集配置的详细信息

从容器内复制文件到指定的路径上:
# docker cp container:path hostpath

使用Dockerfile来构建镜像:
# docker build [options] PATH | URL

–rm=true表示构建成功后,移除所有中间容器
–no-cache=false表示在构建过程中不使用缓存

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325779479&siteId=291194637