Restart with fresh image upon failure/stop of service

In my docker-compose file, I’ve set restart: "always" in order to restart the service if the main program should ever fail. I was expecting that this would mean a clean image (all persistent data was outsourced to volumes or bind mounts). Alas, this is not the case, changes to the main filesystem persist across restarts.

IMHO, this contradicts the main Docker philosophy that fresh containers from an image are cheap and that they should be stateless. Am I wrong here, or is there an option I have overlooked to get the behaviour I am looking for?

A simple example:


version: '3'
    image: "debian:latest"
    restart: "always"
    command: /bin/sleep 120
  1. Start with docker-compose up
  2. Log into container with docker exec -ti compose_test_deb_1 /bin/bash
  3. Create footprint with date > AAA, then log out.
  4. Wait for the remaining time, after 2 minutes, log in again.
  5. See if footprint still exists: cat AAA