Built run commands by developers, for developers.
This repository provides a clear and practical explanation of how docker run works and what actually happens behind the scenes when a container starts.
The objective is to build a solid mental model of container execution. Instead of focusing on advanced features, this repository concentrates on fundamentals:
- How Docker transforms an image into a running container
- What steps are executed internally when docker run is triggered
- How container lifecycle is directly tied to the main process (PID 1)
- How to inspect, stop, and remove containers properly
Understanding docker run is essential because it represents the bridge between static images and running workloads. Every higher-level abstraction in Docker (Compose, Swarm) or even Kubernetes ultimately depends on this core container execution model.
The examples in this repository are intentionally minimal to emphasize clarity and behavior over complexity.
- Overview
- What
docker runDoes - Running a Container
- Foreground vs Detached Mode
- Listing Running Containers
- Stopping a Container
- Removing a Container
- Key Concepts
- Container Lifecycle
This repository explains what happens when you execute the docker run command. The goal is to understand how Docker creates and starts containers from images, and how to manage their lifecycle using basic commands.
The docker run command performs multiple actions in a single step:
- Checks if the image exists locally.
- Pulls the image from a registry if it does not exist.
- Creates a new container from the image.
- Starts the container.
- Attaches the container’s output to your terminal (by default).
Basic syntax: docker run <image>
Example using the official Nginx image:
docker run nginxIf the image is not available locally, Docker will download it automatically. After the container starts, it runs the main process defined by the image.
In this case, Nginx starts and runs in the foreground.
By default, containers run in the foreground. Your terminal is attached to the container’s output.
To run a container in detached mode (background):
docker run -d nginxThe -d flag tells Docker to run the container in the background and return control to your terminal immediately.
To see active containers:
docker psThis shows:
- Container ID
- Image used
- Command executed
- Creation time
- Status
- Exposed ports
- Container name
To see all containers (including stopped ones):
docker ps -aTo stop a running container:
docker stop <container_id>You can use either the full container ID or the container name.
After a container is stopped, it can be removed:
docker rm <container_id>To automatically remove a container after it stops:
docker run --rm nginxThe --rm flag ensures the container is deleted once it exits.
- An image is a blueprint.
- A container is a running instance of an image.
- docker run creates and starts a container.
- Containers are isolated processes.
- Detached mode allows containers to run in the background.
- Containers are ephemeral unless explicitly preserved.
When you run:
docker run nginxThe lifecycle is:
- Verifies if the image exists locally.
- Pulls the image if it is not available.
- Creates a new container from the image.
- Allocates a writable layer for the container.
- Starts the container’s main process (PID 1).
- Attaches the container output to your terminal (unless -d is used).
- Keeps the container running while the main process is alive.
- Stops the container when the main process exits.
