首页 > 解决方案 > 在 Docker 容器中启动 Windows 服务

问题描述

我正在使用 Docker for Windows 并尝试将 Asp.NET MVC 5 转换为容器。剩下的一个障碍是我需要运行 ASPNET 状态服务器。我可以通过交互式终端启动服务,它工作得很好,但我无法让容器自动启动服务。我尝试过使用 CMD、ENTRYPOINT 和 RUN,但据我所知,其中一些只会在构建映像时执行命令,而不是在容器启动时执行。

我的 DOCKERFILE 如下

FROM microsoft/aspnet:4.7.1-windowsservercore-1709
ARG source
WORKDIR /inetpub/wwwroot
COPY ${source:-obj/Docker/publish} .
CMD powershell -Command \
Set-Service aspnet_state automatic; \
Start-Service -name "aspnet_state"; \

EXPOSE 1433

标签: dockerdockerfile

解决方案


我没有使用 CMD,而是使用 RUN 将命令提交到图像,并使用了多个 RUN 命令:

# Enable Session State Server
RUN powershell -Command Set-Service aspnet_state -startuptype automatic 
RUN powershell -Command Stop-Service aspnet_state
RUN powershell -Command Start-Service aspnet_state 
RUN powershell -Command Set-ItemProperty Registry::HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\aspnet_state\Parameters -Name  AllowRemoteConnection -Value 1 

推荐阅读