Home Docker states - Stateless and Stateful
Post
Cancel

Docker states - Stateless and Stateful

Both Docker and Kubernetes have the concept of Stateless and Stateful.

  • Stateless - When the container is removed, all of the data within the container is also removed. Good for strictly ‘compute’ type containers. Data gets passed in and then passed out via network ports or using an API.
  • Stateful - Some of the data within the container is actually stored on the host filesystem. When the container ends, that data is maintained on the host file systems. For a database application, the database application (like MySQL) can be kept in the container, but the database files themselves are stored on the host filesystem.

Stateless application

1
2
3
docker run -it --rm -d -p 8080:80 --name web nginx
sleep 1
curl http://127.0.0.1:8080

This container (‘web’) will always show the “Welcome to nginx” message because the folder called the docroot or webroot will always be the same every time you recreate the container.

Stateful application

1
2
3
4
5
mkdir webroot
echo "<h1>Hello</h1>" > webroot/hello.html
docker run -it --rm -d -p 8080:80 --name web -v $(pwd)/webroot:/usr/share/nginx/html/ nginx
sleep 1
curl http://127.0.0.1:8080/hello.html

Now when you recreate this container, all of the contents of the ./webroot/ folder will be mapped into the container at /usr/share/nginx/html/ which is the webroot of a default nginx.

This post is licensed under CC BY 4.0 by the author.