TheTalker85's avatar

Docker Vite - blocked by CORS policy?

Hi,

I have been watching this docker series and following along using the GitHub repo, i have everything running (i think) other than Vite! When i add the @vite(['resources/css/app.css', 'resources/js/app.js']) to my blade template and have Vite dev running i see the following error in the console.

I am running docker under WSL. How do i fix this or what is wrong? Any help would be great.

https://laracasts.com/series/the-docker-tutorial

https://github.com/aschmelyun/docker-compose-laravel

docker compose run --rm npm run dev
Access to script at 'http://0.0.0.0:5173/resources/js/app.js' from origin 'http://localhost' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

GET http://0.0.0.0:5173/resources/js/app.js net::ERR_FAILED 502 (Bad Gateway)

Access to script at 'http://0.0.0.0:5173/@vite/client' from origin 'http://localhost' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

GET http://0.0.0.0:5173/@vite/client net::ERR_FAILED 502 (Bad Gateway)

Docker compose

version: '3'

networks:
  laravel:


services:
  app:
    build:
      context: ./dockerfiles
      dockerfile: nginx.dockerfile
      args:
        - UID=${UID:-1000}
        - GID=${GID:-1000}
    ports:
      - "80:80"
    volumes:
      - ./src:/var/www/html:delegated
    depends_on:
      - php
      - redis
      - mysql
      - mailhog
    networks:
      - laravel

  mysql:
    image: mariadb:10.6
    restart: unless-stopped
    tty: true
    ports:
      - "3306:3306"
    environment:
      MYSQL_DATABASE: homestead
      MYSQL_USER: homestead
      MYSQL_PASSWORD: secret
      MYSQL_ROOT_PASSWORD: secret
      SERVICE_TAGS: dev
      SERVICE_NAME: mysql
    networks:
      - laravel
    volumes:
      - ./mysql:/var/lib/mysql

  php:
    build:
      context: ./dockerfiles
      dockerfile: php.dockerfile
      args:
        - UID=${UID:-1000}
        - GID=${GID:-1000}
    ports:
      - "9000:9000"
    volumes:
      - ./src:/var/www/html:delegated
    networks:
      - laravel

  redis:
    image: redis:alpine
    restart: unless-stopped
    ports:
      - "6379:6379"
    networks:
      - laravel

  composer:
    build:
      context: ./dockerfiles
      dockerfile: php.dockerfile
      args:
        - UID=${UID:-1000}
        - GID=${GID:-1000}
    volumes:
      - ./src:/var/www/html
    depends_on:
      - php
    entrypoint: [ 'composer', '--ignore-platform-reqs' ]
    networks:
      - laravel

  npm:
    image: node:current-alpine
    volumes:
      - ./src:/var/www/html
    ports:
      - "3000:3000"
      - "3001:3001"
      - "5173:5173"
    working_dir: /var/www/html
    entrypoint: [ 'npm' ]
    networks:
      - laravel

  artisan:
    build:
      context: ./dockerfiles
      dockerfile: php.dockerfile
      args:
        - UID=${UID:-1000}
        - GID=${GID:-1000}
    volumes:
      - ./src:/var/www/html:delegated
    depends_on:
      - mysql
    entrypoint: [ 'php', '/var/www/html/artisan' ]
    networks:
      - laravel

  mailhog:
    image: mailhog/mailhog:latest
    ports:
      - "1025:1025"
      - "8025:8025"
    networks:
      - laravel

Vite config

import { defineConfig } from 'vite';
import laravel from 'laravel-vite-plugin';

export default defineConfig({
    plugins: [
        laravel({
            input: ['resources/css/app.css', 'resources/js/app.js'],
            refresh: true,
        }),
    ],
});

~~
0 likes
4 replies
LaryAI's avatar
Level 58

It looks like you're encountering a CORS issue because Vite is trying to serve your assets from 0.0.0.0:5173, but your Laravel app is being served from a different origin (likely localhost).

To resolve this, you can configure Vite to explicitly set the CORS headers to allow requests from your Laravel app's origin. You can do this by modifying the Vite configuration to include the server property with CORS options.

Here's how you can update your vite.config.js file:

import { defineConfig } from 'vite';
import laravel from 'laravel-vite-plugin';

export default defineConfig({
    server: {
        host: '0.0.0.0',
        port: 5173,
        strictPort: true, // if you want Vite to fail if the port is already in use
        cors: {
            origin: 'http://localhost', // or the specific origin of your Laravel app
            credentials: true,
        },
        hmr: {
            host: 'localhost',
        },
    },
    plugins: [
        laravel({
            input: ['resources/css/app.css', 'resources/js/app.js'],
            refresh: true,
        }),
    ],
});

Make sure to replace 'http://localhost' with the actual origin of your Laravel app if it's different.

Additionally, since you're running Docker under WSL, you might need to ensure that the ports are correctly mapped and accessible from your Windows host. The 0.0.0.0 IP address in Docker should bind to all interfaces, but you might need to access your services using localhost or the specific IP address of your WSL instance from your browser.

Lastly, ensure that your Docker Compose service for npm (which runs Vite) has the correct port mappings. It looks like you've already mapped port 5173 to the host, which is good.

After making these changes, restart your Vite server and try accessing your app again. If you still encounter issues, double-check that the Vite server is running and accessible from your browser, and that there are no network issues between your Docker containers and your host system.

1 like
TheTalker85's avatar

I have tried the above and now see the following errors in the console

GET http://localhost:5173/resources/css/app.css net::ERR_CONNECTION_REFUSED
GET http://localhost:5173/@vite/client net::ERR_CONNECTION_REFUSED
  GET http://localhost:5173/resources/js/app.js net::ERR_CONNECTION_REFUSED
Rhdr0011's avatar
Rhdr0011
Best Answer
Level 5

I ran into the same issue, in your vite.config.js try only adding the server.hmr section:

import { defineConfig } from 'vite';
import laravel from 'laravel-vite-plugin';
import vue from '@vitejs/plugin-vue';

export default defineConfig({
    plugins: [
        laravel({
            input: 'resources/js/app.js',
            refresh: true,
        }),
        vue({
            template: {
                transformAssetUrls: {
                    base: null,
                    includeAbsolute: false,
                },
            },
        }),
    ],
    server: {
        hmr: {
            host: 'localhost',
        },
    },
});

And then make sure you have added your host in package.json

{
    "private": true,
    "type": "module",
    "scripts": {
        "dev": "vite --host 0.0.0.0",  // make sure you host is added here
        "build": "vite build"
    },
    "devDependencies": {
        "@inertiajs/vue3": "^1.0.0",
        "@tailwindcss/forms": "^0.5.2",
        "@tailwindcss/typography": "^0.5.2",
        "@vitejs/plugin-vue": "^4.5.0",
        "autoprefixer": "^10.4.7",
        "axios": "^1.6.4",
        "laravel-vite-plugin": "^1.0.0",
        "postcss": "^8.4.14",
        "tailwindcss": "^3.1.0",
        "vite": "^5.0.0",
        "vue": "^3.2.31"
    }
}

Then run docker-compose run --rm --service-ports npm run dev

1 like
TheTalker85's avatar

That fixed it! Thank you!

I thought i had already tried this but must have had the combo wrong.

Also worth noting that you need to inc the service ports in the command.

Then run docker-compose run --rm --service-ports npm run dev

1 like

Please or to participate in this conversation.