Install Docker and Run Your First Container
A step-by-step setup guide for Docker on Windows, macOS, and Linux. Install the engine, verify it works, and run your first container in under fifteen minutes.
What you'll learn
- ✓How to install Docker Desktop on Windows and macOS
- ✓How to install Docker Engine on Linux
- ✓How to verify the installation works end-to-end
- ✓How to run, list, stop, and remove containers
- ✓How to read what the docker run command actually does
Prerequisites
- •A computer running Windows 10/11, macOS, or a recent Linux distribution
- •Administrator access on the machine
- •Read What is Docker? first
In the previous post we covered what containers are and why Docker became the default tool for working with them. This guide is hands-on: by the end you will have a working Docker installation and a container you started, inspected, and removed yourself.
Pick the section that matches your operating system, install Docker, then continue from “Verify the installation.”
Step 1: Install Docker
One thing to know first. On Windows and macOS, you install Docker Desktop — a packaged application that includes the engine, a small Linux VM, and a GUI. On Linux, you install Docker Engine directly, since the host already has a Linux kernel.
On Windows
- Visit docker.com/products/docker-desktop and download the installer for Windows.
- Run
Docker Desktop Installer.exe. Leave the default options ticked — in particular, “Use WSL 2 instead of Hyper-V” should remain selected. WSL 2 (Windows Subsystem for Linux) is what provides the Linux kernel containers need. - When prompted, sign out and back in (or reboot) so your user is added to the
docker-usersgroup. - Launch Docker Desktop from the Start Menu. The first run takes a minute while it initialises the WSL backend. Wait until the whale icon in the system tray stops animating.
If the installer reports that WSL is missing, open PowerShell as Administrator and run wsl --install, then reboot and try again.
On macOS
- Visit docker.com/products/docker-desktop and download the installer.
- Choose the correct build — Apple Silicon for M1/M2/M3/M4 Macs, Intel for older models. If you are unsure, click the Apple menu → About This Mac and check the Chip line.
- Open the downloaded
.dmgfile and drag the Docker icon into Applications. - Launch Docker from Applications. Grant the permissions it asks for — it needs them to set up networking and the embedded Linux VM.
- Wait for the whale icon in the menu bar to stop animating. When it shows “Docker Desktop is running,” you are ready.
On Linux
Linux users install Docker Engine from the official Docker repositories. The exact commands vary by distribution; the most common is Ubuntu/Debian:
# Remove any old versions
sudo apt remove docker docker-engine docker.io containerd runc
# Install prerequisites
sudo apt update
sudo apt install ca-certificates curl gnupg
# Add Docker's official GPG key and repository
sudo install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | \
sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] \
https://download.docker.com/linux/ubuntu $(. /etc/os-release && echo $VERSION_CODENAME) stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
# Install the engine and Compose plugin
sudo apt update
sudo apt install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
For Fedora, Arch, and others, follow the matching instructions at docs.docker.com/engine/install.
After installation, add your user to the docker group so you do not have to prefix every command with sudo:
sudo usermod -aG docker $USER
newgrp docker
Step 2: Verify the installation
Open a terminal (PowerShell on Windows, Terminal on macOS, your shell of choice on Linux) and run:
docker --version
docker compose version
You should see version numbers for both — something like Docker version 27.x.x and Docker Compose version v2.x.x. If both commands respond, the client is installed correctly.
Now verify that the engine is running and can pull and run an image:
docker run hello-world
The first time you run this, Docker will:
- Notice that the
hello-worldimage is not on your machine. - Pull it from Docker Hub (a few hundred kilobytes).
- Create a container from it.
- Run it. The container prints a confirmation message and exits.
If you see the message that begins with “Hello from Docker!”, everything works. If you see “Cannot connect to the Docker daemon,” the engine is not running — start Docker Desktop on Windows/macOS, or run sudo systemctl start docker on Linux.
Step 3: Run a real container
hello-world is a self-checking image; it is not very useful on its own. Let us run something with actual behaviour — the nginx web server:
docker run -d -p 8080:80 --name web nginx
Read that command carefully — every flag matters.
docker run— create and start a container.-d— detached mode. Run the container in the background instead of attaching your terminal to it.-p 8080:80— publish a port. Map port8080on your host to port80inside the container. Thenginxprocess inside listens on80; you reach it from your machine on8080.--name web— give the container a friendly name. Without this, Docker invents one likeeager_lovelace.nginx— the image to run. With no tag, Docker usesnginx:latest.
Open a browser and visit http://localhost:8080. You should see the default “Welcome to nginx!” page. A real web server is running on your machine, served by a container.
Try it yourself. Without stopping the first container, start a second nginx on a different host port:
docker run -d -p 8081:80 --name web2 nginxVisit http://localhost:8081. You now have two independent web servers running side by side, both from the same image. This is the practical meaning of “containers are cheap to start.”
Step 4: Inspect what is running
Use these commands constantly while learning Docker.
# List running containers
docker ps
# List all containers, including stopped ones
docker ps -a
# Show the most recent log output from a container
docker logs web
# Follow the logs live (like tail -f)
docker logs -f web
# Open a shell inside the running container
docker exec -it web bash
docker exec -it web bash is particularly useful. The -i and -t flags together give you an interactive terminal inside the container’s filesystem. You can run ls, cat /etc/nginx/nginx.conf, or anything else as if you had SSH’d into a tiny Linux machine. Type exit to return to your host shell. The container keeps running.
Step 5: Stop and clean up
A running container costs memory and a small amount of CPU. When you are done, stop and remove it:
docker stop web
docker rm web
docker stop web2
docker rm web2
docker stop sends a polite shutdown signal and waits for the process to exit. docker rm deletes the stopped container. The image stays on your machine for next time.
To see the images you have downloaded:
docker images
To delete an image you no longer want:
docker rmi nginx
You cannot delete an image while any container (running or stopped) still references it — remove the containers first.
Try it yourself. Run docker run -d --name greeter alpine sleep 60. This starts a tiny Alpine Linux container that sleeps for 60 seconds and exits. While it is alive, run docker ps to see it. After a minute, run docker ps (empty) and docker ps -a (you will see it in Exited state). Then docker rm greeter to clean up.
Common installation problems
A few errors are extremely common at this stage.
Cannot connect to the Docker daemon at unix:///var/run/docker.sock
The engine is not running. On Windows/macOS, launch Docker Desktop and wait for the whale icon to stop animating. On Linux, run sudo systemctl start docker and sudo systemctl enable docker so it starts automatically on boot.
permission denied while trying to connect to the Docker daemon socket (Linux)
Your user is not in the docker group. Run sudo usermod -aG docker $USER, then log out and back in (or run newgrp docker).
port is already allocated
Another process on your machine is using the host port you asked for. Either stop that process or pick a different port (-p 9090:80 instead of -p 8080:80).
Docker Desktop requires a newer WSL kernel version (Windows)
Open PowerShell as Administrator and run wsl --update, then restart Docker Desktop.
Recap
You now have:
- A working Docker installation, verified with
docker run hello-world - A real container you started, inspected with
docker psanddocker logs, and removed - Practical familiarity with
docker run,docker ps,docker exec,docker stop, anddocker rm - A clear mental model of host ports vs container ports, and detached vs foreground mode
Every later post in this series builds on these commands.
Next steps
The next post moves from running other people’s images to building your own. You will write a Dockerfile for a small application, build an image from it, and run that image as a container.
→ Next: Writing Your First Dockerfile
Questions or feedback? Email codeloomdevv@gmail.com.