简体   繁体   English

使用Docker Compose在Docker中运行React无法正常工作

[英]Running React in Docker using Docker Compose does not work

I am trying to run a create-react-app application using Docker and Docker Compose. 我正在尝试使用Docker和Docker Compose运行create-react-app应用程序。 I have the following files: 我有以下文件:

docker-compose.yml:

version: '3'

services:
    app:
        restart: always
        build: .
        expose:
            - "3000"
        ports:
            - "3000:3000"
        volumes:
            - .:/app
    nginx:
        restart: always
        image: "nginx:latest"
        ports:
            - "3000:80"
        depends_on:
            - app
        volumes:
            - ./:/app
            - ./nginx.conf:/etc/nginx/nginx.conf

Dockerfile:

FROM node:11.2-alpine
#Setting the working directory as /app
WORKDIR /app
#Copying package.json to Docker Image
COPY package.json /app
#Installing all dependencies.
RUN npm install
COPY . /app
RUN npm run build --production

nginx.conf:

upstream client_LB {
    server web:3000;
}
server {
    listen 80;
    location / {
        proxy_pass         http://client_LB;
        proxy_redirect     off;
        proxy_set_header   Host $host;
        proxy_set_header   X-Real-IP $remote_addr;
        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header   X-Forwarded-Host $server_name;
    }
}

Also, I have the following file structure: 另外,我具有以下文件结构:

my-project
|- .git
|- node_modules
   |- [node modules]
|- public
   |- images
   |- favicon.ico
   |- index.html
   |- manifest.json
|- src
   |- [src files]
|- .dockerignore
|- .editorconfig
|- .env
|- .env.production
|- .gitignore
|- docker-compose.yml
|- Dockerfile
|- gitlab-ci.yml
|- nginx.conf
|- package.json
|- package-lock.json
|- README.md
|- yarn.lock

So, my problem is that after a lot of trial and error, a lot of bug searches, adding nginx, removing nginx and so on, I finally got the containers to start with the above configurations. 因此,我的问题是,经过大量的试验和错误,大量的错误搜索,添加了nginx,删除了nginx等之后,我终于得到了从上述配置开始的容器。

My problem now is that when I try to go to either http://localhost or http://localhost:3000 , nothing shows up. 我现在的问题是,当我尝试访问http:// localhosthttp:// localhost:3000时 ,什么都没有显示。 I see the "This site can't be reached" page with the "ERR_CONNECTION_REFUSED" error code. 我看到“无法访问此站点”页面,显示错误代码“ ERR_CONNECTION_REFUSED”。

So I check if the containers are running using docker ps -a . 所以我检查容器是否正在使用docker ps -a运行。 I see the following: 我看到以下内容:

3f372bdaff3d        nginx:latest              "nginx -g 'daemon of…"   17 minutes ago      Restarting (1) 1 second ago                                       my-project_nginx_1
3252f7ce0283        my-project_app            "node"                   17 minutes ago      Exited (0) 17 minutes ago                                         my-project_app_1

Obviously I can't reach the page if nginx is stuck in restarting and the app isn't even running. 显然,如果nginx停留在重新启动过程中并且应用程序甚至没有运行,我将无法到达页面。

Does anyone know what I might be doing wrong? 有人知道我可能做错了吗?

EDIT #1: docker logs 3f372bdaff3d shows a lot of repeated 编辑#1: docker logs 3f372bdaff3d显示了很多重复

2019/09/18 09:00:05 [emerg] 1#1: "upstream" directive is not allowed here in /etc/nginx/nginx.conf:1
nginx: [emerg] "upstream" directive is not allowed here in /etc/nginx/nginx.conf:1

which does not mean anything to me, but maybe you can help me. 这对我没有任何意义,但是也许您可以帮助我。

EDIT #2: After removing ports section from docker-compose.yml (but keeping expose section) and rewriting nginx.conf (I don't need a load balancer) to look like 编辑#2:docker-compose.yml删除ports部分(但保持expose部分)并重写nginx.conf (我不需要负载平衡器)后看起来像

events { }
http {
  server {
    listen 80;
    root /app/build;
    index index.html;
    location / {
      try_files $uri /index.html;
    }
  }
}

I get the nginx container to run without any errors, but now I'm stuck with the app container restarting instead. 我让nginx容器运行时没有任何错误,但是现在我陷入了重启应用容器的困境。 docker logs [app_id] doesn't show anything. docker logs [app_id]没有显示任何内容。 Any ideas? 有任何想法吗?

My solution was to remove the nginx container and instead run nginx inside the app container (inspired by https://medium.com/@tiangolo/react-in-docker-with-nginx-built-with-multi-stage-docker-builds-including-testing-8cc49d6ec305 ) 我的解决方案是删除nginx容器,而是在应用程序容器内运行nginx(灵感来自https://medium.com/@tiangolo/react-in-docker-with-nginx-built-with-multi-stage-docker-构建包括测试8cc49d6ec305

docker-compose.yml:

version: '3'

services:
    app:
        restart: always
        build: .
        expose:
            - "80"
        ports:
            - "80:80"
        volumes:
            - .:/app

Dockerfile

# Stage 0, "build-stage", based on Node.js, to build and compile the frontend
FROM tiangolo/node-frontend:10 as build-stage

WORKDIR /app

COPY package*.json /app/

RUN npm install

COPY ./ /app/

RUN npm run build

# Stage 1, based on Nginx, to have only the compiled app, ready for production with Nginx
FROM nginx:1.15

COPY --from=build-stage /app/build/ /usr/share/nginx/html

# Copy the default nginx.conf provided by tiangolo/node-frontend
COPY --from=build-stage /nginx.conf /etc/nginx/conf.d/default.conf

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

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