databasedockercontainer-image

Database in docker containers - readonly images vs durability


Docker images are read only. When we instantiate a container from an image, the processes hosted in the container are able to write on disk, but those changes do not survive container restart. There are plenty of docker containers hosting databases services like Sql Server: https://hub.docker.com/r/microsoft/mssql-server-windows/

Doesn't the read only nature of Docker images defeat the purpose of durable databases? What do I see wrong?

Btw, I see great usability of this read-only nature in automated tests (no need to roll back), but that is not the primary use of a db.


Solution

  • If you want durable data that survives a restart of your container, you can use a volume for storing the data.

    The MySQL image uses the /var/lib/mysql directory to store the "live" data of the database. If you map this folder to a Docker volume, it will survive restarts and deleting the container - unless you also delete the volume. It's possible that the MS SQL image has a similar directory that can be mapped to a volume to make the data durable.

    You're right that the default behavior is to not be durable (great for throwaway tests), but if you want to have it survive, you can use volumes.