在 docker 下运行的 .Net Core API [英] .Net Core API running under docker

查看:38
本文介绍了在 docker 下运行的 .Net Core API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个项目(.Net Core API),它在 Windows 的 Docker 下运行,并提供客户可以上传文件的服务.

I have a project (.Net Core API) which runs under Docker for windows and have service where customers can upload files.

我想将这些文件保存在服务器上(例如C:TempFolder"),但是当我在调试模式下(使用 docker)运行项目并从代码动态创建目录时出现错误:'System.IO.IOException: 无效参数..."

I want to save these files on the server (for example "C:TempFolder") but when I running project in debug mode (with docker) and creating directory dynamically from code there is an error: 'System.IO.IOException: Invalid argument..."

CurrentDirectory 是/app 并且 DirectoryInfo FullPaths 中的值为/app/C:TempFolder"

CurrentDirectory is /app and in DirectoryInfo FullPaths value is "/app/C:TempFolder"

当我在没有 docker 的情况下运行应用程序时它工作正常

when I running app without docker it works fine

这听起来可能很糟糕,但我不得不问:

It may sound bad, but I have to ask:

是否可以在 docker 下运行时创建目录(某处,我有权限的地方,例如在我的本地 PC 或服务器上)?

is it possible to create directory (somewhere, where I have permission, for example on my local PC or the server) when running under docker?

这是我使用 docker 的第一个项目,所以...

this is first project for me where I'm using docker so...

推荐答案

你可以使用 docker 卷 作为启动 docker 容器时的参数,或者在 中定义卷docker-compose 如果你正在使用它.

you can use docker volumes as arguments when starting docker container, or have volumes defined in docker-compose if you're using it.

docker volume create my-vol

docker run -d --name devtest --mount source=my-vol,target=/app

或者使用 docker-compose.yml 映射 docker 卷:

Or map docker volume using docker-compose.yml:

    volumes: 
        - my-volume:/app

或者映射到相对于 docker-compose.yml 的目录:

Or map to directory relative to docker-compose.yml:

    volumes: 
        - ./temp-dir:/app

请记住,容器内的代码不知道某些文件夹已映射到容器外部的存储.

Have in mind that code inside container will have no idea some folder is mapped to storage external to container.

从主机的角度来看,映射到 docker 卷和映射到相对路径的主要区别在于,Docker 将决定该卷在主机中的存储位置以及谁可以访问它,而相对路径是您控制的目录.

From host perspective main difference between mapping to docker volume and mapping to relative path is that Docker will be deciding where in the host that volume is stored and who can access it, while relative path is a dir that you control.

使用相对路径时要注意的另一点是确保您对目录设置权限,以便 docker 进程可以使用它.

Another point to note when using relative path is be sure you set permissions on your directory so docker processes can use it.

这篇关于在 docker 下运行的 .Net Core API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆