Docker初探
1.初识Docker
1.1.What is Docker
1.1.1.官网首页
Debug your app, not your environment
Securely build, share and run any application, anywhere
1.1.2.Docs
https://docs.docker.com/get-started/
Docker is a platform for developers and sysadmins to develop, deploy, and run applications with containers. The use of Linux containers to deploy applications is called containerization. Containers are not new, but their use for easily deploying applications is.
Docker是供开发人员和系统管理员使用容器构建,共享和运行应用程序的平台。 使用容器部署应用程序称为容器化。 容器不是新的,但用于轻松部署应用程序的容器却是新的。
1.1.3.不理解,我太难了
不妨从一个需求开始 :开发好了一个项目study,部署上线
- 远古时代
问题: 成本高、部署慢、浪费资源、硬件限制、不利于迁移扩展 - 虚拟化时代
hypervisor:虚拟化技术
优点 :相对利用好资源,相对容易扩展等。
缺点 :虚拟机太重了,一上来占用较多物理资源,移植性差,资源利用率低等。 - 容器时代
1.1.4.再次理解Docker
Docker is a platform for developers and sysadmins to build, share, and run applications with containers. The use of containers to deploy applications is called containerization. Containers are not new, but their use for easily deploying applications is.
Docker是供开发人员和系统管理员使用容器构建,共享和运行应用程序的平台。 使用容器部署应用程序称为容器化。 容器不是新的,但用于轻松部署应用程序的容器却是新的。
发现还是比较容易理解的,但是这里有一句“Containers are not new”,也就是容器化技术很早就 出现了,比如常见的容器化技术有OpenVZ等。
1.1.5.Docker的优势和应用场景
- 有助于微服务的落地和部署
- 充分利用物理机资源,同时能够整合服务器资源
- 提高开发效率,测试效率,部署效率,有利于DevOps的落地,CI/CD
- 云原生落地,应用更好地迁移 …
1.2.What is Image and Container?
docker engine –》 jvm
image –》 class文件
container –》 Java对象
1.2.1.What is Image?
Why is docker?-> https://www.docker.com/resources/what-container
A Docker container image is a lightweight, standalone, executable package of software that includes everything needed to run an application: code, runtime, system tools, system libraries and settings.
Docker容器映像是轻巧的,独立的,可执行的软件软件包,其中包含运行应用程序所需的一切:代码,运行时,系统工具,系统库和设置。
1.2.2.What is Container?
Why is docker?-> https://www.docker.com/resources/what-container
A container is a standard unit of software that packages up code and all its dependencies so the application runs quickly and reliably from one computing environment to another.
容器是打包代码及其所有依赖项的软件的标准单元,因此应用程序可以从一个计算环境快速可靠地运行到另一个计算环境。
1.2.3 Relation between image and container
Container images become containers at runtime and in the case of Docker containers - images become containers when they run on Docker Engine.
容器映像在运行时会成为容器,对于Docker容器而言-映像在Docker Engine上运行时会成为容器。
1.2.4.View from Docs
从帮助文档的角度看 docker官网->Resources->Docs->Get started->Quickstart->Orientation and setup->Images and containers
Fundamentally, a container is nothing but a running process, with some added encapsulation features applied to it in order to keep it isolated from the host and from other containers. One of the most important aspects of container isolation is that each container interacts with its own, private filesystem; this filesystem is provided by a Docker image. An image includes everything needed to run an application – the code or binary, runtimes, dependencies, and any other filesystem objects required.
从根本上说,一个容器不过是一个正在运行的进程,并对其应用了一些附加的封装功能,以使其与主机和其他容器隔离。 容器隔离的最重要方面之一是每个容器都与自己的私有文件系统进行交互。 该文件系统由Docker映像提供。 映像包括运行应用程序所需的所有内容-代码或二进制文件,运行时,依赖项以及所需的任何其他文件系统对象。
1.3.Containers and virtual machines
从帮助文档的角度看 docker官网->Resources->Docs->Get started->Quickstart->Orientation and setup->Containers and virtual machines
A container runs natively on Linux and shares the kernel of the host machine with other containers. It runs a discrete process, taking no more memory than any other executable, making it lightweight.
By contrast, a virtual machine (VM) runs a full-blown “guest” operating system with virtual access to host resources through a hypervisor. In general, VMs incur a lot of overhead beyond what is being consumed by your application logic.
容器在Linux上本地运行,并与其他容器共享主机的内核。 它运行一个离散进程,不占用任何其他可执行文件更多的内存,从而使其轻巧。
相比之下,虚拟机(VM)运行成熟的“来宾”操作系统,并通过虚拟机管理程序对主机资源进行虚拟访问。 通常,VM会产生大量开销,超出了应用程序逻辑所消耗的开销。
1.4.Docker Engine and Architecture
https://docs.docker.com/engine/docker-overview/
1.4.1.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).
Docker Engine是具有这些主要组件的客户端-服务器应用程序:
- 一种长期运行的程序,称为守护程序进程(dockerd命令)的服务。
- REST API,它指定程序可以用来与守护程序进行通信并指示其操作的接口。
- 命令行界面(CLI)客户端(docker命令)。
1.4.2.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使用客户端-服务器架构。 Docker客户端与Docker守护程序进行对话,该守护程序完成了构建,运行和分发Docker容器的繁重工作。 Docker客户端和守护程序可以在同一系统上运行,也可以将Docker客户端连接到远程Docker守护程序。 Docker客户端和守护程序在UNIX套接字或网络接口上使用REST API进行通信。
1.5.Install
见附件
2.Image and Container
2.1.Image
2.1.1.官方image
https://github.com/docker-library
mysql
https://github.com/docker-library/mysql/blob/master/5.7/Dockerfile
1 | FROM debian:stretch-slim |
2.1.2.Dockerfile
2.1.2.1.FROM
指定基础镜像,比如FROM ubuntu:14.04
1 | FROM ubuntu:14.04 |
2.1.2.2.RUN
在镜像内部执行一些命令,比如安装软件,配置环境等,换行可以使用””
1 | RUN groupadd -r mysql && useradd -r -g mysql mysql |
2.1.2.3.ENV
设置变量的值,ENV MYSQL_MAJOR 5.7,可以通过docker run –e key=value修改,后面可以直接使 用${MYSQL_MAJOR}
1 | ENV MYSQL_MAJOR 5.7 |
2.1.2.4.LABEL
设置镜像标签
1 | LABEL email="732060461@qq.com" |
2.1.2.5.VOLUME
指定数据的挂在目录
1 | VOLUME /var/lib/mysql |
2.1.2.6.COPY
将主机的文件复制到镜像内,如果目录不存在,会自动创建所需要的目录,注意只是复制,不会提取和 解压
1 | COPY docker-entrypoint.sh /usr/local/bin/ |
2.1.2.7.ADD
将主机的文件复制到镜像内,和COPY类似,只是ADD会对压缩文件提取和解压
1 | ADD application.yml /etc/kieran/ |
2.1.2.8.WORKDIR
指定镜像的工作目录,之后的命令都是基于此目录工作,若不存在则创建
1 | WORKDIR /usr/local |
会在/usr/local/tomcat下创建test.txt文件
1 | WORKDIR /root |
会在/root/test下多出一个app.yml文件
2.1.2.9.CMD
容器启动的时候默认会执行的命令,若有多个CMD命令,则最后一个生效
1 | CMD ["mysqld"] |
2.1.2.10.ENTRYPOINT
和CMD的使用类似
1 | ENTRYPOINT ["docker-entrypoint.sh"] |
和CMD的不同:
docker run执行时,会覆盖CMD的命令,而ENTRYPOINT不会
2.1.2.11.EXPOSE
指定镜像要暴露的端口,启动镜像时,可以使用-p将该端口映射给宿主机
1 | EXPOSE 3306 |
2.1.3.Dockerfile实战Spring Boot项目
1 | (1)创建一个Spring Boot项目 |
2.1.4.镜像仓库
2.1.4.1.docker hub
1 | (1)在docker机器上登录 |
2.1.4.2.阿里云docker hub
阿里云docker仓库
https://cr.console.aliyun.com/cn-hangzhou/instances/repositories
参考手册
https://cr.console.aliyun.com/repository/cn-hangzhou/kieran2019/test/details
2.1.4.3.搭建自己的Docker Harbor
1 | (1)访问github上的harbor项目 |
2.1.5.Image常见操作
1 | (1)查看本地image列表 |
2.2.Container
既然container是由image运行起来的,那么是否可以理解为container和image有某种关系?
其实可以理解为container只是基于image之后的layer而已,也就是可以通过docker run image 创建出一个container出来。
2.2.1.container到image
既然container是基于image之上的,想想是否能够由一个container反推出image呢? 肯定是可以的,比如通过docker run运行起一个container出来,这时候对container对一些修 改,然后再生成一个新的image,这时候image的由来就不仅仅只能通过Dockerfile咯。
1 | (1)拉取一个centos image |
可以通过docker commit命令基于一个container重新生成一个image,但是一般得到image的 方式不建议这么做,不然image怎么来的就全然不知道了。
2.2.2.container资源限制
如果不对container的资源做限制,它就会无限制地使用物理机的资源,这样显然是不合适的。
查看资源情况 :
1 | docker stats |
2.2.2.1.内存限制
1 | --memory Memory limit |
2.2.2.2.CPU限制
1 | --cpu-shares 权重 |
2.2.2.3.图形化资源监控
https://github.com/weaveworks/scope
1 | sudo curl -L git.io/scope -o /usr/local/bin/scope |
2.2.3.container常见操作
1 | (1)根据镜像创建容器 |
2.3.底层技术支持
Container是一种轻量级的虚拟化技术,不用模拟硬件创建虚拟机。 Docker是基于Linux Kernel的Namespace、CGroups、UnionFileSystem等技术封装成的一种自 定义容器格式,从而提供一套虚拟运行环境。
Namespace:用来做隔离的,比如pid[进程]、net[网络]、mnt[挂载点]等
CGroups: Controller Groups用来做资源限制,比如内存和CPU等
Union file systems:用来做image和container
3.附件
3.1 mac搭建docker、docker-compose环境
https://juejin.im/post/5affada36fb9a07abf72c6e5
3.2 docker-compose.yml配置详解
https://juejin.im/post/5aed4a776fb9a07a9918bb42