0
0
Dockerdevops~5 mins

Why understanding lifecycle matters in Docker - Why It Works

Choose your learning style9 modes available
Introduction
Docker containers have a lifecycle that includes creation, running, stopping, and removal. Understanding this lifecycle helps you manage containers efficiently and avoid common problems like leftover stopped containers or resource waste.
When you want to start a container to run an application and later stop it cleanly.
When you need to remove containers that are no longer needed to free up system resources.
When you want to check the status of containers to know which are running or stopped.
When you want to restart a container after making changes or fixing issues.
When you want to avoid clutter and keep your Docker environment clean and organized.
Commands
This command creates and starts a new container named 'my-nginx' running the nginx web server version 1.23.3 in detached mode, mapping port 8080 on your computer to port 80 in the container.
Terminal
docker run -d --name my-nginx -p 8080:80 nginx:1.23.3
Expected OutputExpected
a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9t0u1v2w3x4y5z6a7b8c9d0e1f2
-d - Run container in background (detached mode)
--name - Assign a custom name to the container
-p - Map host port to container port
This command lists all currently running containers so you can see that 'my-nginx' is up and running.
Terminal
docker ps
Expected OutputExpected
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a1b2c3d4e5f6 nginx:1.23.3 "/docker-entrypoint.…" 10 seconds ago Up 9 seconds 0.0.0.0:8080->80/tcp my-nginx
This command stops the running 'my-nginx' container gracefully, allowing it to finish any ongoing tasks before shutting down.
Terminal
docker stop my-nginx
Expected OutputExpected
my-nginx
This command lists all containers, including stopped ones, so you can see that 'my-nginx' is now stopped but still exists on your system.
Terminal
docker ps -a
Expected OutputExpected
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a1b2c3d4e5f6 nginx:1.23.3 "/docker-entrypoint.…" 2 minutes ago Exited (0) 10 seconds ago my-nginx
-a - Show all containers, including stopped ones
This command removes the stopped 'my-nginx' container from your system to free up space and keep your environment clean.
Terminal
docker rm my-nginx
Expected OutputExpected
my-nginx
Key Concept

If you remember nothing else from this pattern, remember: managing container lifecycle steps—create, run, stop, and remove—keeps your Docker environment clean and efficient.

Common Mistakes
Trying to remove a running container without stopping it first.
Docker will refuse to remove a container that is still running, causing an error.
Always stop the container using 'docker stop' before running 'docker rm' to remove it.
Not using the --name flag and forgetting container IDs.
Without a custom name, you must use long container IDs which are hard to remember and prone to mistakes.
Use the --name flag to assign easy-to-remember names to containers for simpler management.
Leaving stopped containers without removing them.
Stopped containers still consume disk space and clutter your Docker environment.
Regularly remove stopped containers with 'docker rm' to keep your system clean.
Summary
Use 'docker run' to create and start a container with a specific name and port mapping.
Check running containers with 'docker ps' and all containers with 'docker ps -a'.
Stop containers gracefully with 'docker stop' before removing them with 'docker rm'.