【问题标题】:Host ASP.NET Core in Linux with Apache Docker使用 Apache Docker 在 Linux 中托管 ASP.NET Core
【发布时间】:2020-12-04 17:18:22
【问题描述】:

我在 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 运行命令:

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

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

【问题讨论】:

    标签: c# linux apache docker .net-core


    【解决方案1】:

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

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

    另一方面,您实际上可能需要容器内的多个服务。您的问题的一个实际解决方案是编写一个入口点 shell 脚本并启动您希望与主应用程序并行运行的服务。在我的例子中,我想要一个定制的 Jenkins 镜像,它的入口点是/usr/local/bin/jenkins.sh。您可以在您使用的基础镜像的 Dockerfile 中找到它。

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

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

    与:

    ENTRYPOINT ["/bin/tini", "--", "/docker-entrypoint.sh"]
    

    /docker-entrypoint.sh的内容在哪里:

    #! /bin/bash
    /usr/bin/cron &            # This is the additional service I wanted in the background
    /usr/local/bin/jenkins.sh
    

    【讨论】:

      猜你喜欢
      • 2022-10-15
      • 2020-08-04
      • 2020-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-13
      • 2019-04-01
      • 2017-12-04
      相关资源
      最近更新 更多