编写用于构建镜像的 Dockerfile
学习编写 Dockerfile,定义如何将应用环境和依赖项打包到镜像中。
编写用于构建镜像的 Dockerfile 是 CoddyKit 上的免费 Docker & Kubernetes for Developers 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Docker & Kubernetes for Developers 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Docker & Kubernetes for Developers 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
What are Dockerfiles?
Welcome to creating your own Docker images! The heart of this process is the Dockerfile.
A Dockerfile is a simple text file that contains a series of instructions. These instructions tell Docker exactly how to build a Docker image, step by step.
Why Use a Dockerfile?
Dockerfiles automate the image creation process. Instead of manually setting up an environment, you define it once in a Dockerfile.
- Reproducibility: Ensures everyone builds the exact same image.
- Version Control: Dockerfiles can be stored in Git, just like your code.
- Efficiency: Speeds up development and deployment workflows.
Starting Point: The FROM Instruction
Every Dockerfile must start with the FROM instruction. It specifies the base image your image will be built upon.
Think of it as choosing the operating system and initial software for your container. For example, ubuntu:22.04 or node:18-alpine.
FROM node:18-alpineRunning Commands: The RUN Instruction
The RUN instruction executes commands during the image build process. This is where you install software, update packages, or create directories.
Each RUN instruction creates a new layer in your image.
FROM node:18-alpine
RUN apk add --no-cache gitSetting the Workspace: WORKDIR
The WORKDIR instruction sets the working directory for any subsequent RUN, CMD, ENTRYPOINT, COPY, or ADD instructions.
It's good practice to set a specific directory for your application files.
FROM node:18-alpine
WORKDIR /appAdding Files: The COPY Instruction
The COPY instruction copies files or directories from your local machine (the build context) into the Docker image filesystem.
The first argument is the source path on your machine, the second is the destination path inside the image.
FROM node:18-alpine
WORKDIR /app
COPY package*.json ./Installing Dependencies
After copying your project's dependency files (like package.json for Node.js), you'll typically use RUN again to install them.
This step ensures your application has all it needs to run.
FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .Exposing Ports: The EXPOSE Instruction
The EXPOSE instruction informs Docker that the container listens on the specified network ports at runtime.
It's documentation for users, not an actual publishing of the port. You'll publish ports when you run the container.
FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000Default Command: The CMD Instruction
The CMD instruction provides a default command to be executed when a container starts from the image.
Unlike RUN, CMD runs when the container is launched, not during the build. You can override it when running the container.
FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ["npm", "start"]Complete Dockerfile Example
Here's a full example of a simple Dockerfile for a Node.js web application. Each line contributes to building the final image.
# Use an official Node.js runtime as a parent image
FROM node:18-alpine
# Set the working directory in the container
WORKDIR /app
# Copy package.json and package-lock.json to install dependencies
COPY package*.json ./
# Install application dependencies
RUN npm install
# Copy the rest of the application code
COPY . .
# Make port 3000 available to the world outside this container
EXPOSE 3000
# Define the command to run your app
CMD ["npm", "start"]Dockerfile Instruction Check
Which Dockerfile instruction is used to execute commands during the image build process, such as installing packages or creating directories?
Recap: Crafting Dockerfiles
You've learned the essential instructions for crafting Dockerfiles:
FROM: Sets the base image.RUN: Executes commands during build.WORKDIR: Sets the working directory.COPY: Copies files into the image.EXPOSE: Declares listening ports.CMD: Sets the default command to run on container start.
Mastering these allows you to define your application's environment precisely!
常见问题解答
「编写用于构建镜像的 Dockerfile」课时是免费的吗?
是的 — 「编写用于构建镜像的 Dockerfile」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Docker & Kubernetes for Developers 课程的其余内容,请升级到 CoddyKit PRO。 Docker & Kubernetes for Developers 课程共包含 4 节课。
「编写用于构建镜像的 Dockerfile」这节课中我会学到什么?
学习编写 Dockerfile,定义如何将应用环境和依赖项打包到镜像中。 你通过在浏览器中直接运行的动手代码来练习 Docker & Kubernetes for Developers,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Docker & Kubernetes for Developers 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Docker & Kubernetes for Developers 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 4 节。
「编写用于构建镜像的 Dockerfile」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Docker & Kubernetes for Developers 课中编写并运行代码吗?
能。每节 Docker & Kubernetes for Developers 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 编写用于构建镜像的 Dockerfile
- 构建自定义 Docker 镜像
- 使用 Compose 构建多容器应用
- 使用 Docker 卷持久化数据