容器启动后立即停止,安装到卷上

时间:2019-04-01 15:36:41

标签: c# .net docker containers

我有最基本的控制台应用程序,可将消息记录到容器中的某个位置。我只希望将这些消息写入到我的Docker主机中的持久性卷中。

容器-Windows容器

操作系统-Windows 10

代码:

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:

# getting base image
FROM microsoft/windowsservercore:latest

ADD ./bin/debug /exe/

VOLUME c:/data   # this line creates issue

ENTRYPOINT ["/exe/BackendService.bat"]

BackendService.bat

start c:\exe\ConsoleApp16.exe 

命令运行:

docker build -t fridayimg1:1.0 .   

docker run {ImageID}

如果我没有在DockerFile中提供Volume信息,一切都会很好。

P.S:所以问题是,我一开始提到Volume,容器就会启动然后退出。当我做docker {containerID} inspect时,我可以看到 容器c:/data已安装到“我的卷”文件夹中的物理位置。

问题:为什么我的容器启动然后立即退出。但是,一旦我删除了卷标,它就会继续正常工作并循环,并将消息写入容器内的MyTest.txt文件中。

1 个答案:

答案 0 :(得分:0)

您需要在运行容器时而不是在构建容器时(在Dockerfile中)映射卷。

docker run -v ./data:/data <your image>

这会将当前文件夹中的文件夹“ data”映射到容器中的路径“ / data”。

相关问题