How to recover volumes and data in Docker Container

In 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.

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.

#docker

How to recover volumes and data in Docker Container
23.60 GEEK