Docker概念和安装

基本概念

镜像(Image)

Docker 镜像是一个特殊的文件系统,除了提供容器运行时所需的程序、库、资源、配置等文件外,还包含了一些为运行时准备的一些配置参数(如匿名卷、环境变量、用户等)。镜像不包含任何动态数据,其内容在构建之后也不会被改变。

容器(Container)

镜像(Image)和容器(Container)的关系,就像是面向对象程序设计中的类和实例一样,镜像是静态的定义,容器是镜像运行时的实体。容器可以被创建、启动、停止、删除、暂停等。
容器的实质是进程,但与直接在宿主执行的进程不同,容器进程运行于属于自己的独立的 命名空间。因此容器可以拥有自己的 root文件系统、自己的网络配置、自己的进程空间,甚至自己的用户 ID 空间。容器内的进程是运行在一个隔离的环境里,使用起来,就好像是在一个独立于宿主的系统下操作一样。这种特性使得容器封装的应用比直接在宿主运行更加安全。

仓库(Repository)

镜像构建完成后,可以很容易的在当前宿主上运行,但是,如果需要在其它服务器上使用这个镜像,我们就需要一个集中的存储、分发镜像的服务,Docker Registry 就是这样的服务。

Docker的安装

安装linux虚拟机

  1. 安装常见的虚拟机软件,如:VMWare、VirtualBox

  2. 安装新系统或者导入虚拟机文件centos等

  3. 启动虚拟机,登录操作系统

  4. 准备客户端SSH、SmarTTY、Xshell等连上Linux服务器

  5. 设置虚拟机的网路

    • 桥接网络
    • 选好网卡
    • 接入网线
  6. 设置好网络后重启虚拟机或者使用Shell命令重启网络

    1
    service network restart
  7. 查看Linux的IP地址

    1
    ip addr
  8. 使用客户端连接上Linux服务器

安装Dokcer

事前准备
  • 查看CentOS版本:uname -r
  • Docker要求CentOS系统的内核版本要高于3.10,如果低于则要升级软件包和内核:yum update
安装Docker
  1. 这里遇到了坑,先是使用老方法

    1
    2
    yum install docker
    service docker start

    发现Docker启动失败

  2. 后查询得知Docker官方有安装的新方法

    1
    2
    3
    yum remove docker
    yum remove docker -selinux
    vim /etc/yum.repos.d/docker.repo
    1
    2
    3
    4
    5
    name=Docker Repository
    baseurl=https://yum.dockerproject.org/repo/main/centos/$releasever/
    enabled=1
    gpgcheck=1
    gpgkey=https://yum.dockerproject.org/gpg
    1
    yum install docker-engine
    1
    service docker start

    成功启动,查看docker的版本

    1
    docker version
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    [root@localhost ~]# docker version
    Client:
    Version: 18.09.0
    API version: 1.39
    Go version: go1.10.4
    Git commit: 4d60db4
    Built: Wed Nov 7 00:48:22 2018
    OS/Arch: linux/amd64
    Experimental: false

    Server: Docker Engine - Community
    Engine:
    Version: 18.09.0
    API version: 1.39 (minimum version 1.12)
    Go version: go1.10.4
    Git commit: 4d60db4
    Built: Wed Nov 7 00:19:08 2018
    OS/Arch: linux/amd64
    Experimental: false

    还有一种方法:

    1
    curl -fsSL https://get.docker.com/ | sh
  3. 将Docker服务设为开机启动:systemctl enable docker

基本操作命令

操作 命令 说明
检索 docker search 关键字 在docker hub上检索镜像的详情信息
拉取 docker pull 镜像名:tag :tag:软件版本,默认是latest
镜像列表 docker images 查看本地所有镜像详情
镜像删除 docker rmi image-id 删除指定的本地镜像
容器删除 docker rm base 删除base容器,如果base正在运行,则可以使用-f进行强制删除
显示容器 docker ps -a 显示当前宿主机所有容器;没有-a表示显示当前宿主机正在运行的容器
运行容器 docker run -d -p 宿主机端口:容器端口 -name 容器名 镜像名 -d 表示以后台方式运行 -p代表映射端口 -name代表容器名
以交互式命令进入容器 docker exec -it 容器名 /bin/bash 以交互式命令进入容器,可以执行/bin/bash命令
停止容器 docker stop 容器名/容器ID 选定容器停止
启动容器 docker start 容器名/容器ID 选定容器启动
容器日志 docker logs 容器名/容器ID 容器的日志查看
防火墙 service firewalld status 查看防火墙状态
防火墙 service firewalld stop 关闭防火墙
强制关闭docker killall Dokcer 关闭Dokcer

实际操作记录(展示部分)

查看镜像列表

1
2
3
4
5
6
7
8
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
registry.docker-cn.com/library/mongo latest 10dce532716f 2 months ago 393MB
redis latest ce25c7293564 2 months ago 95MB
mysql latest f991c20cb508 3 months ago 486MB
registry.docker-cn.com/library/rabbitmq 3-management d69a5113ceae 4 months ago 149MB
registry.docker-cn.com/library/zookeeper latest f336949ce7a1 4 months ago 148MB
registry.docker-cn.com/library/elasticsearch latest 5acf0e8da90b 5 months ago 486MB

显示容器

1
2
3
4
5
6
7
8
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d1cb816cdc88 10dce532716f "docker-entrypoint.s…" 2 months ago Exited (0) 2 months ago myMongo
f88e287867ce f336949ce7a1 "/docker-entrypoint.…" 2 months ago Exited (143) 7 minutes ago myZooKeeper
58cfffc74b1b 5acf0e8da90b "/docker-entrypoint.…" 2 months ago Exited (143) 2 months ago myES
57d2aa554d9e d69a5113ceae "docker-entrypoint.s…" 2 months ago Exited (0) 2 months ago myRabbitMQ
fd0392de1d53 redis:latest "docker-entrypoint.s…" 2 months ago Exited (0) 2 months ago myRedis
5757e7f9c83d mysql:latest "docker-entrypoint.s…" 2 months ago Exited (0) 2 months ago mysql01

启动容器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d1cb816cdc88 10dce532716f "docker-entrypoint.s…" 2 months ago Exited (0) 2 months ago myMongo
f88e287867ce f336949ce7a1 "/docker-entrypoint.…" 2 months ago Exited (143) 7 minutes ago myZooKeeper
58cfffc74b1b 5acf0e8da90b "/docker-entrypoint.…" 2 months ago Exited (143) 2 months ago myES
57d2aa554d9e d69a5113ceae "docker-entrypoint.s…" 2 months ago Exited (0) 2 months ago myRabbitMQ
fd0392de1d53 redis:latest "docker-entrypoint.s…" 2 months ago Exited (0) 2 months ago myRedis
5757e7f9c83d mysql:latest "docker-entrypoint.s…" 2 months ago Exited (0) 2 months ago mysql01
[root@localhost ~]# docker start 5757e7f9c83d
5757e7f9c83d
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d1cb816cdc88 10dce532716f "docker-entrypoint.s…" 2 months ago Exited (0) 2 months ago myMongo
f88e287867ce f336949ce7a1 "/docker-entrypoint.…" 2 months ago Exited (143) 9 minutes ago myZooKeeper
58cfffc74b1b 5acf0e8da90b "/docker-entrypoint.…" 2 months ago Exited (143) 2 months ago myES
57d2aa554d9e d69a5113ceae "docker-entrypoint.s…" 2 months ago Exited (0) 2 months ago myRabbitMQ
fd0392de1d53 redis:latest "docker-entrypoint.s…" 2 months ago Exited (0) 2 months ago myRedis
5757e7f9c83d mysql:latest "docker-entrypoint.s…" 2 months ago Up 4 seconds 0.0.0.0:3306->3306/tcp, 33060/tcp mysql01

停止容器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d1cb816cdc88 10dce532716f "docker-entrypoint.s…" 2 months ago Exited (0) 2 months ago myMongo
f88e287867ce f336949ce7a1 "/docker-entrypoint.…" 2 months ago Exited (143) 10 minutes ago myZooKeeper
58cfffc74b1b 5acf0e8da90b "/docker-entrypoint.…" 2 months ago Exited (143) 2 months ago myES
57d2aa554d9e d69a5113ceae "docker-entrypoint.s…" 2 months ago Exited (0) 2 months ago myRabbitMQ
fd0392de1d53 redis:latest "docker-entrypoint.s…" 2 months ago Exited (0) 2 months ago myRedis
5757e7f9c83d mysql:latest "docker-entrypoint.s…" 2 months ago Up About a minute 0.0.0.0:3306->3306/tcp, 33060/tcp mysql01
[root@localhost ~]# docker stop 5757e7f9c83d
5757e7f9c83d
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d1cb816cdc88 10dce532716f "docker-entrypoint.s…" 2 months ago Exited (0) 2 months ago myMongo
f88e287867ce f336949ce7a1 "/docker-entrypoint.…" 2 months ago Exited (143) 11 minutes ago myZooKeeper
58cfffc74b1b 5acf0e8da90b "/docker-entrypoint.…" 2 months ago Exited (143) 2 months ago myES
57d2aa554d9e d69a5113ceae "docker-entrypoint.s…" 2 months ago Exited (0) 2 months ago myRabbitMQ
fd0392de1d53 redis:latest "docker-entrypoint.s…" 2 months ago Exited (0) 2 months ago myRedis
5757e7f9c83d mysql:latest "docker-entrypoint.s…" 2 months ago Exited (0) 2 seconds ago mysql01

以交互式命令进入容器

1
2
3
4
5
6
7
8
9
10
11
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d1cb816cdc88 10dce532716f "docker-entrypoint.s…" 2 months ago Exited (0) 2 months ago myMongo
f88e287867ce f336949ce7a1 "/docker-entrypoint.…" 2 months ago Exited (143) 13 minutes ago myZooKeeper
58cfffc74b1b 5acf0e8da90b "/docker-entrypoint.…" 2 months ago Exited (143) 2 months ago myES
57d2aa554d9e d69a5113ceae "docker-entrypoint.s…" 2 months ago Exited (0) 2 months ago myRabbitMQ
fd0392de1d53 redis:latest "docker-entrypoint.s…" 2 months ago Exited (0) 2 months ago myRedis
5757e7f9c83d mysql:latest "docker-entrypoint.s…" 2 months ago Up 5 seconds 0.0.0.0:3306->3306/tcp, 33060/tcp mysql01
[root@localhost ~]# docker exec -it 5757e7f9c83d /bin/bash
root@5757e7f9c83d:/# ls
bin boot dev docker-entrypoint-initdb.d entrypoint.sh etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var

容器日志

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
[root@localhost ~]# docker logs 5757e7f9c83d
Initializing database
2019-03-04T04:25:12.905282Z 0 [Warning] [MY-011070] [Server] 'Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. Consider not using this option as it' is deprecate
d and will be removed in a future release.
2019-03-04T04:25:12.905383Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.13) starting as process 1
2019-03-04T04:25:13.505452Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
2019-03-04T04:25:13.508712Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a differe
nt directory.
2019-03-04T04:25:13.517670Z 0 [Warning] [MY-010315] [Server] 'user' entry 'mysql.infoschema@localhost' ignored in --skip-name-resolve mode.
2019-03-04T04:25:13.517718Z 0 [Warning] [MY-010315] [Server] 'user' entry 'mysql.session@localhost' ignored in --skip-name-resolve mode.
2019-03-04T04:25:13.517816Z 0 [Warning] [MY-010315] [Server] 'user' entry 'mysql.sys@localhost' ignored in --skip-name-resolve mode.
2019-03-04T04:25:13.517844Z 0 [Warning] [MY-010315] [Server] 'user' entry 'root@localhost' ignored in --skip-name-resolve mode.
2019-03-04T04:25:13.517907Z 0 [Warning] [MY-010323] [Server] 'db' entry 'performance_schema mysql.session@localhost' ignored in --skip-name-resolve mode.
2019-03-04T04:25:13.517925Z 0 [Warning] [MY-010323] [Server] 'db' entry 'sys mysql.sys@localhost' ignored in --skip-name-resolve mode.
2019-03-04T04:25:13.518133Z 0 [Warning] [MY-010311] [Server] 'proxies_priv' entry '@ root@localhost' ignored in --skip-name-resolve mode.
2019-03-04T04:25:13.530263Z 0 [Warning] [MY-010330] [Server] 'tables_priv' entry 'user mysql.session@localhost' ignored in --skip-name-resolve mode.
2019-03-04T04:25:13.530294Z 0 [Warning] [MY-010330] [Server] 'tables_priv' entry 'sys_config mysql.sys@localhost' ignored in --skip-name-resolve mode.
2019-03-04T04:25:13.535349Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.13' socket: '/var/run/mysqld/mysqld.sock' port: 3306 MySQL Community Server - G
PL.
2019-03-04T04:25:13.594737Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Socket: '/var/run/mysqld/mysqlx.sock' bind-address: '::' port: 33060
2019-03-04T04:27:05.418852Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.13) MySQL Community Server - GPL.
2019-03-04T04:29:28.981838Z 0 [Warning] [MY-011070] [Server] 'Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. Consider not using this option as it' is deprecate
d and will be removed in a future release.
2019-03-04T04:29:28.981969Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.13) starting as process 1
2019-03-04T04:29:29.334515Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
2019-03-04T04:29:29.336641Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a differe
nt directory.
2019-03-04T04:29:29.344550Z 0 [Warning] [MY-010315] [Server] 'user' entry 'mysql.infoschema@localhost' ignored in --skip-name-resolve mode.
2019-03-04T04:29:29.344601Z 0 [Warning] [MY-010315] [Server] 'user' entry 'mysql.session@localhost' ignored in --skip-name-resolve mode.
2019-03-04T04:29:29.344616Z 0 [Warning] [MY-010315] [Server] 'user' entry 'mysql.sys@localhost' ignored in --skip-name-resolve mode.
2019-03-04T04:29:29.344626Z 0 [Warning] [MY-010315] [Server] 'user' entry 'root@localhost' ignored in --skip-name-resolve mode.
2019-03-04T04:29:29.344666Z 0 [Warning] [MY-010323] [Server] 'db' entry 'performance_schema mysql.session@localhost' ignored in --skip-name-resolve mode.
2019-03-04T04:29:29.344677Z 0 [Warning] [MY-010323] [Server] 'db' entry 'sys mysql.sys@localhost' ignored in --skip-name-resolve mode.
2019-03-04T04:29:29.344693Z 0 [Warning] [MY-010311] [Server] 'proxies_priv' entry '@ root@localhost' ignored in --skip-name-resolve mode.
2019-03-04T04:29:29.353585Z 0 [Warning] [MY-010330] [Server] 'tables_priv' entry 'user mysql.session@localhost' ignored in --skip-name-resolve mode.
2019-03-04T04:29:29.353626Z 0 [Warning] [MY-010330] [Server] 'tables_priv' entry 'sys_config mysql.sys@localhost' ignored in --skip-name-resolve mode.
2019-03-04T04:29:29.359218Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.13' socket: '/var/run/mysqld/mysqld.sock' port: 3306 MySQL Community Server - G
PL.
2019-03-04T04:29:29.499821Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Socket: '/var/run/mysqld/mysqlx.sock' bind-address: '::' port: 33060

小技巧

1
2
docker pull命令下载镜像都是从国外的服务器下载,以下方法可以加速下载
docker pull [registry.docker-cn.com/library]/(redis,mysql,...)

其他命令

删除所有未使用的数据

1
docker system prune

删除所有未使用的volumes

1
docker volume prune

更好的学习当然是看文档介绍啦~~~