การเชื่อมต่อคอนเทนเนอร์ด้วยเครือข่าย Docker Compose
ใช้ Docker Compose กำหนดแอปหลายคอนเทนเนอร์ และให้แอปเหล่านั้นสื่อสารกันผ่านเครือข่ายบริดจ์ที่สร้างขึ้นโดยอัตโนมัติ โดยใช้ชื่อบริการ
การเชื่อมต่อคอนเทนเนอร์ด้วยเครือข่าย Docker Compose เป็นบทเรียน Docker & Kubernetes for Developers ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Docker & Kubernetes for Developers และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Docker & Kubernetes for Developers มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Why Compose for Networking
Running many containers and wiring their networks by hand is tedious. Docker Compose describes your whole stack in one YAML file and creates a dedicated network so services can find each other.
The Default Compose Network
When you run docker compose up, Compose creates a single user-defined bridge network for the project. Every service joins it automatically, so no manual docker network create is needed.
Service Names as Hostnames
On a Compose network, each service is reachable by its service name via built-in DNS. A service named db is reachable at the hostname db from any other service.
A Minimal Compose File
Two services on one network: a web app and a database.
version: "3.9"
services:
web:
image: myapp:latest
ports:
- "8080:80"
db:
image: postgres:16
environment:
POSTGRES_PASSWORD: secretConnecting to db by Name
Inside the web container, the connection string points to the service name, not an IP:
DATABASE_URL=postgres://postgres:secret@db:5432/postgresDefining Custom Networks
You can declare named networks to isolate groups of services.
services:
web:
image: myapp
networks: [frontend]
api:
image: myapi
networks: [frontend, backend]
db:
image: postgres
networks: [backend]
networks:
frontend:
backend:Network Isolation in Practice
In the example above, web can reach api but NOT db, because they share no network. This limits the blast radius if a frontend container is compromised.
Exposing vs Publishing Ports
expose makes a port reachable only to other containers on the network. ports publishes it to the host. Internal services like databases should usually only be exposed, never published.
Aliases for Services
You can give a service extra DNS names with aliases, handy when migrating from an old hostname.
services:
db:
image: postgres
networks:
backend:
aliases:
- database
- legacy-db
networks:
backend:Connecting to External Networks
Set a network as external: true to attach services to a network created outside this Compose file, sharing it across projects.
networks:
shared:
external: trueInspecting the Network
List and inspect the network Compose created to confirm which containers are attached.
docker network ls
docker network inspect myproject_defaultQuick Check
Test what you have learned.
Recap
You learned that Docker Compose creates a network per project, lets services talk by service name, and supports custom networks for isolation, expose vs ports, aliases, and external networks.
คำถามที่พบบ่อย
บทเรียน “การเชื่อมต่อคอนเทนเนอร์ด้วยเครือข่าย Docker Compose” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การเชื่อมต่อคอนเทนเนอร์ด้วยเครือข่าย Docker Compose” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Docker & Kubernetes for Developers ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Docker & Kubernetes for Developers มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การเชื่อมต่อคอนเทนเนอร์ด้วยเครือข่าย Docker Compose”
ใช้ Docker Compose กำหนดแอปหลายคอนเทนเนอร์ และให้แอปเหล่านั้นสื่อสารกันผ่านเครือข่ายบริดจ์ที่สร้างขึ้นโดยอัตโนมัติ โดยใช้ชื่อบริการ คุณปฏิบัติ Docker & Kubernetes for Developers ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Docker & Kubernetes for Developers หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Docker & Kubernetes for Developers บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “การเชื่อมต่อคอนเทนเนอร์ด้วยเครือข่าย Docker Compose” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Docker & Kubernetes for Developers นี้ได้ไหม
ได้ บทเรียน Docker & Kubernetes for Developers ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- ทำความเข้าใจประเภทเครือข่าย Docker
- การกำหนดค่าเครือข่ายคอนเทนเนอร์
- การคงอยู่ของข้อมูลด้วยวอลุ่ม
- การเชื่อมต่อคอนเทนเนอร์ด้วยเครือข่าย Docker Compose