0
0
Dockerdevops~10 mins

Development container patterns in Docker - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Development container patterns
Start: Define base image
Add dependencies & tools
Set working directory
Copy source code
Expose ports & volumes
Define entrypoint or command
Build container image
Run container for development
Code changes reflected via mounted volume
Debug & test inside container
Stop container when done
This flow shows how a development container is built step-by-step, from base image to running with live code changes.
Execution Sample
Docker
FROM python:3.12-slim
WORKDIR /app
COPY requirements.txt ./
RUN pip install -r requirements.txt
COPY . ./
CMD ["python", "app.py"]
This Dockerfile sets up a Python development container with dependencies installed and runs the app.
Process Table
StepDockerfile InstructionActionResulting Image StateNotes
1FROM python:3.12-slimSet base imageImage starts from python 3.12 slimBase OS and Python ready
2WORKDIR /appSet working directoryWorking directory set to /appCommands run inside /app
3COPY requirements.txt ./Copy requirements filerequirements.txt available in /appNeeded for dependency install
4RUN pip install -r requirements.txtInstall dependenciesPython packages installedApp dependencies ready
5COPY . ./Copy source codeAll app files copied to /appApp code ready to run
6CMD ["python", "app.py"]Set default commandContainer runs python app.py on startDefines container behavior
7docker build -t dev-container .Build imageImage named dev-container createdReady to run container
8docker run -v $(pwd):/app -p 5000:5000 dev-containerRun container with volume and portContainer runs with live code syncCode changes reflect immediately
9Modify code on hostEdit source filesChanges appear inside containerLive development enabled
10Stop containerEnd sessionContainer stops runningDevelopment session ends
💡 Container stops when developer finishes coding or testing
Status Tracker
VariableStartAfter Step 3After Step 5After Step 8Final
Image LayersEmptyBase python:3.12-slimAdded requirements.txt and source codeBuilt image with all files and dependenciesImage ready for development
Container StateNot runningNot runningNot runningRunning with volume mountedStopped after development
Key Moments - 3 Insights
Why do we mount the source code as a volume instead of copying it only once?
Mounting the source code as a volume (see step 8) allows live code changes on the host to immediately appear inside the container without rebuilding the image (step 5 only copies once). This enables fast development feedback.
What happens if we don't set the working directory with WORKDIR?
Without WORKDIR (step 2), commands like COPY and RUN might place files or run in unexpected locations, making the container harder to manage. Setting WORKDIR ensures all actions happen inside /app.
Why do we separate installing dependencies from copying source code?
Separating RUN pip install (step 4) from copying source code (step 5) helps Docker cache dependencies. If source code changes but requirements.txt doesn't, Docker reuses the cached layer, speeding up builds.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table at step 8. What is the main benefit of running the container with the volume option?
AThe container image size is reduced
BCode changes on the host immediately reflect inside the container
CThe container runs faster without dependencies
DThe container automatically restarts on failure
💡 Hint
Check the 'Resulting Image State' and 'Notes' columns at step 8 in the execution table
At which step does the container image get created and ready to run?
AStep 7
BStep 5
CStep 3
DStep 9
💡 Hint
Look for the 'docker build' command in the execution table
If we remove the WORKDIR instruction, what would likely happen?
AThe container would run faster
BDependencies would not install
CFiles might be copied to unexpected locations
DThe container would not start
💡 Hint
Refer to the key moment about WORKDIR and step 2 in the execution table
Concept Snapshot
Development container patterns:
- Use a base image (e.g., python:3.12-slim)
- Set WORKDIR for consistent paths
- Copy dependencies file and install them
- Copy source code separately
- Use volumes to sync live code changes
- Define CMD or ENTRYPOINT to run app
- Build and run container with port and volume mappings
- Enables fast, isolated development environment
Full Transcript
Development container patterns involve creating a container image starting from a base image like python:3.12-slim. We set a working directory to keep commands organized. We copy the dependencies file and install packages before copying the full source code. This separation helps caching and speeds up rebuilds. When running the container, we mount the source code as a volume so that changes on the host immediately reflect inside the container, enabling live development. The container runs the app using a defined command. This pattern creates an isolated, reproducible environment for coding, testing, and debugging without affecting the host system.