How to create and use docker volumes on Asp core W

2019-08-27 14:55发布

问题:

I have web app and I'm using docker to deploy it to Linux server. I'm using ASP .NET CORE 2.1, MVC model for my app, and I publish it through Visual studio's integrated Container Registry to docker hub. from my server i pull image, and then run it. Problem is, every time I publish update to my app it resets all files in app, as it should, that's how containers work. But I need some files to stay in server and not to reset every time i update my app, so i need to use volumes. But I can't figure out how to use ASP CORE app and docker volumes, I think i can create them, but how to access them from app ?

I'v tried some methods I found on web,

So normaly I to run docker I pull it from hub.docker with: docker pull mydockerid/appname:tag

Then, run it with command: docker run -p 3000:80 mydockerid/appname:tag and that's it.

I'v tried : docker run -p 3000:80 -v ~mnt/files/xml:/xml mydockerid/appname:tag

As I understood it it should make docker use server folder, "mnt/files/xml" as folder in my app named "xml", but doesn't work.

I'm also Using Digital ocean and directory "mnt/files/xml" is volume connected to droplet.

Perfect solution would be so I could create docker volume inside droplets volume, but really any solution that works would be great !

Thanks, for Help !

回答1:

Okay so I was close,

to create volume in docker you need to chose directory from server and folder from app. the problem for me was, I was showing wrong directory in app. Because in docker container lets say root folder is under /app/root, so was my xml folder, all I needed to fix is to use docker run -p 3000:80 -v ~mnt/files/xml:/app/xml mydockerid/appname:tag instead of docker run -p 3000:80 -v ~mnt/files/xml:/xml mydockerid/appname:tag first directory is always from server and second is always from app.

As for volumes in digital Ocean:

Go to Volumes under manage on left. Press Create Volume button on top right. Choose configuration of Volume, then attach it to your droplet.

to mount it:

$ mkdir -p /mnt/files 

# Mount your volume at the newly-created mount point:
$ mount -o discard,defaults,noatime /dev/disk/by-id/scsi-0DO_volume_name /mnt/files 

# Change fstab so the volume will be mounted after a reboot
$ echo '/dev/disk/by-id/scsi-0DO_volume_name /mnt/files ext4 defaults,nofail,discard 0 0' | sudo tee -a /etc/fstab

Digital Ocean will actualy show all these comands with your volume names if you go into "Config instruction" under "More" on your Volume.

Then becouse my DO volume is mounted to /mnt/files I connect to server via filezila, put needed files there in folder called "xml" and run the docker container with docker run -p 3000:80 -v ~mnt/files/xml:/xml mydockerid/appname:tag, Now whatever is in folder xml in server. will be read at it is inside my app inside, folder called xml, and I can update app and the folder will never loose data. Also I can move this DO volume to another DO droplet without data loss.