Knowledge
Exercise: the container lifecycle and image immutability
Estimated time: 40 minutes.
On a Linux host (for example Ubuntu 24.04), install Docker yourself, run an Nginx container, practice the entire container lifecycle, then prove that images are immutable with a hot-edit → delete → recreate experiment. Finally, observe logs, resource usage and events.
Acceptance criteria: (1) Nginx is reachable at http://<ip>:8080; (2) after hot-editing index.html, deleting the container and running it again from the same image, the content goes back to the default; (3) you can read the container's logs in real time and capture the stop/start events with docker events.
Step 1 — Check the machine first, then install Docker
Build the habit of "checking the facts before you act": look at the IP, hostname and operating system so you pick the right install commands. Install Docker Engine, then verify it.
hostnamectl
cat /etc/os-release
ip -brief a
# install Docker Engine following the official documentation, then:
docker version
docker info
docker run --rm hello-world
Note: If you get permission denied when calling the Docker daemon, add your user to the docker group (sudo usermod -aG docker $USER) and log in again, or run with sudo (Docker Engine — install on Ubuntu).
Step 2 — Manage images
Pull the Nginx image, inspect its metadata and layer history, and set a tag following your internal naming convention. Observe the digest (hash), which guarantees integrity.
docker pull nginx
docker image ls
docker image inspect --format "{{.Os}} {{.Architecture}}" nginx
docker image history nginx
docker image tag nginx lab.inet/nginx:1
Note: If you do not specify a tag, it defaults to latest — in real environments, pin a specific version (docker image).
Step 3 — Run Nginx and publish a port
Run Nginx in the background, with automatic restart, mapping host port 8080 to container port 80. Try accessing it from another machine through the server's IP.
docker run -d --name docker-lab --hostname docker-lab --restart always -p 8080:80 nginx
docker ps
docker container port docker-lab
curl -I http://localhost:8080
Note: -p 8080:80 publishes on 0.0.0.0 (all interfaces); use -p 127.0.0.1:8080:80 if you only want access from the host itself (docker run).
Step 4 — Practice the lifecycle
Practice in order: stop → check the state → start → pause/unpause → rename. Verify each step with docker ps -a.
docker container stop docker-lab
docker ps -a # STATUS = Exited
docker container start docker-lab
docker container pause docker-lab
docker container unpause docker-lab
docker container rename docker-lab web1
Note: stop only moves the container to Exited (it still exists); you must rm it to delete it completely (docker container).
Step 5 — Access the inside of a container
Look from the outside with top (you do not see host processes), then get inside with exec to compare the hostname/IP with the host's.
docker container top web1
docker exec -it web1 /bin/sh
# inside the container:
hostname
hostname -i # the container's IP
cat /etc/hosts # last line: the container's IP + hostname
exit
# from the host, compare with the host's IP (hostname -I):
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' web1
hostname -I
Note: top lists only the processes inside the container — evidence of PID namespace isolation. The official nginx image does not ship the ip command (typing ip a reports not found), so read the IP with hostname -i/cat /etc/hosts inside the container, or with docker inspect from the host (docker inspect).
Step 6 — Prove immutability (the core of the exercise)
Hot-edit the home page and reload to see the change; then delete the container and run it again from the SAME image → the content goes back to the default.
docker exec -it web1 sh -c 'echo "Hello from hoc-vien" > /usr/share/nginx/html/index.html'
curl http://localhost:8080 # shows the content you just edited
docker container stop web1 && docker container rm web1
docker run -d --name web1 -p 8080:80 nginx
curl http://localhost:8080 # back to "Welcome to nginx!"
Note: The hot-edit lives in the writable layer, which is deleted along with the container; running again from the same (read-only) image gives the default content → the image is immutable. To make a change permanent: edit the source and build a new image (Part 2). State that must persist is written to a volume (Docker: volumes).
Step 7 — Logs / stats / events
Open 2 terminals: one follows the logs, the other generates load; then capture lifecycle events with docker events.
# terminal A
docker container logs -f web1
# terminal B
for i in $(seq 1 50); do curl -s localhost:8080/req-$i >/dev/null; done
docker stats --no-stream
# terminal A switches to: docker events --filter container=web1
# terminal B: docker restart web1 ; docker stop web1 ; docker start web1
Note: About 50 log lines correspond to the 50 requests; events emits kill/die/start/restart/stop events. events are not stored long term — you need an external tool (Vector/VictoriaLogs) to collect them and raise alerts (docker logs, docker events).
Illustration 1 — hello-world
Run hello-world to confirm Docker works; Docker pulls the image automatically when it is not present.
docker run --rm hello-world
Result:
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
Digest: sha256:...
Hello from Docker!
This message shows that your installation appears to be working correctly.
Note: With --rm, the container is deleted automatically once it finishes (check that docker ps -a does not show it).
Illustration 2 — Run Nginx and publish a port
The PORTS column confirms the port is published.
docker run -d --name docker-lab -p 8080:80 nginx
docker ps
Result:
CONTAINER ID IMAGE PORTS NAMES
a1b2c3d4e5f6 nginx 0.0.0.0:8080->80/tcp docker-lab
Note: Seeing 0.0.0.0:8080->80/tcp means the port is published on all interfaces thanks to -p.
Illustration 3 — docker container top
docker container top shows only the processes inside the container, with no systemd/init of the host.
docker container top docker-lab
Result:
UID PID PPID CMD
root 2811 2789 nginx: master process nginx -g daemon off;
nginx 2864 2811 nginx: worker process
Note: There are no host management processes — evidence of PID namespace isolation.
Illustration 4 — Proving immutability
Hot-edit, then recreate from the same image, and the content goes back to the default.
docker exec docker-lab sh -c 'echo "Hello" > /usr/share/nginx/html/index.html'
curl -s localhost:8080
docker rm -f docker-lab
docker run -d --name docker-lab -p 8080:80 nginx
curl -s localhost:8080
Result:
Hello
...
<title>Welcome to nginx!</title>
Note: The first curl returns "Hello" (the hot-edit); after recreating from the same image, the page goes back to the default "Welcome to nginx!".
Illustration 5 — Real-time logs and generating load
The number of log lines grows in step with the number of requests.
# terminal A: docker container logs -f docker-lab
# terminal B:
for i in $(seq 1 50); do curl -s localhost:8080/req-$i >/dev/null; done
Result:
172.17.0.1 - - [18/Jul/2026:09:12:01 +0000] "GET /req-1 HTTP/1.1" 404 153
172.17.0.1 - - [18/Jul/2026:09:12:01 +0000] "GET /req-2 HTTP/1.1" 404 153
... (all 50 lines, matching the 50 requests)
Note: Container log = stdout/stderr of the main process; this differs from the app's own logs in /var/log/nginx/.
Illustration 6 — docker events
docker events captures lifecycle events for each operation.
# terminal A: docker events --filter container=docker-lab
# terminal B: docker restart docker-lab
Result:
container kill docker-lab (signal=15)
container die docker-lab
container start docker-lab
container restart docker-lab
Note: A container that restarts or stops unexpectedly is a sign the app has a problem — monitor it so you can raise alerts.