动手玩转docker

六月骄阳 六月骄阳     2022-11-18     680

关键词:

在学习docker之前,先了解一下什么是docker,以及docker技术存在的意义。

Docker是PaaS供应商dotCloud开源的一个基于LXC 的高级容器引擎,源代码托管在 GitHub 上, 基于Go语言开发并遵从Apache 2.0协议开源。Docker提供了一种在安全、可重复的环境中自动部署软件的方式,它的出现拉开了基于云计算平台发布产品方式的变革序幕。

注:英文不好的话,就不要学习Docker了,下面粘贴一部分官方介绍

Docker overview

Estimated reading time: 5 minutes

Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure so you can deliver software quickly. With Docker, you can manage your infrastructure in the same ways you manage your applications. By taking advantage of Docker’s methodologies for shipping, testing, and deploying code quickly, you can significantly reduce the delay between writing code and running it in production.

The Docker platform

Docker provides the ability to package and run an application in a loosely isolated environment called a container. The isolation and security allow you to run many containers simultaneously on a given host. Containers are lightweight because they don’t need the extra load of a hypervisor, but run directly within the host machine’s kernel. This means you can run more containers on a given hardware combination than if you were using virtual machines. You can even run Docker containers within host machines that are actually virtual machines!

Docker provides tooling and a platform to manage the lifecycle of your containers:

  • Develop your application and its supporting components using containers.
  • The container becomes the unit for distributing and testing your application.
  • When you’re ready, deploy your application into your production environment, as a container or an orchestrated service. This works the same whether your production environment is a local data center, a cloud provider, or a hybrid of the two.

Docker Engine

Docker Engine is a client-server application with these major components:

  • A server which is a type of long-running program called a daemon process (the dockerd command).

  • A REST API which specifies interfaces that programs can use to talk to the daemon and instruct it what to do.

  • A command line interface (CLI) client (the docker command).

技术分享图片

The CLI uses the Docker REST API to control or interact with the Docker daemon through scripting or direct CLI commands. Many other Docker applications use the underlying API and CLI.

The daemon creates and manages Docker objects, such as images, containers, networks, and volumes.

Note: Docker is licensed under the open source Apache 2.0 license.

For more details, see Docker Architecture below.

What can I use Docker for?

Fast, consistent delivery of your applications

Docker streamlines the development lifecycle by allowing developers to work in standardized environments using local containers which provide your applications and services. Containers are great for continuous integration and continuous delivery (CI/CD) workflows.

Consider the following example scenario:

  • Your developers write code locally and share their work with their colleagues using Docker containers.
  • They use Docker to push their applications into a test environment and execute automated and manual tests.
  • When developers find bugs, they can fix them in the development environment and redeploy them to the test environment for testing and validation.
  • When testing is complete, getting the fix to the customer is as simple as pushing the updated image to the production environment.

Responsive deployment and scaling

Docker’s container-based platform allows for highly portable workloads. Docker containers can run on a developer’s local laptop, on physical or virtual machines in a data center, on cloud providers, or in a mixture of environments.

Docker’s portability and lightweight nature also make it easy to dynamically manage workloads, scaling up or tearing down applications and services as business needs dictate, in near real time.

Running more workloads on the same hardware

Docker is lightweight and fast. It provides a viable, cost-effective alternative to hypervisor-based virtual machines, so you can use more of your compute capacity to achieve your business goals. Docker is perfect for high density environments and for small and medium deployments where you need to do more with fewer resources.

Docker architecture

Docker uses a client-server architecture. The Docker client talks to the Docker daemon, which does the heavy lifting of building, running, and distributing your Docker containers. The Docker client and daemon can run on the same system, or you can connect a Docker client to a remote Docker daemon. The Docker client and daemon communicate using a REST API, over UNIX sockets or a network interface.

技术分享图片

好吧,我还是比较仁慈的,下面开始上中文

Docker产生的目的就是为了解决以下问题:

1) 环境管理复杂: 从各种OS到各种中间件再到各种App,一款产品能够成功发布,作为开发者需要关心的东西太多,且难于管理,这个问题在软件行业中普遍存在并需要直接面对。Docker可以简化部署多种应用实例工作,比如Web应用、后台应用、数据库应用、大数据应用比如Hadoop集群、消息队列等等都可以打包成一个Image部署。

2) 云计算时代的到来: AWS的成功, 引导开发者将应用转移到云上, 解决了硬件管理的问题,然而软件配置和管理相关的问题依然存在。Docker的出现正好能帮助软件开发者开阔思路,尝试新的软件管理方法来解决这个问题。

3) 虚拟化手段的变化: 云时代采用标配硬件来降低成本,采用虚拟化手段来满足用户按需分配的资源需求以及保证可用性和隔离性。然而无论是KVM还是Xen,在 Docker 看来都在浪费资源,因为用户需要的是高效运行环境而非OS, GuestOS既浪费资源又难于管理, 更加轻量级的LXC更加灵活和快速。如图所示:

技术分享图片

4) LXC的便携性: LXC在 Linux 2.6 的 Kernel 里就已经存在了,但是其设计之初并非为云计算考虑的,缺少标准化的描述手段和容器的可便携性,决定其构建出的环境难于分发和标准化管理(相对于KVM之类image和snapshot的概念)。Docker就在这个问题上做出了实质性的创新方法。

 

1、Docker 概念
1.1 容器技术
Linux 容器技术很早就有了,比较有名的是被集成到主流 Linux 内核中的 LXC 项目。容器通过对操作系统的资源访问进行限制,构建成独立的资源池,让应用运行在一个相对隔离的空间里,同时容器间也可以进行通信。

容器技术对比虚拟化技术,容器比虚拟化更轻量级,对资源的消耗小很多。容器操作也更快捷,启动和停止都要比虚拟机快。但容器需要与主机共享操作系统内核,不能像虚拟机那样运行独立的内核。

Docker 是一个基于 LXC 技术构建的容器引擎,基于 GO 语言开发,遵循 Apache2.0 协议开源。Docker 的发展得益于为使用者提供了更好的容器操作接口。包括一系列的容器,镜像,网络等管理工具,可以让用户简单的创建和使用容器。

Docker 支持将应用打包进一个可以移植的容器中,重新定义了应用开发,测试,部署上线的过程,核心理念就是 Build once, Run anywhere。

Docker 容器技术的典型应用场景是开发运维上提供持续集成和持续部署的服务。

下面我们开始介绍 Docker 中的几个基本概念。

1.2 镜像
Docker 的镜像概念类似于虚拟机里的镜像,是一个只读的模板,一个独立的文件系统,包括运行容器所需的数据,可以用来创建新的容器。

镜像可以基于 Dockerfile 构建,Dockerfile 是一个描述文件,里面包含若干条命令,每条命令都会对基础文件系统创建新的层次结构。

用户可以通过编写 Dockerfile 创建新的镜像,也可以直接从类似 github 的 Docker Hub 上下载镜像使用。

1.3 容器
Docker 容器是由 Docker 镜像创建的运行实例。Docker 容器类似虚拟机,可以支持的操作包括启动,停止,删除等。每个容器间是相互隔离的,但隔离的效果比不上虚拟机。容器中会运行特定的应用,包含特定应用的代码及所需的依赖文件。

在 Docker 容器中,每个容器之间的隔离使用 Linux 的 CGroups 和 Namespaces 技术实现的。其中 CGroups 对 CPU,内存,磁盘等资源的访问限制,Namespaces 提供了环境的隔离。

1.4 仓库
如果你使用过 git 和 github 就很容易理解 Docker 的仓库概念。Docker 仓库相当于一个 github 上的代码库。

Docker 仓库是用来包含镜像的位置,Docker 提供一个注册服务器(Registry)来保存多个仓库,每个仓库又可以包含多个具备不同 tag 的镜像。Docker 运行中使用的默认仓库是 Docker Hub 公共仓库。

仓库支持的操作类似 git,创建了新的镜像后,我们可以 push 提交到仓库,也可以从指定仓库 pull 拉取镜像到本地。






动手学docker

背景动手学 docker最近,终于完成了 动手学docker 系列的编写。动手学docker 是动手学系列 的首个系列。如果反馈的效果不错,后续还将推出 动手学 devops动手学 kubernetes动手学 istio 等系列。动手学系列 的构思来源... 查看详情

[玩转docker] kafka的docker容器化配置

参考技术A注:可以根据环境变量对broker的server.properties里的配置进行定制配置如果要对broker进行更多配置,比如关闭自动创建topic根据如下规则,其他配置依次类推: 查看详情

在windows中玩转dockertoolbox

...大数据,所以从Docker入手,下面介绍一下在Windows下怎么玩转Docker。Docker本身在Windows下有两个软件,一个就是Docker,另一个是DockerToolbox。这里我选择的是DockerToolbox,为什么呢?参见官方文档:https://blog.docker.com/2015/08/docker-toolbox/... 查看详情

14《每天5分钟玩转docker容器技术》学习--一张图了解docker网络

查看详情

8《每天5分钟玩转docker容器技术》学习-docker命令infoversion

a) docker info 命令查看docker系统信息。docker info [OPTIONS]a) docker version 命令docker version :显示 Docker 版本信息。docker version [OPTIONS] 查看详情

在windows中玩转dockertoolbox转载

...大数据,所以从Docker入手,下面介绍一下在Windows下怎么玩转Docker。Docker本身在Windows下有两个软件,一个就是Docker,另一个是DockerToolbox。这里我选择的是DockerToolbox,为什么呢?参见官方文档:https://blog.docker.com/2015/08/docker-toolbox/... 查看详情

《自己动手写docker》pdf

...ker原理介绍或代码剖析的书籍,本书旨在提供给读者一条动手路线,一步一步地实现Docker的隔离性,构建Docker的镜像、容器的生命周期及Docker的网络等。本书涉及的代码都托管在GitHub上,读者可以对照书中的步骤从代码层面学习 查看详情

九爷带你玩转docker五大监控

轻量级虚拟化容器Docker,自发布以来便广受业界关注,在开源界和企业界掀起了一阵风。Docker容器相对于VM有以下几个优势:启动速度快;资源利用率高;性能开销小。650)this.width=650;"src="https://segmentfault.com/image?src=http://news.oneapm.... 查看详情

9《每天5分钟玩转docker容器技术》学习-docker命令之镜像仓库

a) Docker login/logout 命令docker login : 登陆到一个Docker镜像仓库,如果未指定镜像仓库地址,默认为官方仓库 Docker Hubdocker logout : 登出一个Docker镜像仓库,如果未指定镜像仓库地址,默认为官... 查看详情

带你玩转docker容器技术之镜像

镜像是Docker容器的基石,容器是镜像的运行实例,有了镜像才能启动容器。为什么我们要讨论镜像的内部结构?如果只是使用镜像,当然不需要了解,直接通过docker命令下载和运行就可以了。但如果我们想创建自己的镜像,或者... 查看详情

新书发布《每天5分钟玩转docker容器技术》

...的询问,今天终于可以给大家一个交代了。《每天5分钟玩转Docker容器技术》现已在各大书城上架。650)this.width=650;"title="http://7xo6kd.com1.z0.glb.clouddn.com/upload-ueditor-image-20170925-1506345649234050183.png"src="http://7xo6kd 查看详情

在docker中使用flannel-每天5分钟玩转docker容器技术(60)

上一节我们安装和配置了flannel,本节在Docker中使用flannel。配置Docker连接flannel编辑host1的Docker配置文件/etc/systemd/system/docker.service,设置 --bip 和 --mtu。650)this.width=650;"title="http://7xo6kd.com1.z0.glb.cloudd 查看详情

在docker中使用flannel-每天5分钟玩转docker容器技术(60)

上一节我们安装和配置了flannel,本节在Docker中使用flannel。配置Docker连接flannel编辑host1的Docker配置文件/etc/systemd/system/docker.service,设置 --bip 和 --mtu。这两个参数的值必须与/run/flannel/subnet.env中 FLANNEL_SUBNET  查看详情

docker镜像小结-每天5分钟玩转docker容器技术(21)

本节我们对Docker镜像做个小结。这一部分我们首先讨论了镜像的分层结构,然后学习了如何构建镜像,最后实践使用DockerHub和本地registry。下面是镜像的常用操作子命令:images  显示镜像列表history 显示镜像构建历史com... 查看详情

docker镜像小结-每天5分钟玩转docker容器技术(21)

本节我们对Docker镜像做个小结。这一部分我们首先讨论了镜像的分层结构,然后学习了如何构建镜像,最后实践使用DockerHub和本地registry。下面是镜像的常用操作子命令:images  显示镜像列表history 显示镜像构建历史com... 查看详情

10《每天5分钟玩转docker容器技术》学习-docker命令之本地镜像管理

a) Docker images 命令docker images : 列出本地镜像。docker images [OPTIONS] [REPOSITORY[:TAG]]-a :列出本地所有的镜像(含中间映像层,默认情况下,过滤掉中间映像层);--digests :显示镜像的摘要信息... 查看详情

docker的网络通讯,容器卷,通信卷~~~玩转它!!(代码片段)

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档文章目录一、Docker网络模式1、docker容器的虚拟网关2、Docker的网络模式3、Docker自定义网络二、Docker数据卷1、数据卷2、数据卷容器3、容器互联(... 查看详情

docker的网络通讯,容器卷,通信卷~~~玩转它!!(代码片段)

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档文章目录一、Docker网络模式1、docker容器的虚拟网关2、Docker的网络模式3、Docker自定义网络二、Docker数据卷1、数据卷2、数据卷容器3、容器互联(... 查看详情