使用Apache Docker在Linux中托管ASP.NET Core

问题描述

我在Microsoft文档中引用了这篇文章:

https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/linux-apache?view=aspnetcore-3.1

是否有人试图在Docker容器中完成这些步骤? 我已经呆了几天,在运行容器时无法获取kestrel-helloapp.service文件来自动启动应用程序。

运行容器后,我可以手动进入容器并使用dotnet WebApplication3.dll启动我的应用程序。

我觉得启用服务文件后应该会自动发生这种情况。

我能够使其正常工作的唯一方法是将其添加到Dockerfile中:

ENTRYPOINT ["dotnet","WebApplication3.dll"]

但是,当我这样做时,它将导致Apache服务器无法自动启动。

这是我的Dockerfile:

FROM centos:7
# install sudo and dotnet sdk
RUN yum install sudo -y
RUN sudo rpm -Uvh https://packages.microsoft.com/config/centos/7/packages-microsoft-prod.rpm
RUN yum install epel-release -y
RUN yum install dnf -y
RUN sudo dnf install dotnet-sdk-3.1 -y

# copy app files over
COPY ["./publish/","/var/www/helloapp/publish/"]

# install apache and enable it
RUN sudo yum -y install httpd mod_ssl
RUN systemctl enable httpd.service
RUN yum install initscripts -y
RUN sudo service httpd configtest

# copy and enable service file
COPY ["./kestrel-helloapp.service","/etc/systemd/system/"]
RUN sudo systemctl enable kestrel-helloapp.service

# start apache
CMD ["-D","FOREGROUND"]
ENTRYPOINT ["/usr/sbin/httpd"]

EXPOSE 80

docker run命令:

docker run -v "C:\Users\Nick\source\repos\docker-testing\version1\helloapp.conf:/etc/httpd/conf.d/helloapp.conf"  -e "ASPNETCORE_URLS=http://+:8080"  -p 80:80 -p 8080:8080  -t version1

helloapp.conf文件:

<VirtualHost *:*>
    RequestHeader set "X-Forwarded-Proto" expr="http"
</VirtualHost>

<VirtualHost *:8080>
    ProxyPreserveHost On
    ProxyPass / http://127.0.0.1:5000/
    ProxyPassReverse / http://127.0.0.1:5000/
    ServerName www.example.com
    ServerAlias *.example.com
    ErrorLog /var/log/httpd/helloapp-error.log
    CustomLog /var/log/httpd/helloapp-access.log common
</VirtualHost>

kesterl-helloapp.service文件:

[Unit]
Description=Example .NET Web MVC App running on CentOS 7

[Service]
WorkingDirectory=/var/www/helloapp/publish
ExecStart=/usr/local/bin/dotnet /var/www/helloapp/publish/WebApplication3.dll
Restart=always
# Restart service after 10 seconds if the dotnet service crashes:
RestartSec=10
KillSignal=SIGINT
SyslogIdentifier=dotnet-example
User=apache
Environment=ASPNETCORE_ENVIRONMENT=Production 

[Install]
WantedBy=multi-user.target

我知道配置是正确的,因为当我手动启动应用程序时,一切正常。服务文件似乎在启动时没有启动应用程序。 任何帮助将不胜感激。谢谢!

解决方法

我遇到了类似的问题(仅使用 ubuntu 基础映像)。您可能遇到的问题与以下事实有关,即在入口点从 docker 容器启动了一个进程,而不是系统守护进程(init、systemd,不确定正在使用哪个 centos)。因此,您的服务实际上并没有像您描述的那样启动,因为它们会在系统运行级别更改时由完全相同的系统守护程序启动。

在我看来,不启动系统守护进程是一件好事,因为您希望最大限度地减少在容器内运行的服务。

另一方面,您可能实际上需要容器内的多个服务。问题的实际解决方案是编写一个入口点 shell 脚本并启动要与主应用程序并行运行的服务。就我而言,我想要一个自定义的 Jenkins 映像,其入口点为 handler.postDelayed。您可以在您使用的基础镜像的 Dockerfile 中找到它。

我已经替换了原来的入口点:

/usr/local/bin/jenkins.sh

与:

ENTRYPOINT ["/bin/tini","--","/usr/local/bin/jenkins.sh"]

ENTRYPOINT ["/bin/tini","/docker-entrypoint.sh"] 的内容在哪里:

/docker-entrypoint.sh

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...