---
title: Docker for Beginners: Commands, Dockerfile, Images
url: https://www.masaischool.com/resources/docker-for-beginners-explained
description: What Docker is, Docker vs a virtual machine, a proofread Dockerfile for a Python app, the commands you need and the current Docker Desktop licence terms.
updated: 2026-12-01
publisher: Masai (https://www.masaischool.com)
---

# Docker for beginners: what it is, the commands and a Dockerfile

> **Short answer**
>
> Docker packages an app and everything it needs into an image, and runs that image as an isolated process called a container. Unlike a virtual machine, containers share the host's kernel, so they start in about a second and are small. You describe the image in a Dockerfile and run it with docker run.

This page is for students who keep hearing "just use Docker" and want the model first, then the five commands that do most of the work. We read the official Docker documentation on 1 December 2026 and checked every command against its reference page. We did not run them: Docker is not installed on the machine used to write this, so no terminal output is shown and none is invented. The Python app below was run directly with python3 and answered a request on port 8000.

Docker is usually the step after you have a working app and before you deploy it. For where it fits, see the [DevOps engineer roadmap](https://www.masaischool.com/resources/devops-engineer-roadmap) and [DevOps project ideas](https://www.masaischool.com/resources/projects/devops-project-ideas).

1. **Dockerfile** - recipe: base image, files, command
2. **Image** - built, read-only package from the recipe
3. **Container** - a running process started from the image
4. **Registry** - Docker Hub or similar, where images are shared
How the four Docker pieces connect.

## Docker vs a virtual machine

Docker runs apps in containers, and a container is, in Docker's own words, an ordinary process that runs in isolation with its own filesystem, its own view of processes and network, and limits you can set. A virtual machine instead boots a whole guest operating system with its own kernel. The comparison comes from Docker's [What is a container?](https://docs.docker.com/get-started/docker-concepts/the-basics/what-is-a-container/) guide, verified by Docker on 21 October 2026.

|  | Container | Virtual machine |
|---|---|---|
| What it is | An isolated process on the host | A full simulated computer |
| Operating system | Shares the host kernel; carries only the app's files | Boots its own guest OS and kernel |
| Start time | About a second, per Docker's guide | Longer: an OS must boot |
| Isolation | Strong, but separated by a shared kernel | Stronger: separated by a hardware boundary |
| Typical size | Smaller, since there is no guest OS | Larger |
| Common use | One container per app part: backend, database | Cloud servers, strong tenant separation |

The two combine in practice. Cloud servers are usually VMs that run many containers, and on macOS and Windows Docker Desktop runs your Linux containers inside a small Linux VM it manages, because Linux containers need a Linux kernel to share.

## Image vs container vs volume

Beginners mix these three up because the commands look alike. An image is the recipe's finished output; a container is an image that is running; a volume is data that survives the container.

| Term | Is it running? | What it holds | Analogy | Key commands |
|---|---|---|---|---|
| Dockerfile | No, it is a text file | Build instructions | A recipe | docker build |
| Image | No, it is immutable | OS files, runtime, dependencies, your code, the start command | A frozen meal | docker pull, docker images, docker image rm |
| Container | Yes, or stopped | A live process plus a thin writable layer | The meal being eaten | docker run, docker ps, docker stop, docker rm |
| Volume | Not applicable | Data that must outlive a container | An external drive | docker volume create, docker run -v |
| Registry | Not applicable | Stored images with tags | An app store | docker pull, docker push |

The point about volumes is the one that costs people data: Docker's guide on [persisting container data](https://docs.docker.com/get-started/docker-concepts/running-containers/persisting-container-data/) says a container keeps the files it writes inside itself, and removing the container removes them. Put database files in a named volume (-v mydata:/var/lib/data) or a bind mount of a folder on your disk.

## A Dockerfile for a small Python app, line by line

A tiny web server, using only the standard library so there is nothing to install. We ran it with python3 app.py and a request to http://localhost:8000 returned Hello from a container. APP_ENV=unset.# app.py import os from http.server import BaseHTTPRequestHandler, HTTPServer class Hello(BaseHTTPRequestHandler): def do_GET(self): body = f"Hello from a container. APP_ENV={os.getenv('APP_ENV', 'unset')}\n".encode() self.send_response(200) self.send_header("Content-Length", str(len(body))) self.end_headers() self.wfile.write(body) if __name__ == "__main__": # 0.0.0.0, not 127.0.0.1: inside a container, localhost is the container only HTTPServer(("0.0.0.0", 8000), Hello).serve_forever()

The Dockerfile. We proofread it against the [Dockerfile reference](https://docs.docker.com/reference/dockerfile/) and confirmed the base tag python:3.12-slim exists on Docker Hub (last pushed 28 November 2026). We did not build it.# Dockerfile FROM python:3.12-slim WORKDIR /app COPY requirements.txt . RUN pip install --no-cache-dir -r requirements.txt COPY . . EXPOSE 8000 CMD ["python", "app.py"]

- FROM python:3.12-slim picks the base image: Python on a minimal Debian. Pin a tag like this, not latest, so builds stay repeatable.
- WORKDIR /app sets the folder inside the image for the following commands.
- COPY requirements.txt . then RUN pip install ... come before copying your code. Docker caches each step as a layer, so dependencies are reinstalled only when requirements.txt changes. This demo app has no packages, so create an empty requirements.txt; a real app lists them there.
- COPY . . brings in the rest of the code. Add the .dockerignore file below so secrets and junk stay out.
- EXPOSE 8000 documents the port the app listens on. It does not publish it; -p does that at run time.
- CMD ["python", "app.py"] is the exec form (a JSON array), which the reference recommends for ENTRYPOINT and works well for CMD.
# .dockerignore .git .env __pycache__/ .venv/ node_modules/

The most common beginner failure is binding the server to 127.0.0.1. Inside a container that address means only the container, so the published port gives you an empty reply. Bind to 0.0.0.0, as in app.py. Next steps once this works: add a non-root USER (documented in the Dockerfile reference) and a multi-stage build.

## The Docker commands you will actually type

Each command below has a page in Docker's [CLI reference](https://docs.docker.com/reference/cli/docker/). The flags shown were checked there: -d runs in the background, -p publishes a port as hostPort:containerPort, -e sets an environment variable, -v mounts a volume, --rm removes the container when it exits, --name names it.

| Command | What it does |
|---|---|
| docker build -t hello-app . | Builds an image from the Dockerfile in this folder and tags it |
| docker run --rm -d --name hello -p 8000:8000 hello-app | Starts a container in the background, port 8000 on your machine to 8000 inside |
| docker ps and docker ps -a | Lists running containers, then all containers including stopped ones |
| docker logs hello | Prints what the container wrote to stdout and stderr |
| docker exec -it hello sh | Opens a shell inside a running container for debugging |
| docker stop hello and docker rm hello | Stops a container, then deletes it |
| docker images and docker image rm <image> | Lists local images, then deletes one |
| docker pull <image> | Downloads an image from a registry |
| docker volume create mydata | Creates a named volume |
| docker compose up | Starts all services defined in a compose.yaml: app plus database in one command |

A full cycle for the demo app, to run once Docker is installed:docker build -t hello-app . docker run --rm -d --name hello -p 8000:8000 -e APP_ENV=dev hello-app curl http://localhost:8000 docker logs hello docker stop hello

When you have several containers, such as an app and a database, move to Compose. Everything above is deployed for free in [free hosting for AI-built apps](https://www.masaischool.com/resources/tools/free-hosting-for-ai-built-apps), and the pipeline that builds an image on every push is covered in [CI/CD pipeline explained](https://www.masaischool.com/resources/ci-cd-pipeline-explained).

## Docker Desktop licence: what the current terms say

Docker Desktop, the app for Windows, macOS and Linux, is not the same thing as the open-source Docker Engine. As of 1 December 2026, Docker's [licence page](https://docs.docker.com/subscription/desktop-license/) says Docker Desktop is licensed under the Docker Subscription Service Agreement and is free for personal use, education, non-commercial open source projects, and small businesses (fewer than 250 employees and less than $10 million in annual revenue). It requires a paid subscription for professional use in larger organisations and for government entities.

Docker's page also states that the licensing of Docker Engine and the Moby open-source projects is not changing. So a student learning at home or in college is in the free category, while a larger company's employees need their employer to hold a subscription. Terms change: the agreement shows a last-updated date of 26 August 2026 as read on 1 December 2026. This is a summary of a public page, not legal advice; read the current terms before relying on it at work.

## Frequently asked questions

### What is Docker in simple terms?

Docker is a tool that packs an app with everything it needs, such as the language runtime and libraries, into an image. You run that image as a container, an isolated process, and it behaves the same on your laptop, a teammate's machine and a server. It removes the "works on my machine" problem.

### What is the difference between a Docker image and a container?

An image is a read-only package: files, dependencies and a start command. A container is a running instance of an image, with a thin writable layer on top. You can start many containers from one image, and deleting a container does not delete the image.

### Is Docker the same as a virtual machine?

No. A virtual machine boots a full guest operating system with its own kernel. A container is a process that shares the host's kernel, so it is smaller and starts in about a second. VMs isolate more strongly. Cloud servers often run containers inside VMs, so the two are used together.

### Is Docker free for students?

Docker's licence page, read on 1 December 2026, lists personal use and education as free for Docker Desktop, and non-commercial open source as free too. Larger companies need a paid subscription for professional use. Docker Engine, the open-source part, has its own licence that Docker says is not changing.

### How do I stop losing data when a container is deleted?

Data written inside a container is removed with it. Store anything you care about in a named volume (docker volume create, then -v name:/path) or a bind mount of a folder on your disk, so the data lives outside the container and survives replacement.

### What is the difference between CMD and RUN in a Dockerfile?

RUN executes during the image build, for example installing packages, and its result is saved in the image. CMD sets the default command that runs when a container starts from the image. A Dockerfile can have many RUN lines but only the last CMD takes effect.
