Dev tools
What Is a Docker Container? A Plain Guide
What a Docker container is, how it differs from an image and a virtual machine, how it works under the hood, the commands you need, and common mistakes.
A Docker container is a running, isolated instance of an application together with everything it needs: its code, runtime, libraries and settings. It runs as an ordinary process on the host's operating system, but with its own view of the filesystem, network and process list, so it behaves as if it had a small machine to itself. Containers start in seconds, run the same way on a laptop, a server or the cloud, and can be thrown away and recreated at will.
The idea solves an old problem: "it works on my machine." Instead of installing an application and its dependencies on each server and hoping versions match, you package everything into an image once and run it as a container anywhere Docker runs. This guide explains what a Docker container is, how it differs from an image and from a virtual machine, how it works under the hood, the essential commands, how data and networking work, and the mistakes to avoid.
Docker Container at a Glance
| Item | Detail |
|---|---|
| What it is | A running, isolated instance of a container image |
| Built from | An image: a read-only, layered template with the app and its dependencies |
| Isolation | Linux namespaces (what it can see) and control groups (what it can use) |
| Operating system | Shares the host's kernel; no separate guest OS |
| Start-up time | Typically seconds or less |
| Persistence | Container filesystem is temporary; use volumes for data that must survive |
| Created with | docker run (or docker create plus docker start) |
| Standards | Images and runtimes follow Open Container Initiative (OCI) specifications |
| History | Docker was released as open source in March 2013 |
What Is a Docker Container?
What is a Docker container, in practice? Think of it as a sealed box with one job, such as a web server, a database or a background worker. Inside the box: the application and exactly the libraries and tools it needs. Outside: the host machine, which only needs Docker (or another compatible container runtime) installed.
When you run a container, Docker:
- Takes the image you name, downloading it from a registry such as Docker Hub if it isn't already local.
- Adds a thin writable layer on top of the image's read-only layers.
- Creates isolated namespaces so the process has its own filesystem view, process IDs, network interfaces and hostname.
- Applies resource limits with control groups (cgroups), such as maximum memory or CPU.
- Starts the image's configured command as the container's main process.
When that main process exits, the container stops. Delete the container and its writable layer goes with it, while the image remains, ready to start a fresh copy.
Docker Container vs Image
Docker container vs image is the most common point of confusion. The relationship is like a class and an object, or a recipe and a meal:
| Image | Container | |
|---|---|---|
| What it is | A read-only template | A running (or stopped) instance of an image |
| State | Never changes once built | Has a writable layer that changes as it runs |
| Built by | docker build from a Dockerfile | docker run from an image |
| Stored in | Local image store and registries | Local container store on one host |
| How many | One image | Many containers can run from the same image |
| Lifetime | Kept until deleted | Often short-lived and replaced |
An image is built in layers. Each instruction in a Dockerfile adds a layer:
FROM node:22-slim
WORKDIR /app
COPY package*.json ./
RUN npm ci --omit=dev
COPY . .
EXPOSE 3000
CMD ["node", "server.js"]Layers are cached and shared: ten containers from the same image share one copy of the read-only layers on disk, and rebuilding after a code change reuses the unchanged dependency layer. That's why the order of Dockerfile instructions matters: put things that change rarely, such as dependency installation, before things that change often, such as your source code.
Docker Container vs Virtual Machine
On Docker container vs virtual machine: both isolate applications, but at different levels.
| Docker container | Virtual machine | |
|---|---|---|
| What's virtualised | The operating system's view (processes, filesystem, network) | The hardware |
| Guest OS | None; shares the host kernel | A full operating system per VM |
| Size | Often tens to hundreds of MB | Usually several GB |
| Start-up | Seconds or less | Typically tens of seconds to minutes |
| Density | Many per host | Fewer per host |
| Isolation strength | Good; one shared kernel | Stronger; separate kernels |
| Runs a different OS kernel | No (Linux containers need a Linux kernel) | Yes |
On Windows and macOS, Docker Desktop runs Linux containers inside a lightweight Linux virtual machine, because the containers need a Linux kernel. In the cloud, containers usually run inside virtual machines anyway, combining the two: VMs for strong isolation between customers, containers for packaging and density within each VM.
Docker Container Commands
These Docker container commands cover most daily work:
| Task | Command |
|---|---|
| Run a container in the background, mapping a port | docker run -d --name web -p 8080:3000 myapp:1.0 |
| List running containers | docker ps |
| List all containers, including stopped | docker ps -a |
| See logs | docker logs -f web |
| Open a shell inside | docker exec -it web sh |
| Stop and start | docker stop web / docker start web |
| Remove a container | docker rm web (add -f to stop and remove) |
| Show resource use | docker stats |
| Inspect configuration | docker inspect web |
| Build an image | docker build -t myapp:1.0 . |
| Clean up unused data | docker system prune |
Useful docker run options include --rm (delete the container when it exits), -e KEY=value (environment variables), -v or --mount (attach storage), --memory and --cpus (resource limits) and --restart unless-stopped (restart policy for long-running services).
If commands fail with a permissions error on Linux, our guide to fixing Docker permission denied walks through the causes.
Data: Volumes and Bind Mounts
A container's writable layer disappears when the container is removed, so anything that must survive, such as database files or uploads, belongs outside it:
| Storage type | What it is | Good for |
|---|---|---|
| Named volume | Storage managed by Docker | Databases and persistent app data |
| Bind mount | A host folder mapped into the container | Local development with live code reloading |
| tmpfs mount | Memory-only storage | Temporary, sensitive scratch data |
Example: docker run -d -v pgdata:/var/lib/postgresql/data postgres:17 keeps the database files in a volume named pgdata, so you can upgrade or recreate the container without losing data.
Networking
Each container gets its own network interfaces. By default, containers join a bridge network on the host; you publish ports with -p host:container to reach them from outside. Containers on the same user-defined network can reach each other by name, which is how a web container finds its database:
docker network create app
docker run -d --name db --network app -e POSTGRES_PASSWORD=secret postgres:17
docker run -d --name web --network app -p 8080:3000 -e DATABASE_HOST=db myapp:1.0For multi-container apps, Docker Compose describes all the services, networks and volumes in one compose.yaml file and starts them with docker compose up.
Containers in Production
Running one container on one machine is simple. Running hundreds across many machines, with restarts, scaling, rolling updates and load balancing, needs an orchestrator. Kubernetes is the most widely used; our comparison of Docker vs Kubernetes explains how they fit together. Docker isn't the only way to run containers either: tools such as Podman run the same OCI images without a central daemon, as our guide to Docker vs Podman covers.
Security Basics
- Don't run as root inside the container unless you must; add a
USERinstruction to the Dockerfile. - Use small, trusted base images and rebuild regularly to pick up security patches.
- Never bake secrets into images. Pass them at runtime through environment variables or a secrets manager.
- Limit resources so one container can't starve the host.
- Scan images for known vulnerabilities in your build pipeline.
- Remember the shared kernel: a kernel vulnerability affects every container on the host, which is why multi-tenant platforms add VM-level isolation.
Common Mistakes
- Storing data in the container's writable layer and losing it on removal.
- Using `latest` tags in production, so you can't tell which version is running.
- Huge images from copying build tools and caches into the final image; use multi-stage builds.
- One container doing many jobs instead of one process per container.
- Ignoring logs and health checks.
FAQ
Is a Docker container a virtual machine?
No. A container is an isolated process sharing the host's kernel. A virtual machine runs a complete operating system on virtualised hardware.
Does a container keep data when it stops?
Yes, a stopped container keeps its writable layer. The data is lost when the container is removed, unless it's stored in a volume or bind mount.
Can one image run several containers?
Yes. You can start any number of containers from the same image; each gets its own writable layer and isolation.
What's the difference between Docker and container technology?
Docker is a set of tools for building, sharing and running containers. A container is the isolated running instance itself, which other tools can also run.
Do Docker containers work on Windows and Mac?
Yes, through Docker Desktop, which runs Linux containers in a lightweight virtual machine. Windows also supports native Windows containers for Windows applications.