为什么在通过Docker启动响应时出现ERR_EMPTY_RESPONSE?

z9smfwbn  于 2022-11-28  发布在  Docker
关注(0)|答案(1)|浏览(341)

我有这样一个应用程序结构:

react-client
server
docker-compose.yaml

golang服务器通常在docker中启动。现在我想在那里运行react。这是我的react-client的docker文件

FROM node:alpine
WORKDIR /app/react
COPY package.json /app/react
COPY package-lock.json /app/react
COPY . /app/react
EXPOSE 3001
RUN npm i
CMD ["npm", "run", "start"]

我Dock合成文件:

version: '3'

services:
    postgres:
        image: postgres:12
        restart: always
        ports:
            - '5432:5432'    
        volumes:
            - ./db_data:/var/lib/postgresql/data
            - ./server/scripts/init.sql:/docker-entrypoint-initdb.d/create_tables.sql
        env_file:
            - ./server/config/.env    
        healthcheck:
            test: [ "CMD", "pg_isready", "-q", "-d", "devdb", "-U","postgres" ]
            timeout: 45s
            interval: 10s
            retries: 10
    redis:
        image: redis:6.2
        volumes:
          - /var/run/docker.sock:/var/run/docker.sock      
        ports:
          - 6379:6379 
    prometheus:
        image: "prom/prometheus:v2.21.0"
        ports:
          - "9090:9090"
        command:
          - "--config.file=/etc/prometheus/prometheus.yml"
        volumes:
          - "./server/metrics/prometheus.yml:/etc/prometheus/prometheus.yml:ro"
    grafana:
        image: grafana/grafana:6.1.6
        environment:
          - GF_AUTH_DISABLE_LOGIN_FORM=true
          - GF_AUTH_ANONYMOUS_ENABLED=true
          - GF_AUTH_ANONYMOUS_ORG_NAME=Main Org.
          - GF_AUTH_ANONYMOUS_ORG_ROLE=Admin
          - GF_USERS_ALLOW_SIGN_UP=false
        ports:
          - "3000:3000"
        volumes:
          - ./grafana/provisioning:/etc/grafana/provisioning
          - ./grafana/dashboards:/var/lib/grafana/dashboards
    web-client:
        build:
          context: ./react-client
          dockerfile: Dockerfile
        ports:
          - "3001:3001"
    app:
        build: 
          context: ./server
          dockerfile: Dockerfile
        ports:
          - 8080:8080
        depends_on:
          - postgres
          - redis

现在,当您启动Docker并转到localhost:3001时,会出现一个错误:ERR_EMPTY_RESPONSE .
我不知道出了什么问题。如果我运行docker而不使用react,并通过npm start运行它,那么一切都很好。也许不知何故,整个项目没有被复制?

编辑

我试着这么做

web-client:
        build:
          context: ./react-client
          dockerfile: Dockerfile
        ports:
          - "127.0.0.1:3001:3001"

但一切都没有改变
我在端口3000上单独启动了react,它开始得很好。显然在端口3001上启动时发生了某种错误,但不清楚是什么错误

yrefmtwq

yrefmtwq1#

我建议你为react-client建立一个映像,然后手动启动一个容器。

FROM node:alpine
WORKDIR /app/react
COPY . .
EXPOSE 3001
RUN npm i
CMD ["npm", "run", "start"]
  • 然后进入react-client目录并运行docker build -t test .
  • 起始容器:docker run -d --name mytest -p 3001:3001 test
  • 检查日志:docker logs mytest
  • 检查是否复制了整个项目:docker run --rm test ash -c "find /app/react -type f -print0 | xargs -0 ls -t"

相关问题