Docker Basics: Containers, Images, Registries and Essential Commands

DevOps Certification 22 min min read Updated: Nov 17, 2026 Beginner
Docker Basics: Containers, Images, Registries and Essential Commands
Beginner Topic 1 of 4

The Problem: It Works on My Machine

An app needs the right language version, libraries and settings. On a different computer something differs and it breaks. Docker packages the app together with everything it needs into a container image. That image runs the same on a laptop, a test server and the cloud.

Containers vs Virtual Machines

Virtual machineContainer
IncludesFull guest operating systemApp and libraries only, shares the host kernel
SizeGigabytesMegabytes to a few hundred MB
Start timeMinutesSeconds or less
IsolationStrong (hardware level)Process level (namespaces and cgroups)
DensityFew per hostMany per host
How it works: Linux namespaces give each container its own view of processes, network and filesystem. cgroups limit CPU and memory. A container is just a normal Linux process with walls around it.

Key Words

TermMeaning
ImageRead-only template: app plus dependencies, built in layers
ContainerA running (or stopped) instance of an image
DockerfileText recipe to build an image
RegistryServer that stores images (Docker Hub, GitHub Container Registry, Amazon ECR, Azure ACR, Google Artifact Registry)
VolumeStorage that survives container deletion
NetworkVirtual network connecting containers

Analogy: an image is a class, a container is an object. You can start many containers from one image.

Install and Check

bash # Ubuntu quick install using Docker's convenience script (good for labs) curl -fsSL https://get.docker.com | sudo sh sudo usermod -aG docker $USER # run docker without sudo (log out and in) docker --version docker run hello-world # downloads a tiny image and prints a welcome message
Windows and macOS: Install Docker Desktop. For production Linux servers, use the official apt or dnf repositories rather than the convenience script.

Run Your First Containers

bash docker run nginx # runs in the foreground; Ctrl+C stops it docker run -d --name web -p 8080:80 nginx # detached, named, host port 8080 -> container port 80 curl http://localhost:8080 # nginx welcome page

What the flags mean:

FlagMeaning
-dDetached: run in the background
--name webGive the container a name
-p 8080:80Publish: host_port:container_port
-e KEY=valueSet an environment variable
-v name:/pathMount a volume
--rmDelete the container when it exits
-itInteractive terminal (for shells)
--restart unless-stoppedRestart after crashes or reboots
--memory 512m --cpus 1Resource limits

Manage Containers

bash docker ps # running containers docker ps -a # include stopped ones docker logs web # output of the app docker logs -f --tail 50 web # follow the last 50 lines docker exec -it web bash # open a shell inside a running container docker inspect web # full JSON details (IP, mounts, env) docker stats # live CPU and memory docker stop web # graceful stop (SIGTERM, then SIGKILL) docker start web docker restart web docker rm web # remove a stopped container docker rm -f web # force remove a running one

Run a Database in One Command

bash docker run -d --name db \ -e POSTGRES_PASSWORD=secret \ -e POSTGRES_DB=shop \ -p 5432:5432 \ -v pgdata:/var/lib/postgresql/data \ postgres:17 docker exec -it db psql -U postgres -d shop -c "select version();"
Persistence: Data written inside a container disappears when the container is removed. Use a volume (-v pgdata:/var/lib/postgresql/data) for anything you want to keep.

Images

bash docker pull python:3.13-slim # download an image docker images # list local images docker image inspect python:3.13-slim docker history python:3.13-slim # see the layers docker rmi python:3.13-slim # delete an image docker tag myapp:1.0 myrepo/myapp:1.0 docker login docker push myrepo/myapp:1.0

An image name looks like registry/namespace/name:tag. A tag is a label such as 1.0 or latest. The latest tag is just a name, not a guarantee of newest, so pin exact versions in real projects.

Image Layers

Each instruction in a Dockerfile creates a read-only layer. Layers are cached and shared, so pulling or rebuilding only changes what is different. This is why instruction order matters (next lesson).

Clean Up Disk Space

bash docker system df # how much space Docker uses docker container prune # remove stopped containers docker image prune -a # remove unused images docker volume prune # remove unused volumes (careful: data!) docker system prune -a --volumes # remove almost everything unused

Troubleshooting a Container That Exits

  1. docker ps -a shows the exit code. Code 0 is normal exit, 137 usually means out of memory or killed, 1 is an application error
  2. docker logs <name> shows why
  3. A container stops when its main process ends. A shell script that finishes means the container finishes
  4. docker run -it --entrypoint sh image lets you look inside
  5. docker inspect checks environment and mounts, and docker events shows daemon activity

Practice Tasks

  1. Run nginx on port 8081 and change the page by mounting a local index.html with -v $(pwd)/site:/usr/share/nginx/html:ro
  2. Start a PostgreSQL container with a volume, create a table, remove the container, start a new one with the same volume and check the data survived
  3. Run a container with --memory 64m and observe it in docker stats
  4. Find all stopped containers and remove them with one command
  5. Explain why docker run -p 8080:80 is needed to reach the app from your browser

Conclusion

Docker packages an app and its dependencies into an image, and runs it as an isolated container. Learn run, ps, logs, exec, volumes, port mapping and cleaning up, and you can run almost any software in seconds.

Quick Summary: image = template, container = instance; docker run -d -p -e -v; logs/exec/inspect; volumes for data; pin tags.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators