
Docker Note

concept
Dockerfile
- a text document that contains all the commands a user could call on the command line to assemble an image.
image(镜像)
- 根据 dockerfile 构建 image
- Docker 镜像 是一个特殊的文件系统,除了提供容器运行时所需的程序、库、资源、配置等文件外,还包含了一些为运行时准备的一些配置参数(如匿名卷、环境变量、用户等)。
\
Container(容器)
- 镜像(
Image
)和容器(Container
)的关系,就像是面向对象程序设计中的类
和实例
一样
Registry(仓库)
- 一个集中的存储、分发镜像的服务,供其他用户拉取、使用镜像
- 分为公开仓库和私有仓库
docker-compose
- 用于定义和配置多个容器 Docker 应用,负责实现对 Docker 容器集群的快速编排
- 在日常工作中,会碰到需要多个容器相互配合来完成某项任务的情况。例如要实现一个 Web 项目,除了 Web 服务容器本身,往往还需要再加上后端的数据库服务容器,甚至还包括负载均衡容器等。
Compose
恰好满足了这样的需求。它允许用户通过一个单独的 docker-compose.yml
模板文件(YAML 格式)来定义一组相关联的应用容器为一个项目(project)。
Compose
中有两个重要的概念:
- 服务 (
service
):一个应用的容器,实际上可以包括若干运行相同镜像的容器实例。 - 项目 (
project
):由一组关联的应用容器组成的一个完整业务单元,在docker-compose.yml
文件中定义。
目前 Docker 官方用 GO 语言 重写 了 Docker Compose,并将其作为了 docker cli 的子命令,称为 Compose V2
。你可以参照官方文档安装,然后将熟悉的 docker-compose
命令替换为 docker compose
,即可使用 Docker Compose
Workflow(工作流)
1. 编写dockerfile
2. Build image
command:docker build -t [image:name] .
3. Run image
command:docker run -p host_port:container_port IMAGE
How to write Dockerfile
基础命令
1 | From:指定基础镜像,一般FROM OS,比如CentOS,在此基础上构建镜像,比如搭建web服务等 |
1 | 运行指令RUN,Build Image时执行 |
1 | 运行指令CMD,Container启动时执行 |
镜像分层
dockerfile编写习惯不好会导致过多的镜像分层,拖慢构建速度。
1 | FROM debian:stretch |
1 | FROM debian:stretch |
对比以上两种编写方式,第二种是正确的编写方式:
- 其将7层简化为1层
- 在结尾处,添加了清理工作的命令,删除了编译构建所需要的依赖/工具、压缩包、apt缓存等。
docker-compose
1 | version: '3' |
Here’s an explanation of the Docker Compose example file above:
version: '3'
: This specifies that we’re using version 3 of the Docker Compose file format.services:
: This section defines the different services (containers) that make up your application.web:
: This is the name of your first service, presumably a web application.build: .
: This tells Docker to build an image using the Dockerfile in the current directory.ports:
: This maps port 5000 on the host to port 5000 in the container.
redis:
: This is the name of your second service, a Redis database.image: "redis:alpine"
: This tells Docker to use the official Redis image based on Alpine Linux.
- Title: Docker Note
- Author: Aoi Komiya
- Created at: 2024-04-30 09:52:41
- Updated at: 2024-10-06 17:23:40
- Link: https://blog.komiya.monster/2024/04/30/docker_note/
- License: This work is licensed under CC BY-NC-SA 4.0.