📖 Scenario: Your team needs to ensure everyone uses the same software environment to avoid "it works on my machine" problems. Docker helps by packaging the app and its environment together.In this project, you will create a simple Docker setup for a Python app that prints a greeting. This will help your team run the app consistently on any computer.
🎯 Goal: Build a Dockerfile that sets up a Python environment, copies a Python script, and runs it inside a container. This ensures all team members run the same code with the same Python version and dependencies.
📋 What You'll Learn
Create a Python script named app.py that prints 'Hello from Docker!'
Write a Dockerfile that uses the official Python 3.12 image
Copy app.py into the Docker image
Set the container to run app.py when started
Build and run the Docker container to see the output
💡 Why This Matters
🌍 Real World
Teams use Docker to package apps with their environment so everyone runs the same setup, avoiding bugs caused by different software versions.
💼 Career
Knowing how to create Dockerfiles and run containers is a key skill for DevOps engineers and developers working in teams.
Progress0 / 4 steps
1
Create the Python script
Create a file named app.py with one line of code that prints exactly 'Hello from Docker!'.
Docker
Hint
Use the print() function with the exact text inside single quotes.
2
Start the Dockerfile with Python base image
Create a file named Dockerfile and write the line FROM python:3.12 as the first line to use the official Python 3.12 image.
Docker
Hint
The FROM instruction sets the base image for your Docker container.
3
Copy app.py and set the command
In the Dockerfile, add these two lines after FROM python:3.12: COPY app.py /app.py and CMD ["python", "/app.py"] to copy the script and run it.
Docker
Hint
COPY moves your script into the image. CMD tells Docker what to run when the container starts.
4
Build and run the Docker container
Run these two commands in your terminal: docker build -t my-python-app . to build the image, then docker run my-python-app to run the container and see the output.
Docker
Hint
Use docker build -t my-python-app . then docker run my-python-app. The output should be Hello from Docker!
Practice
(1/5)
1. Why is Docker useful for maintaining consistent environments across teams?
easy
A. It packages the app and its environment together in a container.
B. It automatically writes code for the team.
C. It replaces the need for version control systems.
D. It runs apps only on Windows machines.
Solution
Step 1: Understand Docker container purpose
Docker containers bundle the app with all its dependencies and environment settings.
Step 2: Compare options to Docker's role
Only the option stating "It packages the app and its environment together in a container." describes packaging app and environment together, ensuring consistency.
Final Answer:
It packages the app and its environment together in a container. -> Option A
Quick Check:
Docker = consistent app environment [OK]
Hint: Docker bundles app + environment for all team members [OK]
Common Mistakes:
Thinking Docker writes code automatically
Confusing Docker with version control
Believing Docker only works on Windows
2. Which of the following is the correct way to start a Docker container named webapp from an image called myapp:latest?
easy
A. docker build webapp myapp:latest
B. docker start myapp:latest webapp
C. docker create webapp myapp:latest
D. docker run --name webapp myapp:latest
Solution
Step 1: Recall Docker run syntax
The command to start a container with a name is: docker run --name [container_name] [image]
Step 2: Match the correct command
docker run --name webapp myapp:latest matches this syntax exactly, starting container 'webapp' from image 'myapp:latest'.
Final Answer:
docker run --name webapp myapp:latest -> Option D
Quick Check:
docker run --name = start container with name [OK]
Hint: Use 'docker run --name' to start named container [OK]
Common Mistakes:
Using 'docker start' to create container
Confusing 'docker create' with 'docker run'
Using 'docker build' to start containers
3. Given this Dockerfile snippet:
FROM python:3.12-slim
WORKDIR /app
COPY . /app
RUN pip install -r requirements.txt
CMD ["python", "app.py"]
What happens when you build and run this Docker image on different team members' machines?
medium
A. The app runs only if all team members have Python installed locally.
B. The app will fail because Python versions differ on each machine.
C. Each team member runs the app in the exact same environment regardless of their OS.
D. The app runs slower on Windows than Linux due to Docker limitations.
Solution
Step 1: Analyze Dockerfile purpose
The Dockerfile sets up a Python 3.12 environment inside a container, independent of host OS.
Step 2: Understand container consistency
Because the container includes Python and dependencies, all team members get the same environment.
Final Answer:
Each team member runs the app in the exact same environment regardless of their OS. -> Option C
Quick Check:
Docker container = consistent environment [OK]
Hint: Dockerfile builds same environment on all machines [OK]
Common Mistakes:
Assuming local Python is needed
Believing OS affects container environment
Thinking Docker slows app on Windows
4. A team member runs docker run myapp but gets an error: Cannot find image 'myapp' locally. What is the likely cause and fix?
medium
A. Docker is not installed; install Docker to fix this.
B. The image 'myapp' is not built locally; run docker build -t myapp . first.
C. The container name is missing; add --name myapp to the command.
D. The Dockerfile is missing; create a Dockerfile to fix this.
Solution
Step 1: Understand error meaning
The error means Docker cannot find the image 'myapp' locally or on Docker Hub.
Step 2: Identify fix
Building the image locally with docker build -t myapp . creates the image for use.
Final Answer:
The image 'myapp' is not built locally; run docker build -t myapp . first. -> Option B
Quick Check:
Image missing? Build it first [OK]
Hint: Build image before running container [OK]
Common Mistakes:
Thinking Docker is not installed
Confusing container name with image name
Assuming Dockerfile absence causes this error
5. Your team wants to ensure everyone uses the same database version inside Docker containers. Which approach best guarantees this consistency?
hard
A. Create a Docker Compose file specifying the exact database image version and share it with the team.
B. Ask each team member to install the database manually on their machine.
C. Use environment variables to let each member choose their preferred database version.
D. Run the database directly on the host OS instead of in Docker.
Solution
Step 1: Understand Docker Compose role
Docker Compose lets you define multi-container setups with exact image versions in one file.
Step 2: Evaluate options for consistency
Sharing a Compose file with fixed database image version ensures all run the same version easily.
Step 3: Reject other options
Manual installs or letting members choose versions cause inconsistency; running on host loses container benefits.
Final Answer:
Create a Docker Compose file specifying the exact database image version and share it with the team. -> Option A