0Pricing
Django Academy · Lezione

Scrivere un Dockerfile per Django

Creare un'immagine leggera e a livelli per l'app

Scrivere un Dockerfile per Django è una lezione Django Academy gratuita su CoddyKit. Questa è la lezione 1 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.

Why a Dockerfile

A Dockerfile is a recipe that builds your app into a portable image, so it runs the same on your laptop and any server. 🐳

Pick a Base Image

Start every Dockerfile with FROM, choosing a slim Python base so your image stays small and fast to ship.

FROM python:3.12-slim

Set the Work Directory

Use WORKDIR to set the folder where your code lives, so later commands run from a clean, predictable place.

WORKDIR /app

Tame Python Output

Two ENV flags help in containers: unbuffered logs and no .pyc files, keeping output instant and images tidy.

ENV PYTHONUNBUFFERED=1 PYTHONDONTWRITEBYTECODE=1

Copy Requirements First

Copy requirements.txt before your code. Docker caches this layer, so installs are skipped when only your code changes.

COPY requirements.txt .

Install Dependencies

Run pip install with --no-cache-dir so the package cache never bloats your final image.

RUN pip install --no-cache-dir -r requirements.txt

Copy Your Project

Now COPY the rest of your project in. Doing this after the install keeps the cached dependency layer reusable.

COPY . .

Expose the Port

Use EXPOSE to document the port your app listens on. It is a hint to readers and tools, not a firewall.

EXPOSE 8000

Define the Start Command

End with CMD, the default command that launches your app when the container starts.

CMD ["gunicorn", "config.wsgi:application", "--bind", "0.0.0.0:8000"]

Add a .dockerignore

A .dockerignore keeps junk like venvs and .git out of the build context, making builds faster and images leaner.

.git
__pycache__
.venv
*.sqlite3

Build Your Image

Run docker build with a tag to turn your Dockerfile into a reusable, named image you can run anywhere.

docker build -t myapp:latest .

Quick Check

Think about Docker's layer cache and build order.

Recap

You built a Django Dockerfile: pick a slim base, set env, install deps before code for caching, then define CMD. One recipe, runs anywhere. 🎉

Domande Frequenti

La lezione «Scrivere un Dockerfile per Django» è gratuita?

Sì — il testo completo di «Scrivere un Dockerfile per Django» è 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 «Scrivere un Dockerfile per Django»?

Creare un'immagine leggera e a livelli per l'app 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 1 di 4.

Quanto tempo richiede la lezione «Scrivere un Dockerfile per Django»?

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