Docker Tutorial for Beginners: Containers, Commands, Docker Compose, Dockerfile and Volumes, Step by Step
Quick answer
Docker is a tool that packages an application together with everything it needs to run, its code, runtime, libraries and configuration, into a portable unit called a container image. You run that image as a container, and it behaves the same on a developer laptop, a test server or in the cloud. To learn Docker, start with images, containers and the main commands (docker run, ps, logs, exec), then run several services together with Docker Compose, build your own image with a Dockerfile, push it to a private registry, and persist data with volumes.
Video chapters (16)
- 0:00 Intro and course overview
- 1:58 What is Docker?
- 10:56 What is a container?
- 19:40 Docker vs virtual machine
- 23:53 Docker installation
- 42:02 Main Docker commands
- 57:15 Debugging a container
- 66:39 Demo project overview: Docker in practice
- 70:08 Developing with containers
- 89:49 Docker Compose: running multiple services
- 102:02 Dockerfile: building our own Docker image
- 124:36 Private Docker repository on AWS
- 139:06 Deploy our containerized app
- 147:26 Docker volumes: persist data in Docker
- 153:03 Volumes demo: persistence for the demo project
- 165:13 Wrap up
Key takeaways
- A Docker image is the packaged application with everything it needs; a container is a running instance of that image.
- Containers share the host's operating system kernel, which makes them much lighter and faster to start than virtual machines.
- Images are shared through registries: public ones like Docker Hub, or private ones like Amazon ECR.
- The main commands are docker pull, run, ps, stop, start, logs and exec; with them you can run and debug almost anything.
- Docker Compose describes several containers and their configuration in one YAML file, so you start a whole application with one command.
- A Dockerfile is the recipe for building your own image: a base image, your files, the dependencies and the start command.
- Containers are disposable, so any data you need to keep, like a database's files, goes into a volume.
This is the written version of my Docker full course for beginners, which has been watched more than 6.5 million times. It takes you from “what is Docker?” to building, pushing and deploying your own containerized application. You can watch the video, read the guide, or do both: each section below matches a chapter in the video.
All commands in this guide use the current Docker syntax. The video was recorded in 2020, so where something has changed since then, like docker compose replacing docker-compose, I point it out.
What is Docker, and what problem does it solve?
Docker is a tool for packaging an application with everything it needs to run, its code, runtime, system libraries and configuration, into one portable artifact called an image. You run that image as a container, and it behaves the same everywhere: on your laptop, on a test server, or in the cloud.
To see why that matters, look at how applications were developed and deployed before containers:
| Without containers | With containers | |
|---|---|---|
| Development setup | Every developer installs and configures each service (database, cache, message broker) directly on their machine. Steps differ per operating system, and something always goes wrong. | Each service runs in its own container with one command. No installation on your machine, and the same setup for everyone. |
| Deployment | Developers hand over the artifact plus a text file of installation instructions. Operations installs and configures everything on the server, and dependency or version conflicts are common. | Developers and operations work with the same image. The only thing the server needs is a container runtime. |
| Different versions | Running two versions of the same app or database on one server is painful. | Each container is isolated, so you can run several versions side by side. |
If you want to go deeper into this part, read my articles What is Docker and What problems Docker solves.
What is a container, and how is an image different?
A container image is built in layers. At the bottom there is usually a small Linux base image, like Alpine, and on top come the layers for your runtime, your dependencies and your application. Because images are layered, Docker only downloads and stores the layers that change, which saves time and space.
- An image is the package. It doesn’t run; it’s a file stored in a registry or on your machine.
- A container is a running instance of an image. It has its own process, its own network settings and a writable layer on top of the image.
Images are shared through registries. Docker Hub is the biggest public registry, with official images for databases, web servers and programming languages. Companies store their own images in private registries, like Amazon ECR, Google Artifact Registry, GitLab’s container registry or Docker Hub private repositories.
Every image has a tag, which is usually its version, for example postgres:16 or redis:7.2. If you don’t specify a tag, Docker uses latest.
Docker vs virtual machine: what’s the difference?
An operating system has two layers: the kernel, which talks to the hardware, and the applications that run on the kernel. Docker and virtual machines virtualize different layers:
- A virtual machine virtualizes the whole operating system, kernel and applications. It boots its own full OS.
- A Docker container virtualizes only the application layer and uses the kernel of the host.
That’s why container images are often only megabytes in size and start in seconds, while VM images are gigabytes and take minutes to boot. The trade-off is compatibility: a Linux container needs a Linux kernel. On Mac and Windows, Docker Desktop solves this by running a lightweight Linux VM in the background.
I compare the two in detail, with examples of when to use which, in Docker vs virtual machine.
How do you install Docker?
- Mac and Windows: install Docker Desktop. It includes the Docker engine, the
dockercommand line tool and Docker Compose. - Linux: install Docker Engine with your distribution’s package manager, following the official installation guide for your distribution.
Check that it works:
docker version
docker run hello-world
The second command downloads a tiny test image and runs it. If you see a “Hello from Docker!” message, everything is set up.
What are the main Docker commands?
These are the commands you will use every day:
| Command | What it does |
|---|---|
docker pull redis:7.2 | Download an image from a registry |
docker images | List images on your machine |
docker run redis:7.2 | Create and start a new container from an image (pulls it first if needed) |
docker run -d redis:7.2 | Run it in the background (detached mode) |
docker run -d -p 6000:6379 --name my-redis redis:7.2 | Map host port 6000 to container port 6379 and give the container a name |
docker ps | List running containers |
docker ps -a | List all containers, including stopped ones |
docker stop my-redis / docker start my-redis | Stop or restart an existing container |
docker rm my-redis | Delete a stopped container |
Two things confuse almost every beginner here:
docker runalways creates a new container. If you run it five times, you get five containers. To restart a container you stopped, usedocker start.- Container ports vs host ports. A container’s port, like 6379 for Redis, is only reachable inside Docker’s network. To reach it from your machine, you bind it to a free port on the host with
-p host:container. Two containers can use the same container port, but each needs its own host port.
How do you debug a running container?
When a container doesn’t behave, these two commands get you most of the way:
docker logs my-redis # show what the application printed
docker logs -f my-redis # follow the logs live
docker exec -it my-redis /bin/sh # open a shell inside the running container
Inside the container you can check environment variables with env, look at files, and test network connections. Many small images don’t include bash, so /bin/sh is the safer choice. Type exit to leave.
How do you develop an application with containers?
In the demo project of the video, we build a simple web application with a JavaScript (Node.js) backend that stores data in MongoDB, and we use Mongo Express, a web UI for MongoDB, to look at the data. Instead of installing MongoDB on the laptop, we run both MongoDB and Mongo Express as containers.
For containers to talk to each other by name, put them in the same Docker network:
docker network create mongo-network
docker run -d -p 27017:27017 --network mongo-network --name mongodb \
-e MONGO_INITDB_ROOT_USERNAME=admin \
-e MONGO_INITDB_ROOT_PASSWORD=password \
mongo
docker run -d -p 8081:8081 --network mongo-network --name mongo-express \
-e ME_CONFIG_MONGODB_ADMINUSERNAME=admin \
-e ME_CONFIG_MONGODB_ADMINPASSWORD=password \
-e ME_CONFIG_MONGODB_SERVER=mongodb \
mongo-express
Mongo Express connects to the database simply as mongodb, the container name, because both containers are in the same network. Your Node.js app running on your laptop connects to localhost:27017, because that port is bound to the host.
The passwords here are for local development only. Never put real credentials in commands or files that end up in Git.
What is Docker Compose, and how do you run multiple services?
Typing long docker run commands for every service gets tedious fast. Docker Compose lets you write the same configuration in one YAML file and start everything with one command:
# docker-compose.yaml
services:
mongodb:
image: mongo
ports:
- 27017:27017
environment:
MONGO_INITDB_ROOT_USERNAME: admin
MONGO_INITDB_ROOT_PASSWORD: password
mongo-express:
image: mongo-express
ports:
- 8081:8081
environment:
ME_CONFIG_MONGODB_ADMINUSERNAME: admin
ME_CONFIG_MONGODB_ADMINPASSWORD: password
ME_CONFIG_MONGODB_SERVER: mongodb
depends_on:
- mongodb
docker compose -f docker-compose.yaml up -d # start all services
docker compose -f docker-compose.yaml down # stop and remove them
Compose automatically creates a shared network for all services in the file, so you don’t need docker network create anymore.
In the video I use the older docker-compose command (with a hyphen) and a version: line at the top of the file. Today Compose is built into Docker as docker compose (with a space), and the version field is no longer needed. The rest of the file works the same.
How do you build your own Docker image with a Dockerfile?
When your application is ready to deploy, you package it as its own image. The recipe for that is a Dockerfile:
# start from an official Node.js base image
FROM node:20-alpine
# set the working directory inside the image
WORKDIR /home/app
# copy the dependency list first, so this layer is cached
COPY package*.json ./
RUN npm install
# copy the application code
COPY . .
# the command that runs when a container starts
CMD ["node", "server.js"]
The most important instructions:
| Instruction | Purpose |
|---|---|
FROM | The base image you build on |
WORKDIR | The directory where the next instructions run |
COPY | Copy files from your machine into the image |
RUN | Run a command while building the image, like installing dependencies |
ENV | Set environment variables (never secrets) |
EXPOSE | Document which port the app listens on |
CMD | The command that starts the application when the container runs |
Build the image and run it:
docker build -t my-app:1.0 .
docker run -d -p 3000:3000 my-app:1.0
The . at the end tells Docker to use the current directory as the build context. Each time you change the Dockerfile or the code, rebuild the image. For production-ready images, read my 8 Docker best practices, like using small official base images, multi-stage builds and running as a non-root user.
Free download
Want to go from beginner to pro with Docker?
The Docker Roadmap lays out what to learn, week by week, from the first container to production-ready images, so you always know the next step.
How do you push an image to a private registry?
To deploy your image to a server, it has to be in a registry the server can pull from. In the video I use Amazon ECR (Elastic Container Registry), but the steps are the same for any registry:
- Create a repository in the registry (in ECR, one repository per image).
- Log in with
docker login. For ECR, the AWS CLI gives you the login token, so you don’t type a password. - Tag the image with the registry address, because the tag tells Docker where to push it.
- Push it.
aws ecr get-login-password --region eu-central-1 | \
docker login --username AWS --password-stdin <account-id>.dkr.ecr.eu-central-1.amazonaws.com
docker tag my-app:1.0 <account-id>.dkr.ecr.eu-central-1.amazonaws.com/my-app:1.0
docker push <account-id>.dkr.ecr.eu-central-1.amazonaws.com/my-app:1.0
For Docker Hub, the image name is simply <your-username>/my-app:1.0.
How do you deploy the containerized application?
On the server, the same Docker Compose file can now start your own application together with its database. You just add your image to the services, using the full registry name:
services:
my-app:
image: <account-id>.dkr.ecr.eu-central-1.amazonaws.com/my-app:1.0
ports:
- 3000:3000
mongodb:
image: mongo
# ... same as before
The server needs Docker installed and needs to be logged in to the private registry. Then docker compose up -d pulls all images and starts the whole application. In real projects, a CI/CD pipeline does the build, push and deploy steps automatically on every change; my GitHub Actions tutorial shows how to build and push a Docker image in a pipeline.
How do Docker volumes persist data?
A container has its own virtual file system. When you remove the container and start a new one, that data is gone. For a database, that’s a problem. Volumes solve it by mounting a directory from the host into the container, so the data lives outside the container’s lifecycle.
There are three ways to mount data:
| Type | Example | When to use |
|---|---|---|
| Host (bind) mount | -v /home/me/data:/var/lib/mysql | You choose the exact host folder, useful in development |
| Anonymous volume | -v /var/lib/mysql | Docker chooses the location; rarely used on purpose |
| Named volume | -v mongo-data:/data/db | Docker manages the location, you refer to it by name. Use this in production. |
In Docker Compose, named volumes are declared at the bottom of the file and referenced in the service:
services:
mongodb:
image: mongo
volumes:
- mongo-data:/data/db
volumes:
mongo-data:
Now you can stop, remove and recreate the MongoDB container, and the data is still there. Each database stores its data in a different path inside the container, like /data/db for MongoDB, /var/lib/mysql for MySQL and /var/lib/postgresql/data for PostgreSQL. Check the image’s documentation on Docker Hub for the right one.
What should you learn after Docker?
Docker runs containers on one machine. When you have many containers across many servers, you need a container orchestrator, and the standard one is Kubernetes. It takes care of scheduling containers on servers, restarting them when they fail, scaling them and connecting them with networking. My Kubernetes tutorial for beginners is the natural next step.
After that, these skills turn Docker knowledge into a DevOps skill set:
- CI/CD pipelines that build and push images automatically: see GitHub Actions and Jenkins
- Kubernetes in production, with Services and Helm charts
- Infrastructure as Code with Terraform, to create the servers and clusters your containers run on
My DevOps Roadmap shows the whole path, in order.
Frequently asked questions
Is Docker hard to learn for beginners?
No. You can learn the core of Docker, images, containers, the main commands, Docker Compose and Dockerfiles, in a few days of hands-on practice. It helps to know basic Linux commands and how a web application talks to a database, because most Docker examples use exactly that setup.
What is the difference between a Docker image and a container?
An image is the package: the application code, its runtime, libraries and configuration, stored in layers. A container is a running instance of an image, with its own process and file system on top of the image. You can start many containers from the same image, just like you can start a program many times.
What is the difference between Docker and a virtual machine?
A virtual machine virtualizes a whole computer and runs its own full operating system, including its own kernel. A Docker container only packages the application layer and shares the kernel of the host it runs on. That makes containers much smaller, often megabytes instead of gigabytes, and lets them start in seconds instead of minutes.
What is the difference between Docker and Docker Compose?
The docker command manages single images and containers. Docker Compose is a tool on top of it that defines several containers, their ports, environment variables, networks and volumes in one YAML file, and starts or stops them all together with docker compose up and docker compose down.
Is Docker free?
The Docker Engine and command line tools are open source and free. Docker Desktop, the app for Mac and Windows, is free for personal use, education, open source projects and small businesses, while larger companies need a paid subscription. Check Docker's current pricing page for the exact terms.
Should I learn Docker before Kubernetes?
Yes. Kubernetes runs and manages containers, so you need to understand images, containers, ports, environment variables and volumes first. Once you are comfortable building and running your own images with Docker, Kubernetes concepts like Pods, Deployments and Services make much more sense.