0Pricing
Node.js Backend Development Bootcamp · 课时

使用 Docker 容器化

创建 Dockerfile、构建镜像,并在容器中运行 Node.js 应用程序,以获得一致的环境。

使用 Docker 容器化 是 CoddyKit 上的免费 Node.js Backend Development Bootcamp 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Node.js Backend Development Bootcamp 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Node.js Backend Development Bootcamp 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Docker: Consistent Environments

Welcome to containerization with Docker! Docker helps package your application and its dependencies into a single unit called a container.

This ensures your app runs consistently across different environments, from your laptop to production servers.

Images vs. Containers

Think of a Docker Image as a blueprint or a template. It's a lightweight, standalone, executable package of software that includes everything needed to run an application: code, runtime, system tools, system libraries, and settings.

A Docker Container is a running instance of an image. You can have multiple containers running from the same image, each isolated from the others.

Why Docker for Node.js?

Docker offers significant advantages for Node.js applications:

  • Consistency: No more 'it works on my machine' issues.
  • Isolation: Your Node.js app runs in its own environment, preventing conflicts.
  • Dependencies: Easily manage and package all your npm dependencies.
  • Scalability: Quickly deploy and scale identical instances of your app.

The Dockerfile Explained

A Dockerfile is a simple text file that contains a set of instructions. These instructions tell Docker how to build your application's image.

It defines everything from the base operating system to copying files, installing dependencies, and specifying how your application should start.

Simple Node.js App

Let's create a minimal Node.js application that we'll containerize. This simple server will respond with 'Hello from Node.js in Docker!' on port 3000.

You can run this code to see it working locally before Dockerizing.

const http = require('http');

const hostname = '0.0.0.0';
const port = 3000;

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello from Node.js in Docker!\n');
});

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});

Dockerfile: Base Image & Workdir

Every Dockerfile starts with a FROM instruction, specifying the base image. For Node.js, we often use official Node.js images. WORKDIR sets the working directory inside the container.

FROM node:18-alpine
WORKDIR /app

Dockerfile: Dependencies & Files

Next, we copy package.json and package-lock.json (if present) to install dependencies. Then, we copy the rest of our application files. This order optimizes Docker's caching.

  • COPY package*.json ./: Copies package files.
  • RUN npm install: Installs dependencies.
  • COPY . .: Copies all other app files.
COPY package*.json ./
RUN npm install
COPY . .

Dockerfile: Port & Start Command

Finally, we tell Docker which port our application listens on using EXPOSE. This is documentation, not a firewall rule. CMD defines the command to run when the container starts.

EXPOSE 3000
CMD ["npm", "start"]

Building Your Docker Image

With your Dockerfile ready (and your app.js and package.json in the same directory), you can build the image. The -t flag tags your image with a name (e.g., my-node-app).

Open your terminal and run:

docker build -t my-node-app .

Running Your Docker Container

After building, you can run your application in a Docker container. The -p 3000:3000 flag maps port 3000 on your host machine to port 3000 inside the container.

Visit http://localhost:3000 in your browser after running:

docker run -p 3000:3000 my-node-app

Dockerfile Instruction Check

Which of the following Dockerfile instructions are primarily used to manage dependencies and application files within the image?

Recap: Docker for Node.js

You've learned the basics of Docker containerization! We covered:

  • What Docker Images and Containers are.
  • The benefits of Docker for Node.js apps.
  • How to write a Dockerfile for a Node.js application.
  • Commands to build an image and run a container.

Docker provides a powerful way to ensure your applications are consistent and easily deployable.

常见问题解答

「使用 Docker 容器化」课时是免费的吗?

是的 — 「使用 Docker 容器化」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Node.js Backend Development Bootcamp 课程的其余内容,请升级到 CoddyKit PRO。 Node.js Backend Development Bootcamp 课程共包含 4 节课。

「使用 Docker 容器化」这节课中我会学到什么?

创建 Dockerfile、构建镜像,并在容器中运行 Node.js 应用程序,以获得一致的环境。 你通过在浏览器中直接运行的动手代码来练习 Node.js Backend Development Bootcamp,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Node.js Backend Development Bootcamp 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Node.js Backend Development Bootcamp 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 4 节。

「使用 Docker 容器化」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Node.js Backend Development Bootcamp 课中编写并运行代码吗?

能。每节 Node.js Backend Development Bootcamp 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 使用 Docker 容器化
  2. 云部署(AWS/Heroku)
  3. 使用 PM2 管理进程
  4. 使用 GitHub Actions 构建 CI/CD 流水线
← 返回 Node.js Backend Development Bootcamp