问题
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.
What could I be missing?
Command to get IP address of container(and its output):
docker inspect -f "{{ .NetworkSettings.IPAddress }}" <containerId>
IP_AddressOfRunningDockerContainer
Docker-Compose up output:
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
Application 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"]
回答1:
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
.
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
Then access the application at http://localhost:8888
or https://localhost:8443
.
If ports 80 and 443 are free on your host you can replace 8888 and 8443 with 80 and 443 respectively.
来源:https://stackoverflow.com/questions/57583709/port-80-and-port-443-not-accessible-when-published-via-ports-in-docker