0Pricing
Elixir & Phoenix: Scalable Backend Development · 课时

使用 Docker 容器化

将 Elixir/Phoenix 应用打包到 Docker 容器中,实现一致且可移植的部署。

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

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

Welcome to Docker for Elixir!

In this lesson, we'll learn how to package your Elixir and Phoenix applications using Docker containers. This makes your deployments consistent and portable across different environments.

Think of Docker as a way to bundle your application and all its dependencies into a single, isolated package.

Images, Containers, & Dockerfiles

Let's define some core Docker terms:

  • Image: A lightweight, standalone, executable package that includes everything needed to run a piece of software, including the code, a runtime, system tools, libraries, and settings.
  • Container: A running instance of an image. It's an isolated process that shares the host OS kernel but runs in its own environment.
  • Dockerfile: A text file that contains all the commands a user could call on the command line to assemble an image. It's your recipe for building an image.

Why Docker for Elixir/Phoenix?

Elixir and Phoenix applications benefit greatly from containerization:

  • Consistent Environments: Ensure your app runs the same way from development to production, avoiding "it works on my machine" issues.
  • Dependency Management: Easily manage Erlang/OTP, Elixir versions, and system-level dependencies.
  • Portability: Deploy your containerized app on any system that runs Docker, from local machines to cloud servers.
  • Isolation: Containers isolate your application, preventing conflicts with other software on the host system.

A Simple Elixir Script

Before Dockerizing, let's have a simple Elixir script. We'll put this into a file named hello.exs. This script simply prints a greeting.

You can run this locally with elixir hello.exs.

IO.puts "Hello from Elixir in a container!"

Crafting Your Dockerfile

A Dockerfile is like a set of instructions for building your Docker image. Here's a basic one for our simple Elixir script:

  • FROM: Specifies the base image (e.g., an official Elixir image).
  • WORKDIR: Sets the working directory inside the container.
  • COPY: Copies files from your host machine into the container.
  • CMD: Defines the default command to execute when a container starts.

Dockerfile Example: Simple Elixir

Let's create a Dockerfile in the same directory as hello.exs:

FROM elixir:1.15.7-slim-bullseye selects a lean base image with Elixir 1.15.7. COPY . . copies everything from the current directory into the container's working directory. CMD ["elixir", "hello.exs"] runs our script.

FROM elixir:1.15.7-slim-bullseye

WORKDIR /app

COPY hello.exs .

CMD ["elixir", "hello.exs"]

Building the Docker Image

With your Dockerfile and hello.exs ready, you can build your Docker image. Open your terminal in the same directory and run:

The -t my-elixir-app tags your image with a name for easy reference. The . tells Docker to look for the Dockerfile in the current directory.

docker build -t my-elixir-app .

Running Your Elixir Container

Once the image is built, you can run a container from it:

This command starts a new container based on the my-elixir-app image. You should see the output "Hello from Elixir in a container!" printed to your terminal. Your Elixir application is now running in an isolated Docker container!

docker run my-elixir-app

Multi-Stage Builds for Production

For real Phoenix applications, image size matters. Multi-stage builds allow you to use multiple FROM statements in your Dockerfile to create smaller, more secure images.

Typically, you'll have a "build" stage (where dependencies are fetched and compiled) and a "release" stage (which only copies the final compiled application and a minimal runtime). This significantly reduces the final image size.

Quick Check: Dockerfile Basics

Which Dockerfile instruction specifies the base image for building your container?

Recap: Docker for Elixir

We've explored how Docker containerizes Elixir/Phoenix applications, providing consistency and portability. You learned about Docker images, containers, and how to define them using a Dockerfile. We covered building and running a basic Elixir app in a container and introduced multi-stage builds for optimized production images.

Next, you might explore Docker Compose for multi-service applications or deploying these containers to the cloud!

常见问题解答

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

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

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

将 Elixir/Phoenix 应用打包到 Docker 容器中,实现一致且可移植的部署。 你通过在浏览器中直接运行的动手代码来练习 Elixir & Phoenix: Scalable Backend Development,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Elixir & Phoenix: Scalable Backend Development 需要有经验吗?

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

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

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

我能在这节 Elixir & Phoenix: Scalable Backend Development 课中编写并运行代码吗?

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

此课程中的所有课时

  1. 使用 Mix Release 构建发布版本
  2. 使用 Docker 容器化
  3. 云部署策略
  4. 零停机部署与热升级
← 返回 Elixir & Phoenix: Scalable Backend Development