Challenge, Medium,  on  Containers

Some Docker commands resemble typical Linux process management operations, while others sound more like operations on a filesystem (details). For example: docker run / docker stop (process), and docker create / docker rm (filesystem). Containers aren't just processes - they're processes running in isolated environments with their own root filesystem. As a result, changes inside a container may persist across stops and restarts until the container is removed.

Container rootfs is prepared from read-only image layers and an extra copy-on-write layer to persist any changes done by the containerized application.

In this challenge, you will practice stopping and restarting containers and exploring their filesystem.

The target application is an improvised personal finance tracker. Its implementation is intentionally simple - a single web page backed by a Python Flask application that keeps its data in an append-only file under the /var/lib/app directory in the container.

The container is already running. Click here to access its web interface.

First, add one or more records to the application:

To verify that the records have been written to the container's filesystem, execute the following command in the container:

cat /var/lib/app/ledger.txt

Now, let's stop the container. You may want to do that to free up some system resources, or prepare the server for maintenance, or gracefully stop the application before restarting or shutting down the host.

Hint 1: Stopping containers gracefully 💡

Use the docker stop command to gracefully stop a running container:

docker stop CONTAINER_NAME_OR_ID

This sends a signal specified in the Dockerfile's STOPSIGNAL instruction (or the default SIGTERM) to the main process of the container, allowing it to clean up resources before terminating. If the process lacks the graceful termination mechanism or takes too long to exit (10s by default), Docker will send a SIGKILL signal to forcefully stop it.

The tracker's web interface should become unresponsive, and you won't be able to list the existing or add new records.

Now, bring the container back up by starting it again:

Hint 2: Restarting stopped containers 💡

Use the docker start command to restart a stopped container:

docker start CONTAINER_NAME_OR_ID

This preserves all filesystem changes made during the container's previous execution(s). The container maintains its identity and all data stored within its writable layer.

Verify that the records have been preserved by reloading the web interface of the tracker.

Finally, remove the container and then start a new one from the same image:

Hint 3: Removing containers permanently 💡

Use the docker rm command to permanently remove a container - make sure to stop the container first if it's still running:

docker rm CONTAINER_NAME_OR_ID

⚠️ The docker rm command permanently deletes the container and all data stored within its filesystem (excluding data stored in volumes, but it's out of scope for this challenge).

Now, start a new container from the same image, using the following command:

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

Have your previous financial records been preserved over a stop/remove/start cycle?

Level up your Server Side game — Join 12,000 engineers who receive insightful learning materials straight to their inbox