[英]Container starts and immediately stops when mounted to a volume
I have the most basic console application which logs messages to a location in container. 我有最基本的控制台应用程序,它将消息记录到容器中的某个位置。 I just want that messages to be written to a persistance volume located in my docker host.
我只想将这些消息写入我的docker主机中的持久卷。
Container - windows container 容器 - 窗户容器
OS - Windows 10 操作系统 - Windows 10
Code: 码:
class Program
{
static void Main(string[] args)
{
Console.WriteLine("starting");
string path = @"c:\exe\MyTest.txt";
int i = 0;
while (true)
{
string createText = $" {i} + Hello and Welcome"+ Environment.NewLine;
File.AppendAllText(path, createText);
i++;
}
}
}
Dockerfile: Dockerfile:
# getting base image
FROM microsoft/windowsservercore:latest
ADD ./bin/debug /exe/
VOLUME c:/data # this line creates issue
ENTRYPOINT ["/exe/BackendService.bat"]
BackendService.bat BackendService.bat
start c:\exe\ConsoleApp16.exe
Command Run: 命令运行:
docker build -t fridayimg1:1.0 .
docker run {ImageID}
Everything works fine if I do not give Volume
info in the DockerFile. 如果我不在DockerFile中提供
Volume
信息,一切正常。
PS: So, the problem is as soon as I start mentioning Volume
the container starts and then exits. PS:所以,问题是我一开始提到
Volume
容器启动然后退出。 When I do docker {containerID} inspect
, I can see that the container c:/data
has been mounted to a physical location in my Volumes folder. 当我执行
docker {containerID} inspect
,我可以看到容器c:/data
已经挂载到Volumes文件夹中的物理位置。
Question: Why my container starts and then exits immediately. 问题:为什么我的容器启动然后立即退出。 But as soon as I remove the volume tag, it continues to work fine and loop and writes messages to
MyTest.txt
file inside the container. 但是一旦我删除了卷标记,它就会继续正常工作并循环并将消息写入容器内的
MyTest.txt
文件。
You need to map the volume when you run the container, not when you build it (in Dockerfile). 您需要在运行容器时映射卷,而不是在构建容器时映射卷(在Dockerfile中)。
docker run -v ./data:/data <your image>
This will map a folder "data" in your current folder to the path "/data" in your container. 这会将当前文件夹中的文件夹“data”映射到容器中的路径“/ data”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.