title: CORS "Access-Control-Allow-Credentials", "Access-Control-Allow-Origin" response headers appear for local but not docker container
body: <p>can anyone help me solve this issue?</p>
  <p>I'm running a php Laravel backend with fortify and sanctum for auth. I also have a React frontend which makes api calls to this backend, both of these are dockerized. If I run the php server locally, there are no issues with CORs. But once the backend is running via a docker container, the following issue pops up:
  <a href="https://i.stack.imgur.com/ZS2jl.png" rel="nofollow noreferrer">CORS Issue</a></p>
  <pre><code>Access to XMLHttpRequest at 'http://localhost:8000/api/tickets' from origin 'http://localhost' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
  </code></pre>
  <p>On the network tab, we see that the Request URL doesn't have the following fields in the Response Headers:
  &quot;Access-Control-Allow-Credentials&quot;, &quot;Access-Control-Allow-Origin&quot;.
  Is there a way to have two docker containers allow CORs?</p>
  <p>What I have tried:</p>
  <ul>
  <li>Setting up an NGINX container to act as a reverse proxy, including addition of fields in:</li>
  </ul>
  <pre><code>location / {
          resolver 127.0.0.11;
  
          add_header 'Access-Control-Allow-Origin' '*';
          add_header 'Access-Control-Allow-Credentials' 'true';
          add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
          add_header 'Access-Control-Allow-Headers' 'Accept,Content-Type';
  
          set $nextjs nextjs_upstream;
          proxy_pass http://$nextjs;
      }
  </code></pre>
  <ul>
  <li>Added all the required CORS fields in my config files for the server i.e. cors.php file.</li>
  </ul>
  <pre><code>&lt;?php
  
  return [
  
      'paths' =&gt; ['*', 'api/*', 'login', 'logout', 'sanctum/csrf-cookie', 'api/tickets'],
  
      'allowed_methods' =&gt; ['*'],
  
      'allowed_origins' =&gt; ['http://localhost','*'],
  
      'allowed_origins_patterns' =&gt; [],
  
      'allowed_headers' =&gt; ['X-Custom-Header', 'Upgrade-Insecure-Requests','*'],
  
      'exposed_headers' =&gt; [],
  
      'max_age' =&gt; 0,
  
      'supports_credentials' =&gt; true,
  
  ];
  </code></pre>
  <p>Note: This attempt specifically might make sense since localhost in &quot;allowed_origins&quot; refers to that of within the container, but how would we change it to be outside the container? Would it be that container's IP address or something?</p>
  <ul>
  <li>Added additional installations in php Dockerfile: <code>&quot;&amp;&amp; a2enmod headers \ &amp;&amp; sed -ri -e 's/^([ \t]*)(&lt;\/VirtualHost&gt;)/\1\tHeader set Access-Control-Allow-Origin &quot;*&quot;\n\1\2/g' /etc/apache2/sites-available/*.conf&quot;</code></li>
  </ul>
  <p>Interestingly, my login/logout routes don't face the same CORS error which plagues my route: api/tickets, but this is besides the point since It seems like more of an issue with docker containers attempting to communicate.</p>
  <p>Note: localhost has no port since NGINX serves the yarn/npm build.</p>
  <p>Laravel Dockerfile:</p>
  <pre><code># The base image
  FROM php:8.0.2 as base
  # Install system dependencies i.e. Zip , curl:
  RUN apt-get update &amp;&amp; apt-get install -y \
      zip \
      unzip
  
  # Get extensions for PHP &amp; Install Postgres
  RUN apt-get install -y libpq-dev \
      &amp;&amp; docker-php-ext-configure pgsql -with-pgsql=/usr/local/pgsql \
      &amp;&amp; docker-php-ext-install pdo pdo_pgsql pgsql
  # Get the latest composer:
  COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
  
  # Copy everything to the working directory
  COPY . /app
  
  # Set the current working directory
  WORKDIR /app
  
  # Install dependencies for laravel
  COPY composer.json ./
  RUN composer install
  
  # Move the exec file to our workdir and give permissions
  COPY docker-init.sh /usr/local/bin/
  RUN chmod +x /usr/local/bin/docker-init.sh
  
  # Expose internal port which will be mapped
  EXPOSE 8000
  # Run our server
  ENTRYPOINT [ &quot;docker-init.sh&quot; ]
  </code></pre>
  <p>Frontend Dockerfile:</p>
  <pre><code>FROM node:alpine as build
  
  # Set the current working directory
  WORKDIR /usr/app
  
  # Install PM2 globally
  RUN npm install --global pm2
  
  # Get all dependencies
  COPY package*.json ./
  
  # Copy everything to the working directory
  RUN npm install --force --production
  RUN yarn add --dev typescript @types/react
  COPY . ./
  
  RUN npm run build
  
  # Expose internal port which will be mapped
  EXPOSE 3000
  
  USER node
  
  CMD [ &quot;pm2-runtime&quot;, &quot;npm&quot;, &quot;--&quot;, &quot;start&quot; ]
  </code></pre>
  <p>Nginx Dockerfile:</p>
  <pre><code># Prod environment
  FROM nginx:alpine
  
  # Remove any existing config files
  RUN rm /etc/nginx/conf.d/*
  
  # Copy config files
  COPY ./default.conf /etc/nginx/conf.d/
  
  EXPOSE 80
  
  CMD [&quot;nginx&quot;, &quot;-g&quot;, &quot;daemon off;&quot;]
  </code></pre>
  <p>docker-init.sh</p>
  <pre><code>#!/bin/bash
  
  # Exit immediately if a pipeline returns a non zero status
  set -e
  
  # Change to our working directory
  cd /app
  
  # php artisan migrate
  # Serve for all IPv4 addresses:
  php artisan serve --host=0.0.0.0 --port=8000
  </code></pre>
  <p>Thanks for the help lads</p>