Docker Community Forums

Share and learn in the Docker community.

Problem reading/writing docker volume from Visual Studio on Windows

I am prototyping a C# application for docker that has capability for reading and writing files. I am using WSL2 on my Windows 10 PC. I am running my application from Visual Studio.

I created a docker volume manually using this syntax: Docker volume create –name=MyVolume

As expected it creates a folder in this location: \wsl$\docker-desktop-data\version-pack-data\community\docker\volumes\MyVolume

Here is my docker-compose.yml :

version: '3.4'

services:
  myservice:
    image: ${DOCKER_REGISTRY-}myservice
    build:
      context: .
      dockerfile: MyService/Dockerfile
    volumes:
      - MyVolume:/data

volumes:
    MyVolume:
      external: true

I set my startup project to docker-compose and run via the Docker Compose button.

Problem : when I write a file (using File.Create), the file gets written to my application directory, not to the docker volume.

What am I doing wrong?

You need to use docker volumes. You can set the volume on host like below

docker run -v ~/Desktop/images:/my/images/inside/docker
Make sure your code writes to /my/images/inside/docker inside the container. This will move it to the host as well.

You can also use named volumes

docker volume create images

docker run -v images:/my/images …
This will store images on a volume which will remain even container dies. But this will not store it on your host

Edit-1

So docker-compose.override.yml is what you need to use. Change the content of the file as

version: ‘3’

services:
ci-build:
volumes:
- .:/src
- ~/Desktop/images:/images/inside
This will combine both the files and then run it.