0
0
Djangoframework~10 mins

Docker containerization in Django - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Docker containerization
Write Dockerfile
Build Docker Image
Run Docker Container
Container runs Django app
Access app via browser or API
Stop or remove container
This flow shows how you write a Dockerfile, build an image, run a container, and then access your Django app inside the container.
Execution Sample
Django
FROM python:3.12-slim
WORKDIR /app
COPY requirements.txt ./
RUN pip install -r requirements.txt
COPY . ./
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]
This Dockerfile sets up a Python environment, installs dependencies, copies the Django app, and runs the development server.
Execution Table
StepActionDetailsResult
1Read DockerfileFROM python:3.12-slimBase image set to Python 3.12 slim
2Set working directoryWORKDIR /appWorking directory inside container is /app
3Copy requirementsCOPY requirements.txt ./requirements.txt copied to /app
4Install dependenciesRUN pip install -r requirements.txtPython packages installed
5Copy app filesCOPY . ./All app files copied to /app
6Set commandCMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]Container will run Django server on port 8000
7Build imagedocker build -t django-app .Docker image 'django-app' created
8Run containerdocker run -p 8000:8000 django-appContainer runs, Django app accessible at localhost:8000
9Access appOpen browser at http://localhost:8000Django app homepage loads
10Stop containerCtrl+C or docker stopContainer stops running
💡 Container stops when user stops it or system shuts down
Variable Tracker
VariableStartAfter Step 3After Step 4After Step 5Final
Docker ImageNoneBase python:3.12-slimWith dependencies installedWith app files copiedReady to run Django server
Container StateNot runningNot runningNot runningNot runningRunning Django server on port 8000
Key Moments - 3 Insights
Why do we copy requirements.txt and run pip install separately before copying the whole app?
This allows Docker to cache the installed dependencies layer. If requirements.txt doesn't change, Docker skips reinstalling packages, speeding up builds (see execution_table steps 3 and 4).
Why do we use 0.0.0.0:8000 instead of localhost:8000 in the CMD?
0.0.0.0 makes the Django server listen on all network interfaces inside the container, allowing access from outside the container (see execution_table step 6).
What happens if we don't map port 8000 when running the container?
The Django app runs inside the container but is not accessible from the host machine's browser (see execution_table step 8).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4, what happens during this step?
AThe container starts running the Django server
BThe Django app files are copied into the container
CPython packages are installed inside the container
DThe working directory is set inside the container
💡 Hint
Check the 'Action' and 'Details' columns at step 4 in the execution_table
At which step does the container become accessible from the host machine's browser?
AStep 8: Run container with port mapping
BStep 6: Set command
CStep 7: Build image
DStep 9: Access app
💡 Hint
Look for when the container is run with port mapping in the execution_table
If you change requirements.txt, which step will Docker NOT cache and must rerun?
AStep 3: Copy requirements.txt
BStep 4: Install dependencies
CStep 5: Copy app files
DStep 6: Set command
💡 Hint
Changing requirements.txt affects the pip install step in the execution_table
Concept Snapshot
Docker containerization for Django:
- Write Dockerfile with base image, copy files, install dependencies
- Build image with 'docker build'
- Run container with 'docker run -p hostPort:containerPort'
- Use 0.0.0.0 in runserver to allow external access
- Access app via localhost:port in browser
- Stop container when done
Full Transcript
Docker containerization for Django involves writing a Dockerfile that starts from a Python base image, sets a working directory, copies the requirements.txt file, installs dependencies, copies the Django app files, and sets the command to run the Django development server on all interfaces at port 8000. The Docker image is built using 'docker build', then run with 'docker run' mapping port 8000 to the host. This allows accessing the Django app in a browser at localhost:8000. The container runs until stopped by the user. Key points include caching dependencies by copying requirements.txt first, using 0.0.0.0 to listen on all interfaces, and port mapping to expose the app outside the container.