การสร้างอิมเมจ Docker แบบกำหนดเอง
ฝึกสร้างอิมเมจ Docker แบบกำหนดเองจาก Dockerfile ของคุณ และส่งอิมเมจเหล่านั้นไปยังรีจิสทรี
การสร้างอิมเมจ Docker แบบกำหนดเอง เป็นบทเรียน Docker & Kubernetes for Developers ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Docker & Kubernetes for Developers และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Docker & Kubernetes for Developers มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Build & Push Docker Images
Welcome back! In the previous lesson, you learned to write Dockerfiles. Now, let's turn those Dockerfiles into real Docker images and share them!
We'll cover how to:
- Use the
docker buildcommand - Tag your images for versioning
- Log in to a Docker registry
- Push your custom images for others to use
The `docker build` Command
The docker build command is your tool for creating Docker images from a Dockerfile and a "build context."
- It reads the instructions in your Dockerfile.
- It executes each instruction, creating a new layer in the image.
- Finally, it produces a new Docker image.
The basic syntax is docker build [OPTIONS] PATH | URL.
Understanding the Build Context
When you run docker build ., the . (dot) specifies the build context. This is the set of files at the specified PATH or URL that the Docker daemon can access during the build process.
- All files and folders in the build context are sent to the Docker daemon.
- This allows instructions like
COPY . /appto work. - Keep your build context clean to avoid sending unnecessary files!
Your First Image Build
Let's build a very simple image. Create a file named Dockerfile in an empty directory with the content below.
Then, open your terminal in that directory and try building the image:
FROM alpine:latest
CMD ["echo", "Hello from CoddyKit!"]Running the Build Command
To build the image from the Dockerfile you just created, run this command in your terminal:
docker build .
The . tells Docker to look for the Dockerfile in the current directory and use that directory as the build context.
After it completes, you'll see a new image ID!
Tagging Your Docker Images
Image tags are like labels for your images. They help you identify different versions or variants of an image.
- Tags are usually in the format
name:tag(e.g.,my-app:v1.0). - The
latesttag is often used for the most recent stable version. - You can add a tag using the
-tflag during the build process.
Build with a Custom Tag
Let's rebuild our image, but this time, we'll give it a meaningful tag. Replace yourusername with your Docker Hub username.
After building, use docker images to see your newly tagged image!
FROM alpine:latest
CMD ["echo", "Hello from CoddyKit!"]Pushing Images to a Registry
A Docker Registry is a storage and distribution system for Docker images. Docker Hub is the most popular public registry.
To share your images or store them centrally, you'll push them to a registry:
- First, log in to the registry (e.g., Docker Hub).
- Then, use the
docker pushcommand.
Log In to Docker Hub
Before pushing, you need to authenticate with Docker Hub (or another registry). This links your local Docker client to your account.
Run the following command in your terminal. You'll be prompted for your Docker Hub username and password:
docker loginPush Your Custom Image
Once logged in, you can push your image. Remember, the image tag must include your Docker Hub username (e.g., yourusername/my-app:v1.0).
Replace yourusername with your actual Docker Hub username:
docker push yourusername/coddykit-app:v1.0Check Your Knowledge
You've learned to build and push images. Which command is used to assign a name and tag to an image during the build process?
Recap: Build, Tag, Push!
Great job! You've mastered the core steps of turning a Dockerfile into a usable, shareable image:
docker build .: Creates an image from your Dockerfile.docker build -t myuser/myimage:tag .: Builds and tags your image, preparing it for a registry.docker login: Authenticates you with a Docker registry like Docker Hub.docker push myuser/myimage:tag: Uploads your tagged image to the registry.
Now your custom applications can be packaged and distributed efficiently!
คำถามที่พบบ่อย
บทเรียน “การสร้างอิมเมจ Docker แบบกำหนดเอง” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การสร้างอิมเมจ Docker แบบกำหนดเอง” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Docker & Kubernetes for Developers ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Docker & Kubernetes for Developers มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การสร้างอิมเมจ Docker แบบกำหนดเอง”
ฝึกสร้างอิมเมจ Docker แบบกำหนดเองจาก Dockerfile ของคุณ และส่งอิมเมจเหล่านั้นไปยังรีจิสทรี คุณปฏิบัติ Docker & Kubernetes for Developers ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Docker & Kubernetes for Developers หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Docker & Kubernetes for Developers บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน
บทเรียน “การสร้างอิมเมจ Docker แบบกำหนดเอง” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Docker & Kubernetes for Developers นี้ได้ไหม
ได้ บทเรียน Docker & Kubernetes for Developers ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การเขียน Dockerfile สำหรับอิมเมจ
- การสร้างอิมเมจ Docker แบบกำหนดเอง
- แอปพลิเคชันหลายคอนเทนเนอร์ด้วย Compose
- การคงข้อมูลด้วยวอลุ่ม Docker