从Docker容器连接到运行该容器的主机上的服务时,连接被拒绝

时间:2019-11-29 08:52:36

标签: docker nginx centos centos7

我正在Centos 7上运行docker容器和.NET Core服务。我使用以下Dockerfile来生成我的容器(Angular应用程序):

FROM node:11-alpine as builder
COPY package.json ./
RUN npm i && mkdir /ng-app && cp -R ./node_modules ./ng-app
WORKDIR /ng-app
COPY . .
RUN $(npm bin)/ng build --prod --build-optimizer=false

FROM nginx:1.13.3-alpine
RUN rm -f /etc/nginx/conf.d/default.conf
COPY ./docker/nginx.conf /etc/nginx/conf.d/
RUN rm -rf /usr/share/nginx/html/*
COPY --from=builder /ng-app/dist /usr/share/nginx/html

CMD ["nginx", "-g", "daemon off;"]

我使用以下图像构建图像:

docker build -t myservice .

运行容器:

docker run -d -it --name=msrv --net=host myservice
在服务器上运行的

netstat -plntu包含以下两行(在运行容器时):

Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      1469/nginx: master
tcp6       0      0 :::5000                 :::*                    LISTEN      2936/dotnet

.NET Core服务可通过服务器的公共地址访问。使用--net=host选项,我认为容器中运行的应用程序应该能够使用地址http://localhost:5000/api/webtoken连接到服务(以获取登录时的安全令牌),但是当出现以下情况时,我会得到net:ERR_CONNECTION_REFUSED我尝试访问它。使用Postman时,我可以很好地获得令牌。

一切似乎都井然有序,但是我无法从容器连接到dotnet服务。我在做什么错了?

如果有帮助,请参见nginx.conf文件:

server {
  listen 80;

  set $backend_addr http://localhost:5000;

  location /api/ {
    proxy_pass $backend_addr;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  }

  location / {
    root   /usr/share/nginx/html;
    index  index.html index.htm;
    try_files $uri$args $uri$args/ $uri $uri/ /index.html =404;
  }
}

0 个答案:

没有答案
相关问题