Skip to content

镜像构建

前提条件

在构建镜像之前,确保你的项目目录中包含以下文件:

  • Dockerfile
  • 其他需要在镜像中添加的文件或目录

命令

bash
docker build -t <镜像名>:<> <构建上下文路>
  • <镜像名称>:镜像的名称,如 my-image
  • <标签>:镜像的标签,如 latest
  • <构建上下文路径>:Dockerfile 所在的路径,默认是当前目录

构建过程

  • Docker 会读取 Dockerfile 文件。
  • 按照指令顺序逐层构建镜像。
  • 每执行一个指令,都会生成一个临时的中间镜像层。
  • 最终生成一个完整的镜像。

使用 PHP 完整示例 Dockerfile

dockerfile
# 基础镜像
FROM php:7.4-fpm

# 设置工作目录
WORKDIR /var/www/html

# Copy the current directory contents into the container at /app
COPY . /app

# 安装必要的 PHP 扩展
RUN apt-get update && apt-get install -y \
    curl \
    wget \
    git \
    zip \
    unzip \
    nano \
    vim \
    && apt-get clean && rm -rf /var/lib/apt/lists/*

# 安装composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

# 端口
EXPOSE 9000

# 启动命令
CMD ["php-fpm"]