📖 Scenario: You are working on a software project and want to create a development environment that is consistent for everyone on your team. Using a development container helps by packaging all the tools and dependencies inside a container.This project will guide you through creating a simple Dockerfile for a development container, adding configuration, running a command inside the container, and finally displaying the result.
🎯 Goal: Build a Docker development container that installs Python 3.12, sets a working directory, and runs a simple Python script inside the container.
📋 What You'll Learn
Create a Dockerfile with a base image of Python 3.12
Set the working directory inside the container to /app
Add a Python script named hello.py that prints 'Hello from container!'
Run the Python script inside the container and display the output
💡 Why This Matters
🌍 Real World
Development containers help teams work with the same tools and dependencies, avoiding the 'it works on my machine' problem.
💼 Career
Knowing how to create and use development containers is a key skill for DevOps engineers and developers to ensure consistent environments and smooth collaboration.
Progress0 / 4 steps
1
Create the Dockerfile with base image
Create a file named Dockerfile and write a line to use the base image python:3.12-slim.
Docker
Hint
The first line in a Dockerfile usually starts with FROM followed by the base image name.
2
Set working directory and add Python script
In the Dockerfile, add a line to set the working directory to /app. Then create a file named hello.py with the exact content print('Hello from container!').
Docker
Hint
Use WORKDIR /app to set the directory inside the container. The Python script should have exactly one line printing the message.
3
Add command to run Python script
In the Dockerfile, add a line to run the Python script hello.py using the command python hello.py when the container starts.
Docker
Hint
Use CMD ["python", "hello.py"] to run the script when the container starts.
4
Build and run the container to see output
Build the Docker image with the tag dev-container and run it. Write the command to build the image and then the command to run the container so it prints Hello from container!.
Docker
Hint
Use docker build -t dev-container . to build and docker run --rm dev-container to run the container and see the output.
Practice
(1/5)
1. What is the main purpose of using a development container in Docker?
easy
A. To create a consistent coding environment isolated from your main computer
B. To speed up your internet connection
C. To permanently delete files from your computer
D. To run games inside Docker
Solution
Step 1: Understand what a development container does
Development containers provide a consistent environment for coding by isolating tools and dependencies.
Step 2: Match the purpose to the options
Only To create a consistent coding environment isolated from your main computer describes creating a consistent, isolated coding environment.
Final Answer:
To create a consistent coding environment isolated from your main computer -> Option A
Quick Check:
Development container purpose = consistent isolated environment [OK]
Hint: Think: 'Is this about coding environment isolation?' [OK]
Common Mistakes:
Confusing containers with internet speed tools
Thinking containers delete files permanently
Assuming containers run unrelated apps like games
2. Which Dockerfile instruction is used to specify the base image for a development container?
easy
A. RUN
B. CMD
C. FROM
D. COPY
Solution
Step 1: Identify Dockerfile instructions
Dockerfile uses specific instructions like FROM, RUN, COPY, CMD for different purposes.
Step 2: Match instruction to base image
FROM sets the base image; RUN executes commands; COPY copies files; CMD sets default command.
Final Answer:
FROM -> Option C
Quick Check:
Base image instruction = FROM [OK]
Hint: Base image always starts with FROM in Dockerfile [OK]
Common Mistakes:
Using RUN to set base image
Confusing COPY with base image
Thinking CMD sets base image
3. Given this Dockerfile snippet for a development container:
FROM python:3.12-slim
WORKDIR /app
COPY . .
RUN pip install -r requirements.txt
CMD ["python", "app.py"]
What happens when you run this container?
medium
A. The container fails because WORKDIR is missing
B. The container runs but does not install dependencies
C. The container runs but does not start app.py
D. The container runs Python 3.12, installs dependencies, and starts app.py
Solution
Step 1: Analyze Dockerfile commands
FROM sets Python 3.12 base, WORKDIR sets /app, COPY copies files, RUN installs dependencies, CMD runs app.py.
Step 2: Predict container behavior
All steps are correct and complete, so container installs dependencies and runs app.py.
Final Answer:
The container runs Python 3.12, installs dependencies, and starts app.py -> Option D
Quick Check:
Dockerfile commands = install and run app [OK]
Hint: Check each Dockerfile line for its role [OK]
Common Mistakes:
Ignoring RUN command effect
Assuming WORKDIR is missing
Thinking CMD does not start app
4. You wrote this Dockerfile for a development container:
FROM node:18
WORKDIR /app
COPY package.json .
RUN npm install
COPY . .
CMD ["node", "server.js"]
But when you build and run, the app crashes with missing modules error. What is the likely problem?
medium
A. You did not copy package-lock.json before npm install
B. The COPY . . after RUN npm install overwrites node_modules with host files if node_modules not in .dockerignore
C. You copied package.json before npm install but did not copy other files before npm install
D. You copied package.json after npm install
Solution
Step 1: Review Dockerfile copy and install order
package.json copied first for npm install (good for caching), then full COPY . . after.
Step 2: Identify the overwrite issue
RUN npm install builds linux-compatible node_modules, but COPY . . copies host files including node_modules/ (wrong platform), overwriting it and causing missing modules or binary errors. Fix: node_modules in .dockerignore.
Final Answer:
The COPY . . after RUN npm install overwrites node_modules with host files if node_modules not in .dockerignore -> Option B
Assuming source files must be copied before npm install
5. You want to create a development container that automatically reloads your Node.js app when you change source files on your host machine. Which pattern should you use?
hard
A. Use a volume mount to sync source files and run nodemon inside the container
B. Copy source files once during build and restart container manually on changes
C. Build a new image every time source files change
D. Run the app outside Docker and only use Docker for dependencies
Solution
Step 1: Understand live reload needs
Live reload requires syncing source files between host and container and a watcher like nodemon inside container.
Step 2: Match pattern to options
Volume mount syncs files live; nodemon watches and reloads app automatically.
Final Answer:
Use a volume mount to sync source files and run nodemon inside the container -> Option A
Quick Check:
Live reload = volume mount + nodemon [OK]
Hint: Volume mount + nodemon = auto reload dev container [OK]