จุดเริ่มต้น การย้ายข้อมูล และ collectstatic
เรียกใช้ขั้นตอนเริ่มต้นภายในคอนเทนเนอร์
จุดเริ่มต้น การย้ายข้อมูล และ collectstatic เป็นบทเรียน Django Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Django Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Django Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Startup Steps Matter
Before serving traffic, a container often needs to run migrations and gather static files. Let's automate those startup steps. ⚙️
What an Entrypoint Is
An entrypoint is a script that runs every time the container starts, right before your main command takes over.
Write the Script
Create entrypoint.sh with a shebang line, so the container knows to run it with the shell.
#!/bin/sh
set -eApply Migrations on Boot
Run migrate inside the entrypoint, so your database schema is always current when the app starts.
python manage.py migrate --noinputGather Static Files
Run collectstatic to copy CSS and JS into one folder that your server can serve in production.
python manage.py collectstatic --noinputHand Off with exec
End the script with exec so your app becomes process 1 and receives stop signals cleanly.
exec "$@"Make It Executable
Set the script as executable in your Dockerfile so the container can actually run it on startup.
RUN chmod +x entrypoint.shWire It in the Dockerfile
Point ENTRYPOINT at your script, then keep CMD as the command it should run last.
ENTRYPOINT ["./entrypoint.sh"]
CMD ["gunicorn", "config.wsgi"]Why --noinput
The --noinput flag stops Django from pausing for prompts, which is essential for unattended container startups.
Wait for the Database
Postgres may boot slowly, so it helps to wait for the db port before migrating to avoid connection errors.
until nc -z db 5432; do sleep 1; doneWatch the Logs
Use docker compose logs to confirm migrations ran and static files were collected on startup.
docker compose logs -f webQuick Check
Why end an entrypoint script with exec on the main command?
Recap
Your entrypoint now runs migrate and collectstatic with --noinput, waits for the db, then execs the app as PID 1. Boot-ready every time. ✅
คำถามที่พบบ่อย
บทเรียน “จุดเริ่มต้น การย้ายข้อมูล และ collectstatic” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “จุดเริ่มต้น การย้ายข้อมูล และ collectstatic” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Django Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Django Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “จุดเริ่มต้น การย้ายข้อมูล และ collectstatic”
เรียกใช้ขั้นตอนเริ่มต้นภายในคอนเทนเนอร์ คุณปฏิบัติ Django Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Django Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Django Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน
บทเรียน “จุดเริ่มต้น การย้ายข้อมูล และ collectstatic” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Django Academy นี้ได้ไหม
ได้ บทเรียน Django Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การเขียน Dockerfile สำหรับ Django
- docker-compose พร้อม Postgres และ Redis
- จุดเริ่มต้น การย้ายข้อมูล และ collectstatic
- แนวทางปฏิบัติที่ดีสำหรับอิมเมจใช้งานจริง