dockerasp.net-core.net-coredocker-volumelinux-containers

How to persist my appsettings.json to host machine from Docker container and mount that single file


I have an aspnetcore application that has been containerized and it is running properly, the value of the node ConnectionStrings in appsettings.jon file is being saved based on the value entered from the web interface when application is being setup and this file shouldn't be overwritten after the initial setup when application is updated as you know containers are ephemeral. I have a SetUpController for doing this and everything works well, the reason for doing this is the application runs on premise for most of our clients and they have different setups.

My question is how to ensure the appsettings.jon isn't overwritten when the container is updated. I want to be able to copy the file to a host volume after the application is successfully setup and mount the appsettings.json from the host volume the next time the application runs. I am using a linux container on windows and Docker Toolbox because the system where the application is run can't install Docker for windows.

How do I mount an appsettings.json stored on the host machine anytime the application run?

I have checked online for ideas but none of them seems to work.


Solution

  • So basically since I am using Docker Toolbox for windows the host folder available to it by default is /c/Users you can check this by opening VirtualBox -> settings -> Shared Folders but I was trying to mount /c/app which the VM doesn't have access to. So I changed my run command to this

    docker run -d --name=containername -p 80:80 -v /c/Users/appsettings.json:/app/appsettings.json imagename
    

    Also if you are using Docker ToolBox for windows ensure that the file you are mounting exists in the location before running the command in some cases if you are running into problem I would recommend also restarting the virtual machine using

    docker-machine restart default