Data-only containers are obsolete and are now considered an anti-pattern!
In the days of yore, before Docker's
volume subcommand, and before it was possible to create named volumes, Docker deleted volumes when there were no more references to them in any containers. Data-only containers are obsolete because Docker now provides the ability to create named volumes, as well as much more utility via the various
docker volume subcommand. Data-only containers are now considered an anti-pattern for this reason.
Many resources on the web from the last couple of years mention using a pattern called a "data-only container", which is simply a Docker container that exists only to keep a reference to a data volume around.
Remember that in this context, a "data volume" is a Docker volume which is not mounted from the host. To clarify, a "data volume" is a volume which is created either with the
VOLUME Dockerfile directive, or using the
-v switch on the command line in a
docker run command, specifically with the format
-v /path/on/container. Therefore a "data-only container" is a container whose only purpose is to have a data volume attached, which is used by the
--volumes-from flag in a
docker run command. For example:
docker run -d --name "mysql-data" -v "/var/lib/mysql" alpine /bin/true
When the above command is run, a "data-only container" is created. It is simply an empty container which has a data volume attached. It was then possible to use this volume in another container like so:
docker run -d --name="mysql" --volumes-from="mysql-data" mysql
mysql container now has the same volume in it that is also in
Because Docker now provides the
volume subcommand and named volumes, this pattern is now obsolete and not recommended.
To get started with the
volume subcommand and named volumes see Creating a named volume