nicolas
nicolas

Reputation: 7668

Docker volumes with Visual Studio

I created a web project in Visual Studio 2017 with Docker support (my host is a Windows 10 machine). My image is based on Linux.

I have also created a webapi where the user can upload a picture. I have to store this picture on the file system. Currently, I'm doing like this:

File.WriteAllBytes(path, bytes); // path = /pictures/abc.jpg

and I can read the image back like this:

byte[] bytes = File.ReadAllBytes(path);

Everything works fine.

The problem is that the picture is stored in my container, but I would like to store it on my host (at the end, the docker image will be hosted on a Raspberry Pi and I have to store the pictures on a SD card).

I think that the solution is to use VOLUME. I tinker a bit with this instruction, but was not able to see the pictures in my host... How can I do that?

The solution is in this file

docker-compose.override.yml

version: '3'  

services:  
  abc:  
    environment:  
      - ASPNETCORE_ENVIRONMENT=Development  
    ports:  
      - "5001:80"  
    volumes:  
      - .:/src  
      - ./pictures:/pictures  // <- The pictures folder is created where my docker project is

Upvotes: 4

Views: 4991

Answers (1)

Tarun Lalwani
Tarun Lalwani

Reputation: 146540

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.

Upvotes: 2

Related Questions