An open API service indexing awesome lists of open source software.

https://github.com/kartikk-26/docker-container-lifecycle

This repository contains a hands-on Docker 101 puzzle designed to teach beginners how Docker containers behave like both processes and isolated filesystems.
https://github.com/kartikk-26/docker-container-lifecycle

devops-tools docker docker-compose docker-container

Last synced: 13 days ago
JSON representation

This repository contains a hands-on Docker 101 puzzle designed to teach beginners how Docker containers behave like both processes and isolated filesystems.

Awesome Lists containing this project

README

          

---

# 🐳 Docker Container Lifecycle Puzzle Guide

## πŸ”— Go and Try This Puzzle

To experience this challenge hands-on, visit the official Docker 101 puzzle:

πŸ‘‰ [Docker 101: Stop and Restart a Containerized Application](https://labs.iximiuz.com/challenges/docker-101-container-stop-and-restart?s=08)

This interactive lab allows you to practice stopping and restarting containers while exploring their filesystem.

---

## 🧩 What Is This Puzzle About?

This puzzle helps you understand **data persistence in Docker containers**.

* **Containers = processes**: You can start, stop, and restart them like regular programs.
* **Containers = mini-filesystems**: Each has its own isolated root filesystem, created from the Docker image plus a writable layer.

πŸ‘‰ **Key idea**: Stopping a container keeps data; removing a container deletes it (unless you use volumes).

---

## πŸ› οΈ What You’ll Learn

* How to stop and restart containers.
* How to inspect and modify a container’s filesystem.
* The difference between stopping and removing containers.
* How data persists across stops and starts but is lost when containers are removed.

---

## πŸ“ Step-by-Step Solution

### Step A: Add Some Records

1. Open the web interface of the finance tracker.
2. Add records like `Income 1000` or `Expense 200`.
3. Verify inside the container:

```bash
docker exec -it CONTAINER_NAME_OR_ID cat /var/lib/app/ledger.txt
```

You should see your records.

### Step B: Stop the Container

Run:

```bash
docker stop CONTAINER_NAME_OR_ID
```

* Sends a `SIGTERM` for graceful shutdown.
* Docker forces `SIGKILL` if it takes too long.
* The web app becomes unresponsive.

### Step C: Restart the Container

Run:

```bash
docker start CONTAINER_NAME_OR_ID
```

* Container resumes with the same writable layer.
* Reload the web interface β†’ **records are still there** πŸŽ‰

### Step D: Remove the Container

First, stop it (if still running):

```bash
docker stop CONTAINER_NAME_OR_ID
```

Then remove it:

```bash
docker rm CONTAINER_NAME_OR_ID
```

* Container and writable layer are deleted.
* Data inside `/var/lib/app/ledger.txt` **is gone**.

### Step E: Start a New Container from the Same Image

Run:

```bash
docker run -d --name my-finance -p 8080:8080 registry.iximiuz.com/my-finance
```

* Launches a fresh container.
* Open the web interface β†’ **no old records exist**.

---

## βœ… Final Outcome

* **Stop β†’ Start**: Data is preserved βœ…
* **Remove β†’ Run new**: Data is lost ❌

This demonstrates the difference between:

* Container lifecycle (stop/start keeps data)
* Container removal (removes data)

---

## 🧠 Beginner-Friendly Summary

* A container = running program + mini computer with its own filesystem.
* Stop a container β†’ nothing is lost.
* Remove a container β†’ everything inside is deleted.
* New container from same image = fresh start.

**Goal**: Show that stopping keeps data βœ…, removing destroys data ❌

**One-liner**: Stopping a container is like putting your laptop to sleep; removing it is like throwing it away and buying a new one.

---

## πŸ–ΌοΈ Visuals

### Container Filesystem Diagram

![Container Filesystem Diagram](./Assests/container-rootfs-image.png)

*Illustration of Docker's layered filesystem, showing the read-only image layers and the writable container layer.*

### Expected Output Screenshot

![Expected Output Screenshot](./Assests/WhatsApp%20Image%202025-09-10%20at%2008.42.54_b7183fd7.jpg)

*Example of the terminal output after inspecting the container's filesystem.*

---

Feel free to explore the challenge and experiment with the steps to deepen your understanding of Docker container lifecycles and data persistence.