Hi all. I am not sure this is the right place to ask the question or not as this is my first time.
I have been stuck on a CORS issue for over 2 days and it’s really frustrating.
I am using docker-compose.
version: "3.8"
services:
web:
container_name: eco-frontend
build: ./eco-things-frontend
ports:
- 80:80
api:
container_name: eco-backend
build: ./eco-things-backend
ports:
- 3000:3000
links:
- db
environment:
MONGO_INITDB_DATABASE: ecothings
depends_on:
- db
volumes:
- ./db-data/mongo/:/data/db
networks:
- node-network
db:
container_name: mongo-container
restart: always
image: mongo:4.0-xenial
ports:
- "27017:27017"
volumes:
- dbdata6:/data/db
networks:
- node-network
volumes:
dbdata6:
networks:
node-network:
driver: bridge
this is my docker-compose file. my backend is running on 3000 and frontend is running on 80. I am using nginx for the frontend also.
It’s working fine on my local machine(M1 pro).
But, on the server it’s giving me error
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:3000/api/lighting/lighting. (Reason: CORS request did not succeed). Status code: (null)
This is my dockerfile for the backend
#Stage 1
FROM node:16-alpine as build-step
RUN mkdir -p /usr/app
WORKDIR /usr/app
COPY package.*json /usr/app/
RUN npm install
COPY . /usr/app/
EXPOSE 3000
CMD [ "npm", "start" ]
I was digging around and found out maybe it can be achieved by extra_hosts
but I am not sure how as I am a newbie to docker and don’t know how docker networking works.
I am using cors origin * in my code and I have also whitelisted http://localhost:3000 in my config file in the nodejs. Besides, it’s working fine on local so I guess it’s not the code issue.
Any help from you guys is appreciated.
Thank you so much in advance.