Docker Container Lifecycle and Inspection - Part 1: Managing Running Containers

Master Docker container lifecycle management, from creating and running containers to executing commands and inspecting container details. Learn container states, exec commands, and inspection techniques.

11 min read

Understanding the complete lifecycle of Docker containers is essential for effective container management. This comprehensive guide covers everything from creating and running containers to inspecting their state and executing commands within them.

💡

🎯 What You'll Learn: In this hands-on tutorial, you'll discover:

  • Docker system information and container states
  • Creating long-running and short-lived containers
  • Executing commands inside running containers
  • Comprehensive container inspection techniques
  • Container networking and configuration details
  • Managing container lifecycle from creation to removal

🚀 Understanding Docker System State

Before working with containers, let's examine our Docker installation and current system state.

Prerequisites

Before we begin, make sure you have:

  • Docker installed and running on your system
  • Basic understanding of Docker commands
  • Access to terminal with Docker privileges

Checking Docker Version and System Information

docker --version

Output:

Docker version 28.3.3, build 980b856

This shows we're running Docker version 28.3.3, which includes the latest features and security updates.

Comprehensive System Information

docker info

This command provides detailed information about your Docker installation:

SectionKey InformationExample Values
Client InfoDocker CLI version and pluginsbuildx, compose plugins
Server InfoRunning containers and images0 containers, 0 images
StorageStorage driver and filesystemoverlay2 on xfs
System ResourcesCPU, memory, and architecture4 CPUs, 7.501GiB memory, x86_64

📦 Creating and Managing Containers

Long-Running Container

Let's create a container that stays running for an extended period:

docker run -d --name my-ubuntu-container ubuntu sleep 3600

Output:

Unable to find image 'ubuntu:latest' locally
latest: Pulling from library/ubuntu
76249c7cd503: Pull complete 
Digest: sha256:9cbed754112939e914291337b5e554b07ad7c392491dba6daf25eef1332a22e8
Status: Downloaded newer image for ubuntu:latest
72bcfed2c3a9631d3b752e118868496e6fc34d8ee55f5bfb01eee2894356b78c

This command:

  • -d: Runs container in detached mode (background)
  • --name my-ubuntu-container: Assigns a custom name
  • ubuntu: Uses Ubuntu image as base
  • sleep 3600: Keeps container running for 1 hour

Verifying Running Container

docker ps

Output:

CONTAINER ID   IMAGE     COMMAND        CREATED         STATUS         PORTS     NAMES
72bcfed2c3a9   ubuntu    "sleep 3600"   6 seconds ago   Up 5 seconds             my-ubuntu-container
ColumnDescription
CONTAINER IDUnique identifier (shortened)
IMAGEBase image used
COMMANDCommand being executed
CREATEDWhen container was created
STATUSCurrent state and uptime

Short-Lived Container

Now let's create a container that exits immediately:

docker run -d --name short-lived-ubuntu ubuntu

Output:

41ffeef865a4ecbc1960ad413a674a27356516d84e13fdd54a15e219a2aa55ca

Examining All Containers

docker ps -a

Output:

CONTAINER ID   IMAGE     COMMAND        CREATED          STATUS                     PORTS     NAMES
41ffeef865a4   ubuntu    "/bin/bash"    3 seconds ago    Exited (0) 2 seconds ago             short-lived-ubuntu
72bcfed2c3a9   ubuntu    "sleep 3600"   32 seconds ago   Up 31 seconds                        my-ubuntu-container

Notice the difference:

  • my-ubuntu-container: Still running because sleep 3600 keeps it active
  • short-lived-ubuntu: Exited immediately because /bin/bash had no input to process
💡

💡 Container Lifecycle: Containers only run as long as their main process is active. Without a long-running command, containers exit immediately after startup.

🔧 Executing Commands in Running Containers

Interactive Shell Access

Access the running container with an interactive shell:

docker exec -it my-ubuntu-container /bin/bash

Once inside the container, you can run various commands:

cat /etc/os-release
ls -al
ps aux
echo "Hello from inside the container" > /tmp/test.txt
cat /tmp/test.txt
exit

Sample output from inside the container:

PRETTY_NAME="Ubuntu 24.04.3 LTS"
NAME="Ubuntu"
VERSION_ID="24.04"
VERSION="24.04.3 LTS (Noble Numbat)"
VERSION_CODENAME=noble
total 0
drwxr-xr-x.   1 root root   6 Sep 10 20:50 .
drwxr-xr-x.   1 root root   6 Sep 10 20:50 ..
-rwxr-xr-x.   1 root root   0 Sep 10 20:50 .dockerenv
lrwxrwxrwx.   1 root root   7 Apr 22  2024 bin -> usr/bin
drwxr-xr-x.   2 root root   6 Apr 22  2024 boot
drwxr-xr-x.   5 root root 340 Sep 10 20:50 dev
# ... additional directories
USER         PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root           1  0.0  0.0   2696  1524 ?        Ss   20:50   0:00 sleep 3600
root           7  0.4  0.0   4588  4100 pts/0    Ss   20:52   0:00 /bin/bash
root          17  0.0  0.0   7888  4168 pts/0    R+   20:52   0:00 ps aux

Key observations:

  • .dockerenv: Special file indicating we're inside a container
  • PID 1: The sleep 3600 command is the main process
  • Isolated environment: Container has its own process space and filesystem

Non-Interactive Command Execution

Execute commands without entering the container:

docker exec my-ubuntu-container hostname
docker exec my-ubuntu-container ls -al /tmp
docker exec my-ubuntu-container cat /tmp/test.txt

Output:

# hostname command
72bcfed2c3a9

# ls -al /tmp command
total 4
drwxrwxrwt. 1 root root 22 Sep 10 20:52 .
drwxr-xr-x. 1 root root 29 Sep 10 20:50 ..
-rw-r--r--. 1 root root 32 Sep 10 20:52 test.txt

# cat /tmp/test.txt command
Hello from inside the container

🔍 Container Inspection and Analysis

Basic Container Inspection

Get comprehensive information about a container:

docker inspect my-ubuntu-container

This returns extensive JSON data including:

  • Container ID and creation time
  • Current state and process information
  • Network configuration
  • Storage driver details
  • Resource constraints and capabilities

Targeted Information Extraction

Extract specific information using format templates:

docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' my-ubuntu-container
docker inspect --format="{{.State.Status}}" my-ubuntu-container
docker inspect --format="{{.Config.Image}}" my-ubuntu-container
docker inspect --format="{{.Created}}" my-ubuntu-container

Output:

# IP Address
172.17.0.2

# Container Status
running

# Base Image
ubuntu

# Creation Time
2025-09-10T20:50:50.752690623Z
⚠️

⚠️ Template Syntax: Note the difference between .NetworkSettings.Network (incorrect) and .NetworkSettings.Networks (correct). The first will cause a template parsing error.

Inspecting Multiple Containers

You can inspect multiple containers simultaneously:

docker inspect my-ubuntu-container short-lived-ubuntu

This returns an array with detailed information for both containers, useful for comparing configurations or states.

📊 Container Networking Details

From the inspection output, we can see important networking information:

Network PropertyValueDescription
IPAddress172.17.0.2Container's IP on Docker bridge network
Gateway172.17.0.1Docker bridge gateway address
MacAddress5a:f9:49:a3:0e:66Container's MAC address
NetworkModebridgeUses Docker's default bridge network

🛑 Container Lifecycle Management

Stopping Containers

Stop the running container gracefully:

docker stop my-ubuntu-container

Output:

my-ubuntu-container

Check the updated status:

docker ps -a

Output:

CONTAINER ID   IMAGE     COMMAND        CREATED         STATUS                       PORTS     NAMES
41ffeef865a4   ubuntu    "/bin/bash"    6 minutes ago   Exited (0) 6 minutes ago               short-lived-ubuntu
72bcfed2c3a9   ubuntu    "sleep 3600"   7 minutes ago   Exited (137) 6 seconds ago             my-ubuntu-container

Notice the status changed to Exited (137) - the exit code 137 indicates the process was terminated by SIGKILL.

Removing Containers

Remove stopped containers:

docker rm my-ubuntu-container

Output:

my-ubuntu-container

Verify removal:

docker ps -a

Output:

CONTAINER ID   IMAGE     COMMAND       CREATED         STATUS                     PORTS     NAMES
41ffeef865a4   ubuntu    "/bin/bash"   7 minutes ago   Exited (0) 7 minutes ago             short-lived-ubuntu

Force Remove Running Containers

You can also remove containers forcefully without stopping them first:

docker run -d --name test-container ubuntu sleep 3600
docker rm -f test-container

Output:

94f2b80ee952d0333b72584184afd53ef2e3a280f391a2f39990f5faff8c5cfa
test-container

The -f flag forcefully stops and removes the container in one command.

Bulk Container Operations

Manage multiple containers simultaneously:

# Create multiple containers
docker run -d --name container1 ubuntu sleep 300
docker run -d --name container2 ubuntu sleep 300
docker run -d --name container3 ubuntu sleep 300

# Remove all at once
docker rm -f container1 container2 container3

Output:

4b4e81f4983f25afee26003ae12dcf148ecee2df807e5bdbd15366c315a63b9f
46010b1a537590305f425080b7ed81389f9f20b028a4f52efdb90a1d32378cc0
5cd48a0ac7016c33ec4f28a153d9c871ead90e279f208ae278924ed8086e7563
container1
container2
container3

🎯 Key Container States

Understanding container states is crucial for effective management:

StateDescriptionExample Status
RunningContainer is actively executingUp 5 seconds
ExitedContainer has stoppedExited (0) 2 seconds ago
PausedContainer processes are suspendedUp 1 minute (Paused)
RestartingContainer is being restartedRestarting (1) 5 seconds ago

💡 Best Practices for Container Management

Naming Conventions

Always use descriptive names for containers:

# Good: Descriptive and purposeful
docker run -d --name web-server nginx
docker run -d --name database-mysql mysql:8.0
docker run -d --name cache-redis redis:alpine

# Avoid: Generic or meaningless names
docker run -d --name container1 nginx
docker run -d --name test ubuntu

Resource Management

Monitor and limit container resources:

# Check container resource usage
docker stats

# Run with resource limits
docker run -d --name limited-container --memory=512m --cpus=1.0 ubuntu sleep 3600

Log Management

Access container logs for debugging:

# View container logs
docker logs my-ubuntu-container

# Follow logs in real-time
docker logs -f my-ubuntu-container

# Show only recent logs
docker logs --tail 50 my-ubuntu-container

🎯 Key Takeaways

✅ Remember These Points

  1. Container Lifecycle: Containers run only as long as their main process is active
  2. Exec vs Run: Use docker exec for running commands in existing containers
  3. Inspection Tools: docker inspect provides comprehensive container information
  4. State Management: Understand different container states and transitions
  5. Naming Strategy: Use descriptive names for better container organization

📖 Further Reading

Official Resources


🎉 Excellent Progress! You now understand the fundamentals of Docker container lifecycle management, from creation to inspection and removal. These skills form the foundation for advanced container orchestration and management.

💬 Discussion

I'd love to hear about your container management experiences:

  • What naming conventions do you use for your containers?
  • Have you encountered any challenging debugging scenarios?
  • How do you handle container logs and monitoring?
  • What container lifecycle patterns work best for your projects?

Connect with me:

  • 🐙 GitHub - Container management scripts and examples
  • 📧 Contact - Container architecture questions and discussions

Thank you for reading!

Published on September 11, 2025

O

Written by Owais

Passionate developer sharing insights on technology, development, and the art of building great software.

Related Articles

Continue exploring with these handpicked articles that complement what you just read

More Reading

One more article you might find interesting