本文介绍了如何在Asp核心Web应用(MVC)上创建和使用Docker卷的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Web应用程序,并且我正在使用docker将其部署到Linux服务器.我正在为我的应用程序使用ASP .NET CORE 2.1,MVC模型,并通过Visual Studio的集成Container Registry将其发布到docker hub.从我的服务器上拉图像,然后运行它.问题是,每次我将更新发布到我的应用程序时,它都会按需重置应用程序中的所有文件,这就是容器的工作方式.但是我需要一些文件保留在服务器中,并且每次更新应用程序时都不要重置,因此我需要使用卷.但是我不知道如何使用ASP CORE应用程序和docker卷,我想我可以创建它们,但是如何从应用程序访问它们?

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,

所以通常我要运行docker时,我会使用以下命令从hub.docker中将其拉出: docker pull mydockerid/appname:tag

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

然后,使用命令运行它: docker run -p 3000:80 mydockerid/appname:tag 就是这样.

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

我尝试过: docker run -p 3000:80 -v〜mnt/files/xml:/xml mydockerid/appname:tag

据我了解,它应该使docker使用服务器文件夹"mnt/files/xml"作为我的名为"xml"的应用程序中的文件夹,但不起作用.

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.

我也在使用数字海洋,目录"mnt/files/xml"是连接到Droplet的卷.

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

完美的解决方案是这样,这样我就可以在Droplet卷中创建docker卷,但是实际上任何可行的解决方案都很棒!

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

谢谢您的帮助!

推荐答案

好的,我很亲近,

要在Docker中创建卷,您需要从服务器中选择目录,从应用程序中选择文件夹.我的问题是,我在应用程序中显示了错误的目录.因为在docker容器中可以说根文件夹位于/app/root下,所以我的xml文件夹也是如此,所以我需要修复的就是使用 docker run -p 3000:80 -v〜mnt/files/xml:/app/xml mydockerid/appname:tag 而不是 docker run -p 3000:80 -v〜mnt/files/xml:/xml mydockerid/appname:tag 第一个目录始终来自服务器第二个总是来自应用程序.

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:

转到左侧管理下的卷.按右上角的创建音量"按钮.选择Volume的配置,然后将其附加到您的液滴上.

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

安装:

$ 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实际上将显示所有这些命令和卷名.

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

然后因为我的DO卷已挂载到/mnt/files,我通过filezila连接到服务器,将所需的文件放在"xml"文件夹中,并使用 docker容器运行docker run -p 3000:80 -v〜mnt/files/xml:/xml mydockerid/appname:tag ,现在服务器中的xml文件夹中都有.它将在我的应用程序内部(名为xml的文件夹)中读取,并且我可以更新应用程序,并且该文件夹永远不会丢失数据.同样,我可以将此DO体积移动到另一个DO液滴而不会丢失数据.

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.

这篇关于如何在Asp核心Web应用(MVC)上创建和使用Docker卷的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 05:05