Docker compose with NextJS frontend and Ruby on Rails backend, CORS issues

Viewed 189

I got the following docker-compose.yml with a Ruby on Rails backend (uses rack-cors) and a NextJS frontend. However I get CORS issues, because my browser of course isn't able to resolve the docker alias for my backend when trying to make any other requests than GET requests (e.g. trying to add a REST resource, trying to log in, etc.).

In this StackOverflow post I read that I should seperate my requests in client side and server side calls. However is that really the correct approach or could I configure rack-cors in Rails in such a way that the distinction to client side vs. server side calls doesn't have to be made?

Also: In the linked example the client side API url is still localhost, however once its hosted its the domain I've chosen. It can't be a valid solution to change the API url to the actual domain when its already live, or is that really common practice?

version: '3.9'
services:
  db:
    image: postgres:13-alpine
    volumes:
      - ./bar-api/tmp/db:/var/lib/postgresql/data
    env_file:
      - .env
  backend:
    build: ./bar-api
    command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
    volumes:
      - ./bar-api:/api
    depends_on:
      - db
    env_file:
      - .env
    ports:
      - '3000:3000'
  frontend_prod:
    build: ./bar-frontend
    command: yarn start
    depends_on:
      - backend
    env_file:
      - .env
    ports:
      - '3001:3001'
  frontend_dev:
    image: node:16-alpine
    volumes:
      - ./bar-frontend:/frontend
    working_dir: /frontend
    command: yarn dev
    ports:
      - '3002:3001'
    depends_on:
      - backend
    env_file:
      - .env

0 Answers
Related