简体   繁体   English

容器启动并在安装到卷时立即停止

[英]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.

相关问题 Windows服务启动,然后立即停止(代码问题?) - Windows Service starts then immediately stops (code issue?) Windows服务立即停止并启动,但它不应该 - windows service stops and starts immediately, but it shouldn't 使用FileSystemWatcher的Windows服务在之后立即启动和停止 - Windows Service with FileSystemWatcher starts and stops immediately after Asp.Net Core 3.1 服务器启动然后立即停止 - 为什么? - Asp.Net Core 3.1 server starts and then stops immediately - Why? 场景开始时角色立即移动 - the character moves immediately when the scene starts 在容器 1 内运行的 Docker 应用程序如何从容器 2 读取文件,容器 2 是卷安装到主机的 - How does Docker App Running Inside Container 1 should read file from Container 2 which is Volume Mounted to Host Machine XNA声音开始或停止播放时的小噪音 - XNA small noise when sound starts or stops playing Windows服务启动,然后在安装到调试文件夹中时停止 - Windows service starts and then stops when installed in debug folder IIS启动/停止WCF服务时是否可以处理 - Is it possible to handle when IIS starts/stops WCF services 确定已安装的TrueCrypt卷的驱动器号 - Determine the Drive Letter of a mounted TrueCrypt volume
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM