This lesson gets Docker running on your machine and teaches the container life cycle: run, list, stop, start, inspect, exec and remove. These are the commands you will type every day, so practise them until they feel automatic.
In simple words
- Docker Engine: the background service (dockerd) that builds and runs containers.
- Docker CLI: the docker command you type. It sends your requests to the engine.
- Container life cycle: the states a container moves through: created, running, stopped, removed.
- Detached mode (-d): running a container in the background so your terminal stays free.
The pieces of Docker
| Piece | What it is |
|---|---|
Docker CLI (docker) | The command you type. It only sends requests |
Docker daemon (dockerd) | The background service that builds images and runs containers |
| containerd and runc | Lower-level runtimes the daemon uses to actually start containers |
| Docker Desktop | macOS and Windows app that runs all of the above in a small Linux VM |
| Registry | Where images are stored and downloaded from (Docker Hub by default) |
The CLI talks to the daemon over a socket, /var/run/docker.sock. That is why docker commands fail with "Cannot connect to the Docker daemon" when the service is not running.
Installing
macOS and Windows
Install Docker Desktop from docker.com (free for personal use, education and small businesses; larger companies need a paid subscription). Alternatives on macOS are OrbStack, Colima and Rancher Desktop.
Ubuntu (servers and CI runners)
Use Docker's official repository rather than the older docker.io package:
sudo apt-get updatesudo apt-get install -y ca-certificates curlsudo install -m 0755 -d /etc/apt/keyringssudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.ascecho "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] \ https://download.docker.com/linux/ubuntu $(. /etc/os-release && echo $VERSION_CODENAME) stable" \ | sudo tee /etc/apt/sources.list.d/docker.listsudo apt-get updatesudo apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-pluginThen let your user run Docker without sudo (log out and back in afterwards):
sudo usermod -aG docker $USERMembership of the docker group is effectively root access to the host, so only add trusted users.
Amazon Linux 2023
sudo dnf install -y dockersudo systemctl enable --now dockersudo usermod -aG docker ec2-userCheck it works
docker versiondocker run --rm hello-worldThe container life cycle
created → running → (paused) → stopped (exited) → removedRun
docker run -d --name web -p 8080:80 nginxThe most important docker run options:
| Option | Meaning | Example |
|---|---|---|
-d | Detached: run in the background | -d |
--name | A name instead of a random one | --name web |
-p host:container | Publish a port | -p 8080:80 |
-e KEY=value | Set an environment variable | -e TZ=Asia/Kolkata |
-v src:dest | Mount a volume or folder | -v data:/data |
--rm | Delete the container when it stops | --rm |
-it | Interactive with a terminal | -it ubuntu bash |
--restart | Restart policy | --restart unless-stopped |
Anything after the image name is the command to run inside the container, replacing the image default:
docker run --rm alpine echo "hello from alpine"docker run -it --rm ubuntu:24.04 bash # a throwaway Ubuntu shellList
docker psdocker ps -aCONTAINER ID IMAGE STATUS PORTS NAMES3f9a1c2b7d4e nginx Up 2 seconds 0.0.0.0:8080->80/tcp webdocker ps shows running containers; -a includes stopped ones, which is where you find crashed containers and their exit codes.
Test it
curl -I http://localhost:8080Logs
docker logs webdocker logs -f web follows the log live, and --tail 50 shows only the last 50 lines.
Stop, start and remove
docker stop webdocker ps -adocker start webdocker rm -f webdocker stopsends SIGTERM, waits 10 seconds, then SIGKILL, exactly like the Linux process lesson.docker rmdeletes a stopped container;rm -fstops and deletes in one go.
Get inside: exec
docker exec -it web bash # open a shell in a running containerdocker exec web nginx -t # run one command inside itexec runs an extra process in an existing container. It is for debugging, not for making changes you want to keep.
Inspect
docker inspect web # full JSON config and statedocker inspect -f '{{.NetworkSettings.IPAddress}}' webdocker stats --no-stream # live CPU and memory useHousekeeping
Stopped containers and unused images pile up and fill disks, especially on CI runners:
docker container prune # remove all stopped containersdocker image prune -a # remove images not used by any containerdocker system df # how much space Docker usesdocker system prune -a # everything unused (careful on shared hosts)Real-world scenario: a quick database for local development
You need PostgreSQL to test a feature, without installing it:
docker run -d --name pg -e POSTGRES_PASSWORD=devpass -p 5432:5432 postgres:16docker psdocker logs pgYour app connects to localhost:5432. When you are done, docker rm -f pg and your laptop is clean again. This pattern alone saves hours of installing and uninstalling services.
Common mistakes
- Forgetting
-d, so the terminal is attached to the container and Ctrl+C stops it. - Port already allocated: another container or app uses 8080. Pick another host port (
-p 8081:80) or stop the other one. - Using
latesteverywhere. Pin versions (postgres:16.4) so a new release does not surprise you. - Running everything with
sudo dockerinstead of fixing group membership, then creating root-owned files in your projects.
Practice
- Run nginx in the background on port 8080, check it with
curl, read its logs, then remove it. - Start a throwaway Alpine container that prints the date and deletes itself.
- Run PostgreSQL with a password environment variable and find it in
docker ps. - Stop a container and find its exit code with
docker ps -a.
Cheat sheet
| Command | What it does |
|---|---|
docker run -d --name N -p H:C IMAGE | Run in the background with a port |
docker ps -a | All containers and their status |
docker logs -f N | Follow logs |
docker exec -it N sh | Shell inside a running container |
docker stop N / docker start N | Stop / start |
docker rm -f N | Force remove |
docker system df / docker system prune | Disk usage / clean up |
Key takeaways
- The CLI is a client; the daemon does the work.
run,ps,logs,exec,stopandrmcover the whole container life cycle.- Pin image versions and clean up regularly so disks do not fill.
You've finished the free preview of Docker
Upgrade to Pro to unlock the remaining 16 lessons, every practice lab and a certificate when you finish.
See plans