通过Docker中的`ports'发布时,无法访问端口80和443 [英] Port 80 and Port 443 not accessible when published via `ports` in Docker

查看:94
本文介绍了通过Docker中的`ports'发布时,无法访问端口80和443的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的ASP.Net Core应用程序能够使用Docker连接到postGres数据库,并且Docker compose成功完成(请参阅下面的Docker-Compose up输出).但是,我无法使用URL http://IP_AddressOfRunningDockerContainer:443 从URL从我的PC浏览到Docker容器上的ASP.Net Core应用程序或管理员(Postgres客户端)分别为http://IP_AddressOfRunningDockerContainer:8080 .

My ASP.Net Core application is able to connect to a postGres database using Docker and Docker compose successfully(please see Docker-Compose up output below). I am however not able to browse to either the ASP.Net Core application or adminer(Postgres client) on my docker containers, from my PC using the urls http://IP_AddressOfRunningDockerContainer:443 or http://IP_AddressOfRunningDockerContainer:8080 respectively.

我可能会缺少什么?

获取容器IP地址(及其输出)的命令:

docker inspect -f "{{ .NetworkSettings.IPAddress }}" <containerId>
IP_AddressOfRunningDockerContainer

Docker-组合输出:

db_1        | 2019-08-21 01:52:03.905 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
db_1        | 2019-08-21 01:52:03.905 UTC [1] LOG:  listening on IPv6 address "::", port 5432
db_1        | 2019-08-21 01:52:03.925 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db_1        | 2019-08-21 01:52:03.964 UTC [22] LOG:  database system was shut down at 2019-08-21 01:30:03 UTC
db_1        | 2019-08-21 01:52:03.999 UTC [1] LOG:  database system is ready to accept connections
adminer_1   | PHP 7.3.7 Development Server started at Wed Aug 21 01:22:13 2019
adminer_1   | Listening on http://[::]:8080
adminer_1   | Document root is /var/www/html
adminer_1   | Press Ctrl-C to quit.
adminer_1   | PHP 7.3.7 Development Server started at Wed Aug 21 01:23:47 2019
adminer_1   | Listening on http://[::]:8080
adminer_1   | Document root is /var/www/html
adminer_1   | Press Ctrl-C to quit.
adminer_1   | PHP 7.3.7 Development Server started at Wed Aug 21 01:27:23 2019
adminer_1   | Listening on http://[::]:8080
adminer_1   | Document root is /var/www/html
adminer_1   | Press Ctrl-C to quit.
adminer_1   | PHP 7.3.7 Development Server started at Wed Aug 21 01:52:03 2019
scrubber_1  | Hosting environment: Development
scrubber_1  | Content root path: /app
scrubber_1  | Now listening on: https://[::]:443
scrubber_1  | Now listening on: http://[::]:80
scrubber_1  | Application started. Press Ctrl+C to shut down.

Docker-Compose.yml:

    version: '3.4'

docker
    networks:
      frontend:
      backend:

    services:
      db:
        image: postgres
        restart: always
        environment:
          POSTGRES_PASSWORD: <SomeStrongPassword>
          POSTGRES_DB: scrubber
          POSTGRES_USER: ajitgoel      
        networks:
          backend:

      adminer:
        image: adminer
        restart: always
        ports:
          - 8080:8080
        networks:
          backend:

      scrubber:
        image: ${DOCKER_REGISTRY-}scrubber
        environment:
          - ASPNETCORE_ENVIRONMENT=PRODUCTION  
        build:
          context: . 
          dockerfile: Dockerfile
        networks:
          frontend:
          backend:
        depends_on:
          - db

应用dockerFile.yml:

FROM mcr.microsoft.com/dotnet/core/aspnet:2.2-stretch-slim AS base
RUN apt-get update && apt-get -y install iputils-ping && apt-get -y install xvfb && apt-get -y install fontconfig && apt-get -y install libssl1.0-dev && apt-get -y install libx11-dev libx11-xcb-dev libxcb-icccm4-dev libxcb-image0-dev libxcb-keysyms1-dev libxcb-randr0-dev libxcb-render-util0-dev libxcb-render0-dev libxcb-shm0-dev libxcb-util0-dev libxcb-xfixes0-dev libxcb-xkb-dev libxcb1-dev libxfixes-dev libxrandr-dev libxrender-dev
#RUN chmod a+rwx -R /usr/bin/xvfb-run
WORKDIR /app
#EXPOSE 2222
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/core/sdk:2.2-stretch AS build
WORKDIR /src
COPY ["Scrubber/Scrubber.csproj", "Scrubber/"]
RUN dotnet restore "Scrubber/Scrubber.csproj"
COPY . .
WORKDIR "/src/Scrubber"
RUN dotnet build "Scrubber.csproj" -c Release -o /app

FROM build AS publish
RUN dotnet publish "Scrubber.csproj" -c Release -o /app

FROM base AS final
WORKDIR /app
COPY --from=publish /app .
#RUN chmod a+rwx -R /app/QtBinariesLinux
ENTRYPOINT ["dotnet", "Scrubber.dll"]

推荐答案

您不能使用其IP从主机访问容器.您需要映射主机上的公开端口.就像您已经为管理员做过的事情一样.

You cannot access the containers form the host using their IP's. You need to map the exposed ports on the host. Like you already did for adminer.

您的管理员服务应该已经可以在以下位置使用: http://localhost:8080

Your adminer service should already be available at: http://localhost:8080

要使其他容器可用,您需要将服务声明更改为此:

To make your other container available you need to change the service declaration to this:

      scrubber:
        image: ${DOCKER_REGISTRY-}scrubber
        environment:
          - ASPNETCORE_ENVIRONMENT=PRODUCTION  
        build:
          context: . 
          dockerfile: Dockerfile
        networks:
          frontend:
          backend:
        depends_on:
          - db
        ports:
          - 8888:80
          - 8443:443

然后通过 http://localhost:8888 https://localhost:8443 访问该应用程序.

Then access the application at http://localhost:8888 or https://localhost:8443.

如果主机上的端口80和443空闲,则可以分别用80和443替换8888和8443.

If ports 80 and 443 are free on your host you can replace 8888 and 8443 with 80 and 443 respectively.

这篇关于通过Docker中的`ports'发布时,无法访问端口80和443的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆