How to create a persistent Docker volume

Robert Canare
3 min readMay 21, 2021

--

Our Bitwarden application is currently running inside the Docker containers, some of the files and directories are not persistent, which means all the files we have updated straight inside the container will be lost once the container restarted.

Docker images

All the containers are relying on each image.

Docker images

It means when we restart the container it will pull the default data from their own image.

Docker Compose file

The compose file contains all the configurations for each container, the compose file is currently located at /root/bwdata/docker/ docker-compose.yml.

Compose File

Creating a persistent volume

In this example, we will be creating a volume for the container bitwarden-web.

Go inside the directory where the compose file is located.

cd /root/bwdata/docker

And run these to list all the container names.

docker ps 

Once you get the container name you will need to go inside the container to locate the file you need and copy it to the host server.

docker exec -ti bitwarden-web bash

In this example, we will make a persisted volume for the /app/image directory on the container bitwarden-web then exit from the container once you get the path.

Content of /app/image

Then you need to copy the whole directory to the host.

docker cp bitwarden-web:/app/images .

Then check it on your current location.

Directory /image is now on the host

You need to turn off the containers now, make sure you are inside the directory /root/bwdata/docker where the compose file is located.

docker-compose down
Docker Compose Down

Once the container is down, you will need to update the compose file.

vi docker-compose.yml

And add the location -./location-on-host:/target/location-inside-the-container.

Adding volume on web container

Then save and exit.

Then power up the containers.

docker-compose up -d

And wait for all the containers to boot up.

To test it, create a text file inside the persistent volume.

Added test file

And see if it inside the container.

Inside the container

The /app/image directory on the container bitwarden-web is now persisted.

--

--

Robert Canare
Robert Canare

Written by Robert Canare

I’m just curious on everything.

No responses yet