Containers are processes
Start a container, find its process, and see how Linux gives it a separate view and a resource budget.
What you will understand
- Explain what runs when a container starts.
- Identify what namespaces hide and what control groups measure or limit.
- Predict which files survive a stop, a restart, and a removal.
What a container runs
Start with something familiar: a command such as sleep 300 runs for five minutes and then exits. A container runs a program too. When Docker starts the example below, the program it launches is a shell. The shell prints a message, starts sleep, and eventually exits. The container’s running state follows the lifetime of its main process.
A container is a way to run one or more processes with an isolated view of parts of the system and a configured share of resources. On Linux, those processes use a Linux kernel shared with other processes on the same host. On Docker Desktop for Mac or Windows, Linux containers run inside a Linux environment, so the shared kernel in this explanation is the Linux kernel there.
A Linux namespace gives a process its own view of a system resource. For example, a PID namespace controls which processes show up in ps, and a network namespace gives the processes a view of network interfaces and ports. Two containers using separate network namespaces can both listen on port 80. Their listeners belong to different views of the network. This is the reason to ask which process can see this port? before changing a port number.
What can a process see inside a container?
The container gets its own view of processes and networking. Its system calls still go to the host kernel.
On the host
- Container runtimeLinux host kernelcreate namespaces
The host kernel runs processes. A container runtime asks that kernel to create the isolation needed for the configured executable.
Inside the container
- Container A: PID 1Shared kernelsystem calls
- Container B: PID 1Shared kernelsystem calls
Process A sees its own PID and mount namespaces. Another process can see different IDs and filesystem roots while still calling the same kernel.
After the process stops
- A: executable exited
- B: still runningShared kernelsystem calls
When A exits, its container stops. B can keep running; no guest operating system needs to be shut down.
Two Linux containers share one Linux host; sandboxed runtimes are outside this diagram.
Explore another case
What the container can see and use
A control group, or cgroup, groups processes so the kernel can track and limit resources they use. In this example, --memory 64m sets a memory limit and --cpus 0.5 gives the container up to about half a CPU of execution time under the runtime’s quota. The scheduler may run that work on different cores. Use ps to see the process view and docker stats to read current usage. One observation answers what is visible? The other answers what is being consumed?
# The foreground executable is a shell running a finite command.
docker run --name process-demo --memory 64m --cpus 0.5 \
alpine:3.20 sh -c 'echo "container PID: $$"; sleep 300'
# In another terminal, inspect that process from inside its boundary.
docker exec process-demo ps
# Resource reporting comes from runtime/host accounting.
docker stats --no-stream process-demo
# Stop the demo and remove its container metadata and writable layer.
docker stop process-demo
docker rm process-demoThe first command stays in the foreground because it starts the container without -d. Leave it running while you use a second terminal for docker exec and docker stats. The printed PID comes from inside the container. The same process can have a different PID in the host’s view. When sleep finishes or you stop the container, the main process exits and Docker reports the container as stopped.
A private view and a resource budget
Namespaces change what a process can see. Control groups track and constrain what it can consume.
Allocate memory
- Isolated processHost memoryallocate
A PID namespace changes visible process IDs. It does not impose a memory budget.
Apply a memory boundary
- Process in cgroupmemory.max: 67108864account usage
- memory.max: 67108864Host memoryconstrain
The runtime places the process in a cgroup. Memory usage is accounted against the configured 64 MiB maximum.
Inspect both mechanisms
- Private PID viewBounded consumptionresource accounting
- Private PID viewSame Linux kernelsystem calls
The process has a private view and bounded consumption. Neither creates separate physical memory or a separate kernel.
A Linux node uses cgroup v2 memory.max; reclaim, temporary limit overshoot, and exact OOM victim selection are simplified.
Explore another case
Try writing a file inside the container
An image supplies the starting files. Docker adds a writable layer to each container. If a process writes /tmp/report.txt there, stopping and starting that same container normally keeps the file. Removing the container deletes that writable layer. A new container made from the same image starts with a fresh layer. To keep a file beyond removal, put it on storage with its own lifetime, such as a named Docker volume.
What survives when the container is removed
When a container stops immediately, first check what its main command did. A program that prints a report and exits with code 0 may have finished exactly as intended. A service is different because people expect it to stay available for new requests. For that case, inspect the exit code and logs. The module lab lets you compare a short-lived Docker process with a web server running in a Kubernetes Pod.
Check your understanding
Both containers use port 80. What decides whether they can run at the same time?
With separate network namespaces, each can bind port 80 inside its own network. If you also publish both on the same host IP, port, and protocol, the second publication conflicts on the host. The port number is meaningful only within its network boundary.
Primary documentation
Go to the source
Reviewed 2026-10-27 · Kubernetes 1.37 · Related local exercise