Programming
Docker explained for beginners: images, containers and your first app
What Docker is, how images and containers differ, how containers compare with virtual machines, and the commands to run and build your first container.
“It works on my machine” is one of the oldest problems in software. Your app runs perfectly on your laptop, then fails on a colleague’s computer or on the server because of a different library version or a missing setting. Docker solves this by packaging an application together with everything it needs to run.
What Docker actually does
Docker lets you put your code, its runtime, its libraries and its configuration into a single package called an image. Anyone with Docker can run that image and get the same result, whether they use Windows, macOS or Linux.
Images and containers
These two words cause the most confusion, so it is worth getting them clear:
- An image is a read-only template. Think of it as a recipe, or as a class in programming.
- A container is a running instance of an image. It is the dish cooked from the recipe, or an object created from the class.
You can start many containers from the same image, and each one runs in isolation from the others.
Containers versus virtual machines
A virtual machine runs a complete guest operating system on top of virtual hardware. A container shares the host computer’s operating system kernel and isolates only the application and its dependencies. As a result, containers usually start in seconds, use less memory and are much smaller to store and share. Virtual machines still make sense when you need a different operating system or stronger isolation.
Your first container
After installing Docker, try this:
docker run hello-world
Docker downloads the hello-world image from Docker Hub, starts a container and prints a message. Next, run a real web server:
docker run -d -p 8080:80 --name web nginx
Open http://localhost:8080 in your browser and you will see the nginx welcome page. Here -d runs the container in the background, and -p 8080:80 maps port 8080 on your computer to port 80 inside the container.
Commands you will use every day
docker pslists running containers; add-ato include stopped ones.docker imageslists the images on your machine.docker logs webshows a container’s output.docker stop webanddocker rm webstop and remove a container.docker exec -it web shopens a shell inside a running container.
Building your own image with a Dockerfile
A Dockerfile is a text file of instructions for building an image. Here is one for a small Python app:
FROM python:3.12-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD ["python", "app.py"]
Build and run it:
docker build -t my-app .
docker run my-app
Notice that requirements.txt is copied and installed before the rest of the code. Docker caches each step, so when you change only your code, it skips reinstalling the libraries and rebuilds much faster.
Keeping data with volumes
Anything written inside a container is lost when the container is removed. To keep data, such as a database’s files, use a volume:
docker run -d -v pgdata:/var/lib/postgresql/data -e POSTGRES_PASSWORD=secret postgres
The data now lives in the pgdata volume and survives restarts and upgrades.
Running several containers with Docker Compose
Real applications often need several services, such as a web app, a database and a cache. Docker Compose describes them all in one compose.yaml file and starts them together with docker compose up. It is the usual next step once single containers feel comfortable.
Where to go next
Practise by containerising a small project of your own, then add a database with Compose. Our Learn Docker: Full Masterclass app walks through images, networking, volumes and Compose step by step, with quizzes and a completion certificate. If version control is new to you, start with the Git commands every beginner should know.





