🚧 Under construction — VoTechWorks is being built. A free tech program — complimentary, as our commitment to get America working.
voTechWorksGrade 13 · a vocational technology learning center, by urbanicity
newSchoolWorks.com › VoTechWorks › Technology & Design › Web & Software Development › Deployment: Servers, CI/CD, and Cloud Publishing › Lesson 7
TEC-WEB46-07 · Lesson 7 · 16 clock hours · lab

Introduction to Docker and Containerization

Objective. Students write a Dockerfile for a simple web application, build a Docker image, run the container locally, and describe how containerization improves environment consistency.

⚠ Pending professional review

This lesson, its worked examples and its practice questions has not yet been verified by an industry-certified professional or by a certification or approval body. It is a study draft and may contain errors. Do not use it as instructions for work on a job site or on live equipment. Always follow your instructor, your supervising professional, and current official references and regulations.

VoTechWorks program approval and accreditation are still in progress. Credentials are issued only by the certifying bodies named in each program, after their own exams.

What Is Containerization?

When you build a web application on your laptop, it works perfectly. Then you deploy it to a server and it crashes because the server runs a different Python version, is missing a library, or has a conflicting package. This is the classic "it works on my machine" problem. Containerization solves it by packaging your application together with everything it needs—runtime, libraries, configuration files—into a single portable unit called a container.

A container is not a virtual machine. A virtual machine emulates an entire computer, including its own operating system kernel, which makes VMs large and slow to start. Containers share the host operating system's kernel but keep each application's file system, processes, and network stack isolated. This makes containers lightweight, fast to start, and efficient with resources.

Docker: The Industry Standard Tool

Docker is the most widely used containerization platform. It provides:

When you install Docker Engine on a Linux VPS or local machine, you get all three components you need for this lab.

Images and Containers

Two terms are essential:

Images are built from a text file called a Dockerfile. Each line in a Dockerfile adds a layer to the image. Docker caches layers, so if you change only your application code, Docker reuses the cached OS and dependency layers—builds stay fast.

Anatomy of a Dockerfile

Here is a Dockerfile for a simple Python Flask application:

```

FROM python:3.11-slim

WORKDIR /app

COPY requirements.txt .

RUN pip install --no-cache-dir -r requirements.txt

COPY . .

EXPOSE 5000

CMD ["python", "app.py"]

```

FROM – Every Dockerfile starts with a base image. `python:3.11-slim` is an official, minimal Python 3.11 image. Using official images from Docker Hub is safer than building from scratch because they are maintained and regularly patched for security vulnerabilities.

WORKDIR – Sets the working directory inside the container. All subsequent commands run relative to this path.

COPY – Copies files from your host machine into the image. Copying `requirements.txt` before the rest of the code is intentional: it lets Docker cache the `pip install` layer separately. If you only change `app.py`, Docker skips reinstalling packages.

RUN – Executes a shell command during the image build. Use it to install dependencies. The `--no-cache-dir` flag keeps the image smaller by not storing pip's download cache.

EXPOSE – Documents which port the application listens on inside the container. It does not automatically open that port to the outside world; that happens at `docker run`.

CMD – The default command Docker runs when the container starts. Use the JSON array form (`["python", "app.py"]`) to avoid shell-wrapping issues.

Building and Running

From the folder containing your Dockerfile, build the image:

```

docker build -t my-flask-app .

```

The `-t` flag gives your image a name (tag). The `.` tells Docker to look for the Dockerfile in the current directory.

To run the container and make it accessible:

```

docker run -d -p 8080:5000 --name flask-demo my-flask-app

```

Open a browser to `http://localhost:8080` or run `curl http://localhost:8080` to verify the app responds.

Useful inspection commands:

| Command | What it does |

|---|---|

| `docker ps` | Lists running containers |

| `docker images` | Lists built images and their sizes |

| `docker logs flask-demo` | Shows container stdout/stderr |

| `docker stop flask-demo` | Stops the container |

| `docker rm flask-demo` | Removes the stopped container |

Why Containerization Improves Environment Consistency

Because the container image includes the exact runtime and dependencies, the same image runs identically on a developer's laptop, a QA server, and a production cloud instance. This consistency is foundational to modern CI/CD pipelines: your pipeline builds the image once, runs automated tests against it, and pushes that exact artifact to production—no environment drift.

Safety and Good Practices

Key terms8

ContainerA lightweight, isolated process that packages an application together with its dependencies and runs on a shared OS kernel, separate from other containers.
ImageA read-only, layered blueprint used to create containers; built from a Dockerfile and stored in a registry such as Docker Hub.
DockerfileA plain-text file containing step-by-step instructions (FROM, RUN, COPY, CMD, etc.) that Docker uses to build an image.
Docker EngineThe background service (daemon) installed on a host machine that builds images and manages the lifecycle of containers.
Port MappingThe -p flag in docker run that connects a port on the host machine to a port inside the container, making the containerized service reachable from outside.
Base ImageThe starting image named in the FROM instruction; provides the operating system layer and, often, a pre-installed runtime such as Python or Node.js.
LayerAn intermediate read-only file-system snapshot produced by each instruction in a Dockerfile; Docker caches layers to speed up subsequent builds.
Docker HubDocker's public registry where official and community-contributed images are hosted and can be pulled with docker pull or referenced in a FROM instruction.
In class: Install Docker Engine on the VPS (or a local machine), run the official hello-world container, and review the output explaining what happened at each step. · Write a Dockerfile for a provided Python Flask or PHP application, including base image selection, working directory, dependency installation via pip or composer, and CMD instruction; build the image with docker build. · Run the container with docker run mapping an external port to the container port, verify the app responds in a browser or with curl, and record image size and run command in the lab log.
Materials: Student laptops or VPS, Docker Engine, provided sample web application code, Dockerfile reference documentation (docs.docker.com), lab log
Check: Instructor inspects student's Dockerfile for correct syntax and runs docker build without errors; container must respond correctly on the mapped port. Lab log entry required. Both components must pass.

Practice check8

Answer each question, then check it. Nothing is recorded; this is your own practice.

CompTIA ITF+ - IT Concepts and Terminology
1. A developer's Flask app works on her laptop but fails on the production server because the server has a different Python version. Which benefit of Docker directly addresses this problem?
CompTIA Linux+ - System Management
2. Which Dockerfile instruction sets the working directory inside the container for all subsequent RUN, COPY, and CMD instructions?
CompTIA Linux+ - Networking
3. A student runs: docker run -d -p 3000:8080 my-app. Which statement correctly describes the port mapping?
CompTIA Linux+ - Security
4. Why is it a security best practice to avoid running the main process inside a container as the root user?
CompTIA ITF+ - IT Concepts and Terminology
5. A student copies requirements.txt into the image and runs pip install before copying the rest of the application code. What is the primary reason for this ordering?
CompTIA Linux+ - System Management
6. Which command lists all Docker images currently stored on the local machine along with their sizes?
CompTIA Linux+ - Security
7. A student wants to store a database password that her containerized Python app needs. Which approach follows security best practices?
CompTIA ITF+ - Infrastructure
8. How does a Docker container differ fundamentally from a virtual machine?
Record this lesson on my transcript. Sign in with your key and enrollment email to record lessons (enroll free if you have no key). This page stays open to read and practise.