【问题标题】:Container starts and immediately stops when mounted to a volume容器在安装到卷时启动并立即停止
【发布时间】:2019-04-01 15:36:40
【问题描述】:

我有最基本的控制台应用程序,它将消息记录到容器中的某个位置。我只想将这些消息写入位于我的 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 文件。

【问题讨论】:

  • 你在docker logs看到了什么?
  • 您不能在 Dockerfile 中使用绝对路径。你到底想达到什么目的?
  • 我希望将消息写入主机的卷中,在持久性存储中
  • 那个VOLUME 指令不能这样做;除了 COPYADD 之外,Dockerfile 中没有任何内容引用主机。它会产生一些令人惊讶的副作用,我建议将其移除。 @Mihai 的答案没有它就可以正常工作。

标签: c# .net docker containers


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-21
    • 2018-05-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多