0Pricing
Elixir & Phoenix: Scalable Backend Development · บทเรียน

การทำคอนเทนเนอร์ด้วย Docker

จัดแพ็กเกจแอปพลิเคชัน Elixir/Phoenix ลงในคอนเทนเนอร์ Docker เพื่อการนำไปใช้งานที่สอดคล้องและย้ายแพลตฟอร์มได้

การทำคอนเทนเนอร์ด้วย Docker เป็นบทเรียน Elixir & Phoenix: Scalable Backend Development ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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 ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Elixir & Phoenix: Scalable Backend Development ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Elixir & Phoenix: Scalable Backend Development มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การทำคอนเทนเนอร์ด้วย Docker”

จัดแพ็กเกจแอปพลิเคชัน Elixir/Phoenix ลงในคอนเทนเนอร์ Docker เพื่อการนำไปใช้งานที่สอดคล้องและย้ายแพลตฟอร์มได้ คุณปฏิบัติ Elixir & Phoenix: Scalable Backend Development ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Elixir & Phoenix: Scalable Backend Development หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Elixir & Phoenix: Scalable Backend Development บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 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