locked
Why I can not access the website in linux localhost? RRS feed

  • Question

  • User1052024640 posted

    I am a beginner of Linux.

    These days I am following the official tutorial of Microsoft(https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/docker/building-net-docker-images?view=aspnetcore-3.1 ) to run an image of an asp.net core project.

    The project runs well in the IIS of the Windows server without any error.

    I run these codes in centos8:

    docker build -t aspnetapp .
    docker run -it --rm -p 5000:80 --name aspnetcore_sample aspnetapp

    And the centos8 shows these:

    [root@worker ~]# docker run -it --rm -p 5000:80 --name testpage test
    warn: Microsoft.AspNetCore.DataProtection.Repositories.FileSystemXmlRepository[60]
    Storing keys in a direcory '/root/.aspnet/DataProtection-Keys' that may not be persisted outside of the container. Protected data will be unavailable while container is destoryed.
    warn: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[35]
    No XML encryptor configured. Key{fc3b5a5e-e157-4f59-8d7d-569258b0bc5b} may be persisted to storage in unencrypted form.
    Hosting environment:Production
    Content root path:/app
    Now listening on :http://[::]:80
    Application started. Press Ctrl+C to shut down.

    It seems the website runs successfully.

    Whereas, when I input the URL(http://localhost:5000/) which the tutorial said in firefox of Gnome. It can not access the website and display "unable to connect" in the browser.

    I doubt whether there are some problems with the network or the project.

    I tried to access www.google.com in the firefox of Gnome and successfully. The firewall of the centos8 has not started.

    And also, I tried to use the official sample project in the tutorial to replace my project. The problem still here.

    What's wrong with it? How can I solve it? Thank you.

    Wednesday, January 22, 2020 1:14 PM

Answers

  • User1052024640 posted

    Finally, I found the problem.

    I set the router like this before:

    app.UseRouter(routes =>
                {
                    routes.MapMiddlewareRoute("{culture=en}/{*mvcRoute}", subApp =>
                    {
                        subApp.UseRequestLocalization(options);
                        subApp.UseRouting();
                        subApp.UseEndpoints(mvcRoutes =>
                        {
                            mvcRoutes.MapControllerRoute(
                                name: "default",
                                pattern: "{culture=en}/{controller=Home}/{action=Index}");
                            // map the .html suffix
                            mvcRoutes.MapControllerRoute(
                                name: "home.html",
                                pattern: "{culture=en}/{controller=Home}/{action}.html");
                        });
                    });
                });

    So that the URL should be http://172.17.0.1:5000/index/ or http://172.17.0.1:5000/index.html but not http://172.17.0.1:5000

    All works well now.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 23, 2020 1:58 PM

All replies

  • User-474980206 posted

    In your docker container you are mapping port 5000 to port 80. If the asp.net core app is configured for port 5000, to access it  outside the docker image you use port 80.

    Wednesday, January 22, 2020 3:26 PM
  • User1052024640 posted

    When I access the http://localhost:80, it turns out to be the welcome page of Nginx but not the sample project of Microsoft.

    In addition, I have pulled the Nginx image in the docker of this computer but never use it or start it in docker. I don't know whether it is the problem.

    Thursday, January 23, 2020 4:39 AM
  • User1052024640 posted

    I tried to input "nginx -v" in command line of root and it shows:

    nginx version: nginx/1.14.1

    It seems there is an Nginx not only in docker but also on the computer.

    I tried to use "systemctl stop nginx" to stop the Nginx. After I stop it, from the "system status nginx" shows the Nginx service is dead.

    However, after I access the URL(localhost:80) it still turns out to be the welcome page of Nginx.

    Thursday, January 23, 2020 4:46 AM
  • User1052024640 posted

    Well, I tried another tutorial in https://github.com/dotnet/dotnet-docker/blob/master/samples/aspnetapp/README.md

    Just by input 'docker run --rm -it -p 8000:80 mcr.microsoft.com/dotnet/core/samples:aspnetapp'

    Now it works. But I still don't know what's the problem with it.

    Thursday, January 23, 2020 5:02 AM
  • User1052024640 posted

    I have paid a whole day to learn more about this.

    It is so strange that now I can connect to a brand new website created by visual studio successfully while I can not connect to the website I made before.

    The website I made before runs successfully in IIS or Docker Compose in windows 10.

    Here is the code I ran it in centos8:

    [root@test gameone]# docker run -d -p 5000:80 --name gameone gameone
    68ed8f8631dda4b4ac584f0d32781e468427d87a372f4a728841805ade8b7ef6
    [root@test gameone]# docker ps -a
    CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
    68ed8f8631dd gameone "dotnet gameone.d…" About a minute ago Up About a minute 0.0.0.0:5000->80/tcp gameone
    96538af15a99 nginx "nginx -g 'daemon of…" 56 minutes ago Up 56 minutes 0.0.0.0:8080->80/tcp docker-nginx
    [root@test gameone]#

    As you see, there is a nginx ran also. I can access the nginx test page via the URL http://172.17.0.1:8080/ successfully.

    However, I can not access my asp.net core website via the URL http://172.17.0.1:5000

    I don't know what's wrong with this. Could you please help me? Thank you.

    Thursday, January 23, 2020 10:20 AM
  • User1052024640 posted

    Finally, I found the problem.

    I set the router like this before:

    app.UseRouter(routes =>
                {
                    routes.MapMiddlewareRoute("{culture=en}/{*mvcRoute}", subApp =>
                    {
                        subApp.UseRequestLocalization(options);
                        subApp.UseRouting();
                        subApp.UseEndpoints(mvcRoutes =>
                        {
                            mvcRoutes.MapControllerRoute(
                                name: "default",
                                pattern: "{culture=en}/{controller=Home}/{action=Index}");
                            // map the .html suffix
                            mvcRoutes.MapControllerRoute(
                                name: "home.html",
                                pattern: "{culture=en}/{controller=Home}/{action}.html");
                        });
                    });
                });

    So that the URL should be http://172.17.0.1:5000/index/ or http://172.17.0.1:5000/index.html but not http://172.17.0.1:5000

    All works well now.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 23, 2020 1:58 PM