Docker 简介 安装

docker概述 

Docker 是一个开源的应用容器引擎,基于 Go 语言 并遵从Apache2.0协议开源。使用最广泛的开源容器引擎、一种操作系统级的虚拟化技术、依赖于Linux内核特性:Namespace(资源隔离)和Cgroups(资源限制)、一个简单的应用程序打包工具。

Docker 可以让开发者打包他们的应用以及依赖包到一个轻量级、可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现虚拟化。容器是完全使用沙箱机制,相互之间不会有任何接口(类似 iPhone 的 app),更重要的是容器性能开销极低。

Docker 从 17.03 版本之后分为 CE(Community Edition: 社区版) 和 EE(Enterprise Edition: 企业版),我们用社区版就可以了。

 

docker的应用场景

  • Web 应用的自动化打包和发布。

  • 自动化测试和持续集成、发布。

  • 在服务型环境中部署和调整数据库或其他的后台应用。

  • 从头编译或者扩展现有的 OpenShift 或 Cloud Foundry 平台来搭建自己的 PaaS 环境。


docker 的优点

    • 1、简化程序:
      Docker 让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的 Linux 机器上,便可以实现虚拟化。Docker改变了虚拟化的方式,使开发者可以直接将自己的成果放入Docker中进行管理。方便快捷已经是 Docker的最大优势,过去需要用数天乃至数周的 任务,在Docker容器的处理下,只需要数秒就能完成。

    • 2、避免选择恐惧症:
      如果你有选择恐惧症,还是资深患者。那么你可以使用 Docker 打包你的纠结!比如 Docker 镜像;Docker 镜像中包含了运行环境和配置,所以 Docker 可以简化部署多种应用实例工作。比如 Web 应用、后台应用、数据库应用、大数据应用比如 Hadoop 集群、消息队列等等都可以打包成一个镜像部署。

    • 3、节省开支:
      一方面,云计算时代到来,使开发者不必为了追求效果而配置高额的硬件,Docker 改变了高性能必然高价格的思维定势。Docker 与云的结合,让云空间得到更充分的利用。不仅解决了硬件管理的问题,也改变了虚拟化的方式。

 

docker架构

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

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

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

Docker面向对象
容器对象
镜像

Docker 镜像(Images)

Docker 镜像是用于创建 Docker 容器的模板。

Docker 容器(Container)

容器是独立运行的一个或一组应用。

Docker 客户端(Client)

Docker 客户端通过命令行或者其他工具使用 Docker API (https://docs.docker.com/reference/api/docker_remote_api) 与 Docker 的守护进程通信。

Docker 主机(Host)

一个物理或者虚拟的机器用于执行 Docker 守护进程和容器。

Docker 仓库(Registry)

Docker 仓库用来保存镜像,可以理解为代码控制中的代码仓库。

Docker Hub(https://hub.docker.com) 提供了庞大的镜像集合供使用。

 

Docker Machine

Docker Machine是一个简化Docker安装的命令行工具,通过一个简单的命令行即可在相应的平台上安装Docker,比如VirtualBox、 Digital Ocean、Microsoft Azure。

 

 

 

 

 容器 VS 虚拟机

 

 

 

 

 

 

 

 源码安装

一、基础环境
1、操作系统:CentOS 7.6
2、Docker版本:https://download.docker.com/linux/static/stable/x86_64/
3、官方参考文档:https://docs.docker.com/install/linux/docker-ce/binaries/#install-static-binaries

二、Docker安装

1、下载

1
wget https: //download.docker.com/linux/static/stable/x86_64/docker-18.09.4-ce.tgz

2、解压

1
tar -xvf docker-18.09.4-ce.tgz

3、将解压出来的docker文件内容移动到 /usr/bin/ 目录下

cp docker/* /usr/bin/

4、将docker注册为service

1
vim /etc/systemd/system/docker.service
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
[Unit]
Description=Docker Application Container Engine
Documentation=https: //docs.docker.com
After=network-online.target firewalld.service
Wants=network-online.target
  
[Service]
Type=notify
# the default is not to use systemd for cgroups because the delegate issues still
# exists and systemd currently does not support the cgroup feature set required
# for containers run by docker
ExecStart=/usr/bin/dockerd
ExecReload=/bin/kill -s HUP $MAINPID
# Having non-zero Limit*s causes performance problems due to accounting overhead
# in the kernel. We recommend using cgroups to do container-local accounting.
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
# Uncomment TasksMax if your systemd version supports it.
# Only systemd 226 and above support this version.
#TasksMax=infinity
TimeoutStartSec=0
# set delegate yes so that systemd does not reset the cgroups of docker containers
Delegate=yes
# kill only the docker process, not all processes in the cgroup
KillMode=process
# restart the docker process if it exits prematurely
Restart= on -failure
StartLimitBurst=3
StartLimitInterval=60s
  
[Install]
WantedBy=multi-user.target

5、启动

1
2
3
4
5
chmod +x /etc/systemd/system/docker.service #添加文件权限并启动docker
 
systemctl daemon-reload                 #重新加载配置文件
systemctl start docker          #启动Docker
systemctl enable docker.service         #设置开机自启

6、验证

1
2
systemctl status docker         #查看Docker状态
docker -v           #查看Docker版本

 

 

 

 yum安装

 

# 安装依赖包
yum install -y yum-utils device-mapper-persistent-data lvm2
# 添加Docker软件包源
yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo
# 安装Docker CE
yum install -y docker-ce
# 启动Docker服务并设置开机启动
systemctl start docker
systemctl enable docker

 

 

镜像 

1、镜像是什么?

•一个分层存储的文件
•一个软件的环境
•一个镜像可以创建N个容器
•一种标准化的交付
•一个不包含Linux内核而又精简的Linux操作系统
镜像不是一个单一的文件,而是有多层构成。我们可以通过docker history <ID/NAME> 查看镜像中各层内容及大小,每层对应着Dockerfile中的一条指令。Docker镜像默认存储在/var/lib/docker/\<storage-driver\>中。

2、镜像从哪里来?

Docker Hub是由Docker公司负责维护的公共注册中心,包含大量的容器镜像,Docker工具默认从这个公共镜像库下载镜像。
地址:https://hub.docker.com/explore
配置镜像加速器:https://www.daocloud.io/mirror
curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://f1361db2.m.daocloud.io

 

3、镜像与容器联系

如图,容器其实是在镜像的最上面加了一层读写层,在运行容器里文件改动时,会先从镜像里要写的文件复制到容器自己的文件系统中(读写层)。
如果容器删除了,最上面的读写层也就删除了,改动也就丢失了。所以无论多少个容器共享一个镜像,所做的写操作都是从镜像的文件系统中复制过来操作的,并不会修改镜像的源文件,这种方式提高磁盘利用率。若想持久化这些改动,可以通过docker commit 将容器保存成一个新镜像。

4、管理镜像常用操作指令

 

 

[root@localhost overlay2]# docker image ls  #列出所镜像信息
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              7                   9f38484d220f        5 months ago        202MB
[root@localhost overlay2]# 
[root@localhost overlay2]# docker image inspect centos:7  #显示一个或多个镜像详细信息
[
    {
        "Id": "sha256:9f38484d220fa527b1fb19747638497179500a1bed8bf0498eb788229229e6e1",
        "RepoTags": [
            "centos:7"
        ],
        "RepoDigests": [
            "centos@sha256:a799dd8a2ded4a83484bbae769d97655392b3f86533ceb7dd96bbac929809f3c"
        ],
        "Parent": "",
        "Comment": "",
        "Created": "2019-03-14T21:19:53.361167852Z",
        "Container": "958baf5225f586da9c70a21e911a0a875402dd22d83133d78b3b3aa6130e7892",
        "ContainerConfig": {
            "Hostname": "958baf5225f5",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
            "Cmd": [
                "/bin/sh",
                "-c",
                "#(nop) ",
                "CMD [\"/bin/bash\"]"
            ],
            "ArgsEscaped": true,
            "Image": "sha256:294e8d8145287e70f07328cc09d840fad8980b801223321b983442f097aff0d8",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {
                "org.label-schema.build-date": "20190305",
                "org.label-schema.license": "GPLv2",
                "org.label-schema.name": "CentOS Base Image",
                "org.label-schema.schema-version": "1.0",
                "org.label-schema.vendor": "CentOS"
            }
        },
        "DockerVersion": "18.06.1-ce",
        "Author": "",
        "Config": {
            "Hostname": "",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
            "Cmd": [
                "/bin/bash"
            ],
            "ArgsEscaped": true,
            "Image": "sha256:294e8d8145287e70f07328cc09d840fad8980b801223321b983442f097aff0d8",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {
                "org.label-schema.build-date": "20190305",
                "org.label-schema.license": "GPLv2",
                "org.label-schema.name": "CentOS Base Image",
                "org.label-schema.schema-version": "1.0",
                "org.label-schema.vendor": "CentOS"
            }
        },
        "Architecture": "amd64",
        "Os": "linux",
        "Size": 201782942,
        "VirtualSize": 201782942,
        "GraphDriver": {
            "Data": {
                "MergedDir": "/var/lib/docker/overlay2/20f145b94417a6bdb98b3f99679455f43957dc1af24ff157f4d73dc78dbb920a/merged",
                "UpperDir": "/var/lib/docker/overlay2/20f145b94417a6bdb98b3f99679455f43957dc1af24ff157f4d73dc78dbb920a/diff",
                "WorkDir": "/var/lib/docker/overlay2/20f145b94417a6bdb98b3f99679455f43957dc1af24ff157f4d73dc78dbb920a/work"
            },
            "Name": "overlay2"
        },
        "RootFS": {
            "Type": "layers",
            "Layers": [
                "sha256:d69483a6face4499acb974449d1303591fcbb5cdce5420f36f8a6607bda11854"
            ]
        },
        "Metadata": {
            "LastTagTime": "0001-01-01T00:00:00Z"
        }
    }
]
显示该镜像详细信息  
[root@localhost overlay2]# docker image history centos:7
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
9f38484d220f        5 months ago        /bin/sh -c #(nop)  CMD ["/bin/bash"]            0B                  
<missing>           5 months ago        /bin/sh -c #(nop)  LABEL org.label-schema.sc…   0B                  
<missing>           5 months ago        /bin/sh -c #(nop) ADD file:074f2c974463ab38c…   202MB    



[root@localhost ~]# docker image save centos:7 > centos7.tar #导出镜像
[root@localhost ~]# du -sh centos7.tar 
200M    centos7.tar
[root@localhost ~]# docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              7                   9f38484d220f        5 months ago        202MB



[root@localhost ~]# docker ps -a  #查看运行的容器
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES


[root@localhost ~]# docker rm -f $(docker ps -a | awk '{print $1}')#删除所有运行的容器




[root@localhost ~]# docker image rm centos:7 #删除镜像
Untagged: centos:7
Untagged: centos@sha256:a799dd8a2ded4a83484bbae769d97655392b3f86533ceb7dd96bbac929809f3c
Deleted: sha256:9f38484d220fa527b1fb19747638497179500a1bed8bf0498eb788229229e6e1
Deleted: sha256:d69483a6face4499acb974449d1303591fcbb5cdce5420f36f8a6607bda11854



[root@localhost ~]# docker image load < centos7.tar #导入镜像
d69483a6face: Loading layer [==================================================>]  209.5MB/209.5MB
Loaded image: centos:7


[root@localhost ~]# docker image pull mysql:5.7 #下载镜像
5.7: Pulling from library/mysql
[DEPRECATION NOTICE] registry v2 schema1 support will be removed in an upcoming release. Please contact admins of the docker.io registry NOW to avoid future disruption.
9fc222b64b0a: Pull complete 
291e388076f0: Pull complete 
d6634415290b: Pull complete 
1f1e7d852ad4: Pull complete 
125fc05f36e0: Pull complete 
2aed16e5b02f: Pull complete 
5fa9342b7235: Pull complete 
a1e72cc32505: Pull complete 
81225f74ecbe: Pull complete 
b9a45d065520: Pull complete 
a3e7b2fe9950: Pull complete 
Digest: sha256:8fbb73711ebcdd8a76d34b0bbeef80b5b7ef8ca43edbdbc213cede5654ec7b81
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7

 

 

 

 

 

转载于:https://www.cnblogs.com/linux985/p/11389990.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值