Table of contents
- Learning points:
- Challenge 1: Write a simple Dockerfile that runs a Python script using an official base image (python:3.9) or serves a static HTML page using Nginx
- Challenge 2: Build a Docker image from your Dockerfile and tag it with a version (docker build -t myapp:v1 .).
- Challenge 3: Run a container from your custom image, map necessary ports, and test the app.
- Challenge 4: Push your custom Docker image to Docker Hub
- Challenge 5: Use docker exec -it <container_id> bash to enter a running container and explore it.
- Challenge 6: Run a detached container (-d flag), restart it, and check logs (docker logs <container_id>).
- 🚀 Learning Points from This Challenge
- 🔹 Useful Docker Commands & Their Explanations
Learning points:
🔹 What is Containerization? – How containers differ from virtual machines and why they are beneficial.
🔹 Docker Terminology – Image (template), Container (running instance), Dockerfile, Docker Hub (image registry).
🔹 Docker Architecture – Understanding the client-server model, Docker Engine, and Docker Daemon.
🔹 Working with Containers – Running, stopping, removing containers, mapping ports.
🔹 Building Docker Images – WritingDockerfile
, adding dependencies, and optimizing images.LEARN
What is Virtualisation & Containerisation? by Sandip Das - Click Here
Mastering Docker: A Comprehensive Guide from Basics to Advanced by Sandip Das - Click Here
Initial Tasks:
✅ Task 1: Install Docker on your system (Installation Guide).
✅ Task 2: Verify Docker installation by running: docker run hello-world
✅ Task 3: Run an Nginx web server container: docker run -d -p 80:80 nginx
Open http://localhost
in a browser to verify it's working.
✅ Task 4: Learn basic Docker commands:
List running containers:
docker ps
List all containers:
docker ps -a
View Docker images:
docker images
✅ Task 5: Explore Docker Hub
(hub.docker.com) and search for official images
GITHUB REPO - Link
Challenge 1: Write a simple Dockerfile
that runs a Python script using an official base image (python:3.9)
or serves a static HTML page using Nginx
Step 1: Create a Python file (app.py) - flask application
from flask import Flask
app = Flask(__name__)
@app.route('/')
def home():
return "Hello from Flask inside Docker!"
if __name__ == "__main__":
app.run(host='0.0.0.0', port=5000)
Step 2: Create a docker file (Dockerfile)
# Use lightweight Python base image
FROM python:3.9-slim
# Set working directory
WORKDIR /app
# Copy only necessary files first (helps with caching)
COPY requirements.txt ./
# Install dependencies
RUN pip install --no-cache-dir -r requirements.txt
# Copy the rest of the app
COPY app.py ./
# Expose Flask port
EXPOSE 5000
# Run the Flask app
CMD ["python", "app.py"]
Challenge 2: Build a Docker image from your Dockerfile
and tag it with a version (docker build -t myapp:v1 .
).
Step 1: Build the Dockerfile
docker build -t myapp:v1 .
Step 2: check the image using “docker images” command
docker images
Challenge 3: Run a container from your custom image, map necessary ports, and test the app.
docker run -d -p 5000:5000 --name mypythonapp myapp:v1
Step 1: Check the running container
Step 2: Check the Output - localhost on port 5000
Challenge 4: Push your custom Docker image to Docker Hub
Step 1: Log in to Docker Hub
- Enter your Docker Hub username and password when prompted.
docker login
Step 2: Tag the Image for Docker Hub
docker tag myapp:v1 your-dockerhub-username/myapp:v1
Step 3: Push the Image to Docker Hub
docker push your-dockerhub-username/myapp:v1
Step 4: Verify the Pushed Image
- Go to Docker Hub and check if the image is available.
Challenge 5: Use docker exec -it <container_id> bash
to enter a running container and explore it.
Step 1: Run a Docker Container
Step 2: Enter the Running Container
- Use the docker exec command to access the container's shell:
docker exec -it mypythonapp sh
Step 3: Explore the Container
Once inside, you can run commands to explore the container.
Challenge 6: Run a detached container (-d
flag), restart it, and check logs (docker logs <container_id>
).
Step 1: Run a Container in Detached Mode
docker run -d -p 5000:5000 --name flaskdetached myapp:v1
Step 2: Check Running Containers
docker ps
🚀 Learning Points from This Challenge
Using a Lightweight Base Image
- Instead of
python:3.9
, usingpython:3.9-slim
reduces image size and improves security.
- Instead of
Ensuring Flask Binds to the Correct IP
- Flask should bind to
0.0.0.0
inside the container, or it won’t be accessible externally.
- Flask should bind to
Proper Dockerfile Structuring
- Copy
requirements.txt
first, install dependencies, then copy the rest → efficient caching.
- Copy
Understanding Container Lifecycle
- A container may exit immediately if dependencies are missing or Flask is misconfigured.
Exposing and Mapping Ports Correctly
- Use
EXPOSE 5000
inDockerfile
, then-p 5000:5000
when running the container.
- Use
Debugging Containers Like a Pro
Check logs (
docker logs <container_id>
) if the container exits unexpectedly.Use
docker exec -it <container_id> sh
to enter a running container.
Pushing Images to Docker Hub
- Tag and push images to share and deploy them anywhere easily.
🔹 Useful Docker Commands & Their Explanations
Command | Explanation |
docker build -t myapp:v1 . | Build a Docker image from the current directory. |
docker run -d -p 5000:5000 --name myapp myapp:v1 | Run the container in detached mode and map ports. |
docker ps | List running containers. |
docker ps -a | List all containers (including stopped ones). |
docker logs myapp | Check logs of the running container. |
docker exec -it myapp sh | Enter a running container's shell. |
docker stop myapp | Stop a running container. |
docker rm myapp | Remove a stopped container. |
docker rmi myapp:v1 | Remove a Docker image. |
docker tag myapp:v1 mydockerhub/myapp:v1 | Tag an image before pushing to Docker Hub. |
docker push mydockerhub/myapp:v1 | Push the image to Docker Hub. |
docker network prune | Clean up unused Docker networks. |