Docker Note

Docker Note

Aoi Komiya Lv3

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

commanddocker build -t [image:name] .

3. Run image

commanddocker run -p host_port:container_port IMAGE

How to write Dockerfile

基础命令

1
2
3
4
5
6
7
8
9
From:指定基础镜像,一般FROM OS,比如CentOS,在此基础上构建镜像,比如搭建web服务等

LABEL:注释

WORKDIR: 类似cd,不同的是,若没有该目录就创建

COPY:仅拷贝,支持Go通配符
ADD:拷贝+其他操作(解压、创建目录……)

1
2
3
4
5
6
7
8
9
10
11
12
13
运行指令RUN,Build Image时执行

shell格式:

RUN yum -y install vim


EXEC格式:

RUN ["yum","-y","install","vim"]


二者区别:shell创建子进程、EXEC不会;推荐EXEC
1
运行指令CMD,Container启动时执行

镜像分层

dockerfile编写习惯不好会导致过多的镜像分层,拖慢构建速度。

1
2
3
4
5
6
7
8
9
FROM debian:stretch

RUN apt-get update
RUN apt-get install -y gcc libc6-dev make wget
RUN wget -O redis.tar.gz "http://download.redis.io/releases/redis-5.0.3.tar.gz"
RUN mkdir -p /usr/src/redis
RUN tar -xzf redis.tar.gz -C /usr/src/redis --strip-components=1
RUN make -C /usr/src/redis
RUN make -C /usr/src/redis install
1
2
3
4
5
6
7
8
9
10
11
12
13
14
FROM debian:stretch

RUN set -x; buildDeps='gcc libc6-dev make wget' \
&& apt-get update \
&& apt-get install -y $buildDeps \
&& wget -O redis.tar.gz "http://download.redis.io/releases/redis-5.0.3.tar.gz" \
&& mkdir -p /usr/src/redis \
&& tar -xzf redis.tar.gz -C /usr/src/redis --strip-components=1 \
&& make -C /usr/src/redis \
&& make -C /usr/src/redis install \
&& rm -rf /var/lib/apt/lists/* \
&& rm redis.tar.gz \
&& rm -r /usr/src/redis \
&& apt-get purge -y --auto-remove $buildDeps

对比以上两种编写方式,第二种是正确的编写方式:

  • 其将7层简化为1层
  • 在结尾处,添加了清理工作的命令,删除了编译构建所需要的依赖/工具、压缩包、apt缓存等。

docker-compose

1
2
3
4
5
6
7
8
9
version: '3'

services:
  web:
    build: .
    ports:
     - "5000:5000"
  redis:
    image: "redis:alpine"

Here’s an explanation of the Docker Compose example file above:

  1. version: '3': This specifies that we’re using version 3 of the Docker Compose file format.
  2. services:: This section defines the different services (containers) that make up your application.
  3. 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.
  4. 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.