i am trying to setup a node + react app for local develpoment environment using docker + nginx
nginx is running on 3002
- for route /api strip off the /api and redirect to service api i.e. the node app
- redirect other routes to service client i.e. react app
or
docker-compose file
version: "3" services: api: build: context: api dockerfile: Dockerfile.dev ports: - 3000:3000 volumes: - ./api:/app client: build: context: client dockerfile: Dockerfile.dev ports: - 3001:3000 volumes: - ./client:/app nginx: restart: always build: context: nginx dockerfile: Dockerfile.dev ports: - 3002:80 depends_on: - client - api
nginx default.dev.conf
upstream api { server api:3000; } upstream client { server client:3001; } server { listen 80; location / { proxy_pass http://client/; } location /api { rewrite /api/(.*) /$1; proxy_pass http://api/; } }
client Dockerfile.dev
FROM node:alpine WORKDIR /app CMD ["npm", "run", "start"]
api Dockerfile.dev
FROM node:alpine WORKDIR /app CMD ["npm", "run", "dev"]
nginx Dockerfile.dev
FROM nginx COPY default.dev.conf /etc/nginx/conf.d/default.conf
- on visiting http://localhost:3002/api - i get Cannot GET /api (magically works now i didnt change anything)
- on visiting http://localhost:3002 - i get 502 Bad Gateway
- http://localhost:3000 and http://localhost:3001 work
- on docker exec -it bytecode_api_1 sh (i.e. going inside terminal of api container) ping client and ping nginx work
没有评论:
发表评论