Docker Container - Volume and Data Recovery
This code snippet helps you recover data from a crashed or exited container.
Join the DZone community and get the full member experience.
Join For FreeIn this article, we will look at how to run a Docker container using volume mount and recover data from the Docker volume when a container is crashed/exited.
You may also enjoy: How to Back Up Your Data Volumes to Docker Hub
Run the Docker Container With Volume Using the -v
parameter
Syntax:
docker run -dit --name <containerName> -v <volumeName>:<directoryName> -p 8080:8080 --env-file <env-file.env> <dockerImageName>
Sample command volume for /opt directory:
docker run -dit --name testcontainer -v opt-vol:/opt -p 8080:8080 --env-file properties.env <dockerImageName>
-dit
runs the container in detached mode. For interactive mode use -it.
-p
helps to expose specified ports. If port is already in use, try with other port of host to connect.
List the active Docker containers
docker ps
List the active/exited Docker containers
docker ps -a
If the above container "testcontainer" is exited, then run another container (testcontainer1) with a new volume "opt-vol-1" for /opt directory.
docker run -dit --name testcontainer1 -v opt-vol-1:/opt -p 8080:8080 --env-file properties.env <dockerImageName>
Use the docker cp
command to copy the data from existing container volume "opt-vol" to currrent container /opt
once the new container is up.
docker cp /var/lib/docker/volumes/opt-vol/_data/confighome/config <new-containerID-testcontainer1>:/opt/confighome/
List the Docker volume:
docker volume ls
Inspect Docker volume:
docker volume inspect <volumeName>
docker volume inspect opt-vol
Note: Fill the values in your environment in angle brackets.
Further Reading
Demystifying the Data Volume: Storage in Docker
Opinions expressed by DZone contributors are their own.
Comments