Master Docker Container Lifecycle in DevOps: 13 Practical Steps

Master Docker Container
Master Docker Container

What is a Docker Container ?

The Docker container lifecycle is a core concept in DevOps, covering how containers are created, accessed, started, stopped, and removed.

A Docker container is a lightweight environment that contains your application along with everything it needs to run (libraries, software, configuration).

For example, if your app requires Python, a MySQL client, and some libraries, all of that can be packaged inside a container. When you run it, your app just works no additional setup required.

How Does a Container Work ?

Containers run on the Docker Engine, which is the software responsible for managing them.

  • You can run multiple containers on the same system simultaneously.
  • Each container is isolated, meaning one doesn’t interfere with another.

Benefits of Containers

  • Lightweight —> Smaller and faster than Virtual Machines.
  • Portable —> Run anywhere (Laptop, Server, Cloud, windows, Liunx, Mac) with the same results.
  • Fast Startup —> Starts in seconds.
  • Isolated —> Each container runs in its own separate environment.

Practical Example: Docker Container Lifecycle

Below is a step-by-step walkthrough of managing containers in Docker.

1. Check Containers (Running / Stopped / All)

Show only running containers:

docker container ls

Show all containers (running + stopped):

docker container ls -a

2. Create a Container with Sleep Command

Run a container in the background for 60 seconds:

docker container run -d ubuntu sleep 60

Check running or stopped containers:

docker container ls -a

3. Create Container from Latest Ubuntu Image

Start an interactive Ubuntu container with bash access:

docker run -it ubuntu:latest bash

4. Create a Container with Console Access

Run a container and directly access its shell:

docker run -it --name ubuntutest ubuntu bash

5. Exit Container (and Stop It)

From inside the container:

exit

6. Stop a Running Container

Stop a container by ID or name:

docker container stop ubuntutest

7. Start a Stopped Container

docker container start ubuntutest

8. Restart a Container

docker container restart ubuntutest

9. Attach to an Already Running Container

Enter an existing running container:

docker exec -it ubuntutest bash

10. Create a Detached Container

Run in background mode with terminal support:

docker container run -d -it ubuntu

11. Delete a Running Container

Force remove a running container:

docker container rm -f ubuntutest

12. Delete Multiple Containers

docker container rm -f container_id1 container_id2 container_id3

13. Delete Stopped Containers

docker container rm container_id

Command Breakdown

  • docker container run → Creates and starts a new container.
  • -d → Runs in detached (background) mode.
  • -it → Interactive terminal (useful for shell access).
  • ubuntu → Official Ubuntu image from Docker Hub.

Conclusion

Docker containers simplify development and operations by providing lightweight, portable, and isolated environments.

By mastering the container lifecycle — create, access, sleep, start, stop, restart, and remove — you gain full control over your applications in any environment.


Read more DevOps tutorials at LetsTalkAboutDevOps.com/

Leave a Reply