Volumes

By default, Docker containers don’t retain any state from past containers. For example, if you:

  1. Start a container from an image
  2. Make some changes to the filesystem (like installing a new package) in that container
  3. Stop the container
  4. Start a new container from the same image

The new container does not have the changes you made in step 2. The new container starts from scratch again with a copy of the original image. All stateful changes are lost.

(Note: If you restart the stopped container, it will have the changes. But typically, containers are meant to be ephemeral—destroyed and recreated at will.)

Persistent State with Volumes

Docker supports “persistent state” through storage volumes. A volume is basically a filesystem that lives outside of the container but can be accessed by the container. If a container uses a volume, then stateful changes can be persisted to the volume even if the container is deleted.

Creating a Volume

You can create a new named volume using the docker volume create command:

docker volume create my-vol

To list all your volumes and make sure it worked:

docker volume ls

To see detailed information about the volume, including where it is stored on your local machine:

docker volume inspect my-vol