Automatically translated.View original post

Docker composer...What is it

# Trending # Lemon 8 Howtoo # Drug sign with lemon8 # Web Site Writing Basics

Docker Compose is a tool that allows us to manage Multi-container applications (projects with multiple containers working together) using only one setup file.

If compared, visualize:

Docker Engine is a "crane driver" who lifts one container at a time.

Docker Compose is the "Chief Conductor" who holds the tree and instructs all cabinets (Node.js, Nginx, Database) to be placed in the correct position and connected automatically.

What functions does Docker Compose perform?

Its main function is to turn cumbersome procedures into simple ones. Through the process as follows:

1.Defining specification (Definition)

Instead of typing long run commands one container at a time, you write everything into a single docker-compose.yml file, such as:

What image will be used?

Which port must be opened?

Which Database needs to be connected to?

2. Single Command Orchestration

When the file is prepared, you use one command, docker compose up. Docker Compose will act:

Build: Create Image for every Service as specified.

Create: Create a simulated network to allow containers to talk to each other

Start: Start the operation of every container simultaneously in the correct order.

3. Service Dependencies

It can be defined as "must complete the Database and then run Node.js" through the Depends _ on command to prevent the project from crashing because the app cannot find the database.

Benefits you can get (from the mai-app project)

Save time: No need to type the docker building command and the docker run separately several times.

Infrastructure as Code: The docker-compos.yml file is like a project document. Anyone who gets this file will be able to run a project exactly like you.

Clean Environment: When deprecated, just order the docker compose down, the system immediately sweeps the container and network generated.

Summary of workflow

Write the Dockerfile of each Service (e.g. Node.js).

Write docker-compos.yml to put everything together.

Run the docker compose up to start the entire system.

2/17 Edited to

... Read moreจากประสบการณ์ส่วนตัวในการใช้ Docker Compose พบว่ามันช่วยลดความซับซ้อนในการจัดการแอปพลิเคชันที่มีหลายคอนเทนเนอร์อย่างมาก เนื่องจากเราสามารถกำหนดบริการต่างๆ ทั้ง Node.js, Nginx, และฐานข้อมูลไว้ในไฟล์ docker-compose.yml เดียว ไม่ต้องรันคำสั่งแยกกันหลายครั้ง ซึ่งสะดวกและรวดเร็วมาก สิ่งที่ประทับใจอย่างหนึ่งคือการใช้คำสั่งเดียว "docker compose up" เพื่อจัดการตั้งแต่การ Build Image สร้างเครือข่ายจำลองให้คอนเทนเนอร์สื่อสารกัน และเริ่มทำงานพร้อมกันอย่างมีลำดับขั้นตอนโดยอัตโนมัติ ทำให้ไม่ต้องมาคอยเริ่มหรือหยุดแต่ละส่วนเอง นอกจากนี้ ความสามารถในการจัดการความสัมพันธ์ระหว่างเซอร์วิส เช่น การใช้ depends_on ทำให้มั่นใจได้ว่าแอปฯ จะไม่พังเพราะหาฐานข้อมูลไม่เจอ ซึ่งเคยเจอปัญหานี้มาก่อนเมื่อต้องรันหลายคอนเทนเนอร์ด้วยคำสั่งปกติ อีกอย่างที่ผมชอบคือแนวคิด Infrastructure as Code ที่ไฟล์ docker-compose.yml ทำหน้าที่เหมือนเอกสารประกอบโปรเจกต์คนอื่นๆ ก็สามารถนำไปรันซ้ำได้เลยโดยไม่ต้องตั้งค่าใหม่ ช่วยเพิ่มความโปร่งใสและลดข้อผิดพลาดในการตั้งค่าสภาพแวดล้อม พอเลิกใช้งานก็ทำความสะอาดได้ง่ายด้วยคำสั่ง docker compose down สำหรับผู้ที่สนใจ เริ่มต้นง่ายๆ ด้วยการเขียน Dockerfile ของแต่ละเซอร์วิส แล้วรวมไฟล์ทุกอย่างไว้ใน docker-compose.yml จากนั้นรันด้วย docker compose up เพียงเท่านี้ก็จัดการระบบ Multi-container ได้อย่างมืออาชีพแล้ว