What is a Docker Container?
A Docker Container is a running instance of a Docker image. It is a lightweight, isolated, and portable unit that contains everything needed to run an application — code, runtime, libraries, and settings.
Think of an image as a class and a container as an object created from that class.
Container Lifecycle
| State | Description |
|---|---|
| Created | Container is created but not started |
| Running | Container is actively running |
| Paused | Container is temporarily paused |
| Stopped | Container is stopped but still exists |
| Deleted | Container is permanently removed |
Essential Container Commands
| Command | Description |
|---|---|
docker run <image> |
Create and start a container |
docker ps |
List running containers |
docker ps -a |
List all containers (including stopped) |
docker stop <container> |
Stop a running container |
docker start <container> |
Start a stopped container |
docker restart <container> |
Restart a container |
docker rm <container> |
Remove a stopped container |
docker exec -it <container> bash |
Open shell inside running container |
docker logs <container> |
View container logs |
Example 1: Run a Simple Container
# Run nginx container in background (-d = detached mode)
docker run -d --name my-nginx -p 8080:80 nginx
Now open http://localhost:8080 in your browser — Nginx is running inside a container!
Example 2: Run Interactive Container
# Run ubuntu and open bash shell
docker run -it --name my-ubuntu ubuntu bash
Inside the container you can run Linux commands:
ls
pwd
apt-get update
exitExample 3: List and Inspect Containers
# List running containers
docker ps
# List all containers
docker ps -a
# Inspect container details
docker inspect my-nginx
Example 4: Stop, Start, Remove Container
# Stop container
docker stop my-nginx
# Start stopped container again
docker start my-nginx
# Remove container (must be stopped first)
docker rm my-nginx
# Force remove running container
docker rm -f my-nginx
Example 5: Copy Files To/From Container
# Copy file from host to container
docker cp index.html my-nginx:/usr/share/nginx/html/
# Copy file from container to host
docker cp my-nginx:/usr/share/nginx/html/index.html ./
Run Flags Reference
| Flag | Meaning |
|---|---|
-d |
Detached (run in background) |
-it |
Interactive terminal |
--name |
Give container a custom name |
-p host:container |
Port mapping |
--rm |
Auto-remove container after it exits |
-e KEY=VALUE |
Set environment variable |
Summary
- A container is a running instance of a Docker image.
- Use
docker runto start containers anddocker psto list them. - Use
-dfor background mode and-pto map ports. - Use
docker exec -itto enter a running container shell.
Your Feedback
Help us improve by sharing your thoughts
Online Learner helps developers master programming, database concepts, interview preparation, and real-world implementation through structured learning paths.
Quick Links
© 2023 - 2026 OnlineLearner.in | All Rights Reserved.
