0Pricing
Django Academy · Lezione

Entry point, migrazioni e collectstatic

Eseguire i passaggi di avvio all'interno del container

Entry point, migrazioni e collectstatic è una lezione Django Academy gratuita su CoddyKit. Questa è la lezione 3 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Django Academy, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Django Academy include 4 lezioni in totale.

Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.

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 -e

Apply Migrations on Boot

Run migrate inside the entrypoint, so your database schema is always current when the app starts.

python manage.py migrate --noinput

Gather Static Files

Run collectstatic to copy CSS and JS into one folder that your server can serve in production.

python manage.py collectstatic --noinput

Hand 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.sh

Wire 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; done

Watch the Logs

Use docker compose logs to confirm migrations ran and static files were collected on startup.

docker compose logs -f web

Quick 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. ✅

Domande Frequenti

La lezione «Entry point, migrazioni e collectstatic» è gratuita?

Sì — il testo completo di «Entry point, migrazioni e collectstatic» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Django Academy, passa a CoddyKit PRO. Il corso Django Academy include 4 lezioni in totale.

Cosa imparerò in «Entry point, migrazioni e collectstatic»?

Eseguire i passaggi di avvio all'interno del container Eserciti Django Academy con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.

Ho bisogno di esperienza per iniziare Django Academy?

Non è richiesta alcuna esperienza precedente. Django Academy su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 3 di 4.

Quanto tempo richiede la lezione «Entry point, migrazioni e collectstatic»?

La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.

Posso scrivere ed eseguire codice in questa lezione Django Academy?

Sì. Ogni lezione Django Academy include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.

Tutte le lezioni di questo corso

  1. Scrivere un Dockerfile per Django
  2. docker-compose con Postgres e Redis
  3. Entry point, migrazioni e collectstatic
  4. Buone pratiche per le immagini di produzione
← Torna a Django Academy