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

使用 Mix Release 构建发布版本

使用 `mix release` 创建自包含且可直接部署的 Elixir 应用发布版本。

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

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

Welcome to Elixir Releases

When you develop an Elixir application, you often run it with mix run --no-halt. This is great for development, but not ideal for production.

For deployment, we need a self-contained package that can run independently, without needing the Mix build tool or even Elixir's source code on the target machine. This is where releases come in!

Why `mix run` Isn't Enough

Running an application with mix run has a few drawbacks for production:

  • It requires the full Mix build tool and your project's source code.
  • It uses the development environment configuration by default.
  • Startup can be slower as it needs to compile and load dependencies.
  • It lacks proper daemonization features for background processes.

Introducing `mix release`

mix release creates an OTP release. Think of it as a complete, self-contained package of your Elixir application and its dependencies.

It includes the Erlang VM, your compiled code, and all necessary scripts to start, stop, and manage your application as a standalone service.

Benefits of Using Releases

Using releases offers several key advantages:

  • Self-contained: No need for Mix or Elixir installation on the server.
  • Faster Startup: Pre-compiled and optimized for production.
  • Production-ready: Designed for daemonization and supervision.
  • Hot Code Swapping: Supports upgrading running systems without downtime (advanced).

Generating Your First Release

To create a release, first ensure your project has a supervisor. Most Phoenix projects and projects created with mix new my_app --sup already have one.

Then, simply run the mix release command. This will compile your project and its dependencies for the production environment.

mix new my_release_app --sup
cd my_release_app
mix deps.get
mix release

Exploring the Release Structure

After running mix release, you'll find your release in _build/prod/rel/your_app_name/.

Inside, you'll see a bin directory containing a script to manage your app, lib with all compiled dependencies, and releases with version-specific files.

my_release_app/
├── _build/
│   └── prod/
│       └── rel/
│           └── my_release_app/
│               ├── bin/
│               │   └── my_release_app (start/stop script)
│               ├── lib/
│               ├── releases/
│               └── erts-...
├── config/
├── lib/
└── mix.exs

Running Your Released Application

To start your application from a release, use the generated script in the bin directory. You can start it in the foreground or as a daemon.

For a production environment, you'd typically start it in the background.

# Start in foreground (for testing)
_build/prod/rel/my_release_app/bin/my_release_app start_iex

# Start as daemon (production)
_build/prod/rel/my_release_app/bin/my_release_app start

# Stop a running daemon
_build/prod/rel/my_release_app/bin/my_release_app stop

Runtime Configuration with `runtime.exs`

For production deployments, you often need to configure your application based on its environment (e.g., database credentials, API keys).

mix release uses config/runtime.exs, which is executed after the release is built and before the application starts. This is perfect for reading environment variables.

# config/runtime.exs
import Config

config :my_release_app, 
  database_url: System.get_env("DATABASE_URL"),
  api_key: System.get_env("API_KEY")

Upgrades and Downgrades (Advanced)

One of the most powerful features of OTP releases is the ability to perform hot code upgrades. This means you can update your running application with new code without stopping it, leading to zero downtime deployments.

While this is an advanced topic, `mix release` lays the groundwork for it by packaging your application in a way that allows for version management and patching.

Release Benefits Check

Which of the following are key benefits of using mix release for deploying an Elixir application?

Recap: Ready for Deployment!

You've learned that mix release is the standard way to package Elixir applications for production. It creates a self-contained, optimized bundle, independent of the Mix toolchain.

Releases offer faster startup, robust daemonization, and support for advanced features like hot code upgrades. Coupled with config/runtime.exs, they provide a powerful and flexible deployment solution.

常见问题解答

「使用 Mix Release 构建发布版本」课时是免费的吗?

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

「使用 Mix Release 构建发布版本」这节课中我会学到什么?

使用 `mix release` 创建自包含且可直接部署的 Elixir 应用发布版本。 你通过在浏览器中直接运行的动手代码来练习 Elixir & Phoenix: Scalable Backend Development,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

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

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

「使用 Mix Release 构建发布版本」课时需要多长时间?

大多数 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