Docker概述

Docker 是一个基于 Go 语言开源的应用容器引擎。

Docker 可以让开发者打包他们的应用以及依赖包到一个轻量级、可移植的容器中。

容器是完全使用沙箱机制,相互之间不会有任何接口(类似 iPhone 的 app),更重要的是容器性能开销极低。

Docker架构

Docker 包括三个基本概念:

  • 镜像(Image):Docker 镜像(Image),就相当于是一个 root 文件系统。比如官方镜像 ubuntu:16.04 就包含了完整的一套 Ubuntu16.04 最小系统的 root 文件系统。
  • 容器(Container):镜像(Image)和容器(Container)的关系,就像是面向对象程序设计中的类和实例一样,镜像是静态的定义,容器是镜像运行时的实体。容器可以被创建、启动、停止、删除、暂停等。
  • 仓库(Repository):仓库可看成一个代码控制中心,用来保存镜像。

Docker 使用客户端-服务器 (C/S) 架构模式,使用远程API来管理和创建Docker容器。

Docker 容器通过 Docker 镜像来创建。

容器与镜像的关系类似于面向对象编程中的对象与类。

Docker安装使用

使用官方安装脚本一键安装

1
curl -fsSL https://get.docker.com | bash -s docker --mirror aliyun

启动Docker

systemctl start docker

验证Docker服务

输入这条命令:docker run hello-world,若输出以下信息则代表安装成功:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[root@template logstash]# docker run hello-world

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64)
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/

For more examples and ideas, visit:
https://docs.docker.com/get-started/

拉取Centos 7镜像

输入这条命令拉取Centos 7镜像:docker pull centos:7,如果不指定版本的话,默认会拉取Centos 8的镜像。然后输入docker images命令查看本地的镜像,可以看到Centos7的镜像拉取成功。

1
2
3
4
5
6
7
8
9
10
11
[root@template logstash]# docker pull centos:7
7: Pulling from library/centos
2d473b07cdd5: Pull complete
Digest: sha256:0f4ec88e21daf75124b8a9e5ca03c37a5e937e0e108a255d890492430789b60e
Status: Downloaded newer image for centos:7
docker.io/library/centos:7
[root@template logstash]#
[root@template logstash]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest d1165f221234 3 months ago 13.3kB
centos 7 8652b9f0cb4c 7 months ago 204MB

如果需要别的版本的话,去docker官网选择对应版本的拉取命令:

https://hub.docker.com/_/centos?tab=tags&page=1

容器的基本使用

容器的启动

docker run -it 8652b9f0cb4c /bin/bash,该命令会使用镜像ID为 8652b9f0cb4c 的镜像启动一个容器,并以命令行模式进入该容器。

1
2
3
4
5
6
[root@template logstash]# docker run -it 8652b9f0cb4c /bin/bash
[root@32ee4dd688b7 /]#
[root@32ee4dd688b7 /]#
[root@32ee4dd688b7 /]# exit
exit
[root@template logstash]#

参数说明:

  • -i:交互式操作。
  • -t:终端。
  • 8652b9f0cb4c:镜像ID。
  • /bin/bash:放在镜像名后的是命令,这里我们希望有个交互式 Shell,因此用的是 /bin/bash。

exit会退出进入的容器。

查看已经启动的容器

docker ps命令可以查看已启动的容器,详细的可以看以下说明:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[root@template logstash]# docker ps --help

Usage: docker ps [OPTIONS]

List containers

Options:
-a, --all Show all containers (default shows just running)
-f, --filter filter Filter output based on conditions provided
--format string Pretty-print containers using a Go template
-n, --last int Show n last created containers (includes all states) (default -1)
-l, --latest Show the latest created container (includes all states)
--no-trunc Don't truncate output
-q, --quiet Only display container IDs
-s, --size Display total file sizes
[root@template logstash]#

启动已停止运行的容器

查看所有的容器命令如下:

1
2
3
4
5
[root@template logstash]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
32ee4dd688b7 8652b9f0cb4c "/bin/bash" 4 minutes ago Exited (0) 45 seconds ago quirky_cartwright
93a7a66ddc4b hello-world "/hello" 22 minutes ago Exited (0) 22 minutes ago flamboyant_hamilton
ee564fc969ae ubuntu:15.10 "/bin/echo 'Hello wo…" 25 minutes ago Exited (0) 25 minutes ago trusting_payne

使用 docker start 启动一个已停止的容器:

1
2
3
[root@template logstash]# docker start 32ee4dd688b7
32ee4dd688b7
[root@template logstash]#

进入已启动的容器

docker exec -it 32ee4dd688b7 /bin/bash,使用docker exec命令,exit退出这个容器时,容器不会停止运行。

1
2
3
4
5
6
7
8
[root@template logstash]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
32ee4dd688b7 8652b9f0cb4c "/bin/bash" 7 minutes ago Up About a minute quirky_cartwright
[root@template logstash]#
[root@template logstash]#
[root@template logstash]# docker exec -it 32ee4dd688b7 /bin/bash
[root@32ee4dd688b7 /]#
[root@32ee4dd688b7 /]#

使用docker自定义容器

一般来说有两个方法来自定义容器:

  • 一是制定DockerFile;
  • 二是拉取一个相关的镜像后,将环境部署好后提交成一个专属的镜像。

这里我们来讲解一下第二种方式,也比较简单,进入容器后,再将他提交成另一个专属镜像即可。

本次例子中会使用 Centos 7 镜像来进行讲解,注意,如果使用别的镜像的话,有些基本的命令比如yum会不支持,还需要额外安装。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
[root@template logstash]# docker run -it 8652b9f0cb4c /bin/bash
[root@3a4dc8cbcad8 /]#
[root@3a4dc8cbcad8 /]# yum install net-tools
Loaded plugins: fastestmirror, ovl
Determining fastest mirrors
* base: mirrors.aliyun.com
* extras: mirrors.cn99.com
* updates: mirrors.aliyun.com
base | 3.6 kB 00:00:00
extras | 2.9 kB 00:00:00
updates | 2.9 kB 00:00:00
(1/4): extras/7/x86_64/primary_db | 242 kB 00:00:00
(2/4): base/7/x86_64/group_gz | 153 kB 00:00:00
(3/4): base/7/x86_64/primary_db | 6.1 MB 00:00:01
(4/4): updates/7/x86_64/primary_db | 8.8 MB 00:00:03
Resolving Dependencies
………………

Installed:
net-tools.x86_64 0:2.0-0.25.20131004git.el7

Complete!
[root@3a4dc8cbcad8 /]#
[root@3a4dc8cbcad8 /]#
[root@3a4dc8cbcad8 /]# exit
exit
[root@template logstash]#
[root@template logstash]# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3a4dc8cbcad8 8652b9f0cb4c "/bin/bash" 3 minutes ago Exited (0) 2 minutes ago elastic_jemison
[root@template logstash]#
[root@template logstash]# docker commit 3a4dc8cbcad8 centos-yum_net-tools
sha256:bf3693078aa1a31480f6be5b415b28cec0bcd29dca962a1c57df80295c8fc865
[root@template logstash]#
[root@template logstash]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos-yum_net-tools latest bf3693078aa1 5 seconds ago 327MB
hello-world latest d1165f221234 3 months ago 13.3kB
centos 7 8652b9f0cb4c 7 months ago 204MB
ubuntu 15.10 9b9cb95443b5 4 years ago 137MB
[root@template logstash]#

这样我们的专属镜像 centos-yum_net-tools 就制作好了,以后我们再次进入就可以直接使用netstat相关的命令。