(C# ASP.NET Core) How to use Docker Volumes to Persist Data

In the previous tutorial we were able to run an image in a docker container. The docker container provided a RW memory for our project and also for our sqlite database file. And, we noticed that although the app was running fine, yet the data was not persisting between restarts of the container - it was not getting commited to a persistent store. Whenever we closed and restarted the app we found that the sqlite database started blank, with no data. In this tutorial we shall learn how to attach a virtual disk (or a volume) to your container so that data can persist on your hard disk, so that the data remains available on the next start of the container.
(Rev. 31-Oct-2024)

Categories | About |     |  

Parveen,

Run the Docker Desktop

As a first step ensure that the Docker Desktop is up and running on your machine. This ensures that all the necessary background services are in started state.

We shall be using commandline to run our docker container, so you can close the Docker window after starting it.

Video Explanation

Please watch the following youtube video:

Locate the Working Directory in Dockerfile

Next let us locate the working directory for the docker image of your project. We will need this for mounting the volume.

Open your project in visual studio and open the Dockerfile.

Towards the end you will see WORKDIR mentioned. In most cases the working dir is app in lower case.

// Dockerfile 

// somewhere near the end 
// one statement like this 

WORKDIR /app

Run the container

KEEP VISUAL STUDIO and IIS Server/Kestrel everything CLOSED because we will be using docker to run our image now!

Open a command prompt and type the command -


// image is assumed to be of name hellodocker 
// see the previous tutorials where we created it 

docker run --rm --name mycontainer -v myvol2:/app  -d -p 43190:80 hellodocker

  

This is the same command that we used in our previous tutorial, except for an additional parameter: v.

This paramater instructs docker to create a volume/virtual disk of name myvol2, and mount it to the working directory of your container.

Now hit the enter key to run this command!

Your app will start running in a docker container, and it will be available on port 43190.

Verify the app in the Browser

Open the browser to verify that your app is now running inside the browser.

Enter some data and see that everything is functioning. Try to add some data.

This time your data is being saved to the volume myvol2!

Stop the Container

Next let us stop the container so that we can check if the data has been saved or not.

Open a command prompt and type the command -


docker stop mycontainer

  

Try to open the app again on the browser.

The app fails to open because the container has been removed.

Run the App Again

Open a command prompt and again type the command -


docker run --rm --name mycontainer -v myvol2:/app  -d -p 43190:80 hellodocker

  

Open the app again in the browser.

The app opens and we verify that the data has persisted now.

Remove the Volume

Finally, let us remove the volume and clean the things up.

Run the command to remove your volume and perform the cleanup:


docker volume rm  myvol2

  

This Blog Post/Article "(C# ASP.NET Core) How to use Docker Volumes to Persist Data" by Parveen is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.