简体   繁体   English

Docker-compose 构建但应用程序不在本地主机上提供服务

[英]Docker-compose builds but app does not serve on localhost

Docker newbie here. Docker 新手在这里。 Docker-compose file builds without any issues but when I try to run my app on localhost:4200, I get a message - localhost didn't send any data on chrome and the server unexpectedly dropped the connection in safari. Docker-compose 文件构建没有任何问题,但是当我尝试在 localhost:4200 上运行我的应用程序时,我收到一条消息 - localhost 没有在 chrome 上发送任何数据,服务器意外断开 safari 中的连接。 I am working on MacOs Catalina.我正在开发 MacOs Catalina。 Here is my yml file:这是我的 yml 文件:

version: '3.0' 

services:
  my-portal:

    build: .

    ports:
    - "4200:4200"

    depends_on:
    - backend

  backend: 

   build: ./backend

   ports:
    - "3000:3000"

   environment:

      POSTGRES_HOST: host.docker.internal
      POSTGRES_USER: "postgres"
      POSTGRES_PASSWORD: mypwd

   depends_on:
    -db

  db:
    image: postgres:9.6-alpine

    environment:
      POSTGRES_DB: mydb
      POSTGRES_USER: "postgres"
      POSTGRES_PASSWORD: mypwd
      POSTGRES_HOST: host.docker.internal

    ports:
    - 5432:5432
    restart: always
    volumes:
    - ./docker/db/data:/var/lib/postgresql/data

Log for Angular: Angular 的日志:

/docker-entrypoint.sh: Configuration complete; /docker-entrypoint.sh:配置完成; ready for start up Log for Node: db connected Log for Postgres: database system is ready to accept connections准备启动 节点日志:db connected Postgres 日志:数据库系统已准备好接受连接

Below are my Angular and Node Docker files:以下是我的 Angular 和节点 Docker 文件:

FROM node:latest AS builder
WORKDIR /app
COPY . .
RUN npm install
RUN npm run build --prod
EXPOSE 4200
# Stage 2
FROM nginx:alpine
COPY --from=builder /app/dist/*  /usr/share/nginx/html/

Node:节点:

FROM node:12    
WORKDIR /backend    
COPY package*.json ./    
RUN npm install    
COPY . .    
EXPOSE 3000
CMD [ "node", "server.js" ]

When I created Angular image and ran my app on localhost:4200 it worked fine.当我创建 Angular 图像并在localhost:4200上运行我的应用程序时,它运行良好。 Please let me know if I am missing anything.如果我遗漏了什么,请告诉我。

Your Angular container is built FROM nginx , and you use the default Nginx configuration from the Docker Hub nginx image . Your Angular container is built FROM nginx , and you use the default Nginx configuration from the Docker Hub nginx image . That listens on port 80, so that's the port number you need to use in use ports: directive:它在端口 80 上侦听,因此这是您需要在 use ports:指令中使用的端口号:

services:
  quickcoms-portal:
    build: .
    ports:
    - "4200:80" # <-- second port must match nginx image's port
    depends_on:
    - backend

The EXPOSE directive in the first stage is completely ignored and you can delete it.第一阶段的EXPOSE指令被完全忽略,您可以将其删除。 The FROM nginx line causes docker build to basically completely start over from a new base image, so your final image is stock Nginx plus the files you COPY --from=builder . FROM nginx行导致docker build基本上完全从新的基础映像重新开始,因此您的最终映像是库存 Nginx 加上您COPY --from=builder

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM