How To Deal With Persistent Storage (e.g. Databases) In Docker?
The best way to deal with persistent data storage (such as a database) in Docker is to use Docker’s volume API (for docker 1.9.0 or newer) or use data-only containers for older versions of Docker.
Docker 1.9.0 or newer
The best approach for you is to use docker volume
API as shown in the following example:
docker volume create --name myvolume
docker run -d -v myvolume:/container/path/for/volume container_image my_command
Let’s break down the example command:
- The first command creates a new volume named
myvolume
in the current working directory - The second command runs the container image
container_image
and configures it to consume the newly created volumemyvolume
If you create a container with a -v volume_name:/container/fs/path
Docker will automatically create a named volume for you that can:
- Be listed through the
docker volume ls
- Be identified through the
docker volume inspect volume_name
- Backed up as a regular directory
- Backed up as before through a
--volumes-from
connection
You can also identify dangling volumes (unused volumes)
docker volume ls -f dangling=true
And then remove them using their name:
docker volume rm <volume_name>
Docker 1.8.X and older
The approach that seems to work best for production is to use a data-only container. The data-only container is run on a barebones image and does nothing except expose a data volume.
Then you can run any other container to have access to the data container volumes:
docker run --volumes-from data_container some_other_container command_to_exec
You can read more about this technique in the official documentation .
-
How To Deal With Persistent Storage (e.g. Databases) In Docker?
The best way to deal with persistent data storage (such as a database) in Docker is to use Docker’s volume API (for docker 1.9.0 or newer) or use data-only containers for older versions of Docker. ...
Questions -
Solved: Cron missing newline before EOF
This error may also happen when a crontab file is generated automatically and the generator failed to insert a newline character at the end of the crontab file.
Questions -
How to Include Files outside of Docker’s Build Context?
You may have ended up in a situation where you wanted to include a file from outside of Docker's build context using the ADD command, but the ADD command requires the path to be within the build co...
Questions -
Solved: Errors in crontab file, can't install
You may encounter this error while creating a new crontab or updating an existing one that has a syntax error.
Questions
Make your mark
Join the writer's program
Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.
Write for usBuild on top of Better Stack
Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.
community@betterstack.comor submit a pull request and help us build better products for everyone.
See the full list of amazing projects on github