ShahedPHP's avatar

Docker: Laravel NGINX MySQL (Local)

I am running on Mac OS

I am using PHPStorm and Docker. When I update my code file it does it job of updating it in the container too. However, when I am viewing my project on Safari (localhost). The changes do not happen unless I run "npm run build" in my container. Also when I use "npm run dev" localhost comes up with a blank page? Is that meant to happen? How do I dynamically have my work updated with my browser without having the need to manually update it?

0 likes
7 replies
RemiM's avatar

Have you exposed Vite's dev server port in your docker-compose.yml? Also, how is your vite.config.js set up? It might help if you could share some of your configuration files.

ShahedPHP's avatar

@remim thanks for taking your time to reply. I really appreciate it. I'll post my docker-compose.yml and vite.config.js

ShahedPHP's avatar
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,
                },
            },
        }),
    ],
});
RemiM's avatar
RemiM
Best Answer
Level 16

You need to:

  1. Expose Vite’s development server to your host machine.
  2. Configure Vite to accept requests from outside the container.

Step 1: Update docker-compose.yml

Modify the app service to expose Vite’s default port (5173) and ensure it's properly bound to the host.

    app:
		...
        ports:
            - "9000:9000"
            - "5173:5173"  # Expose Vite's port
		...

Step 2: Update vite.config.js

Modify vite.config.js to ensure it's accessible from your host.

...
export default defineConfig({
    server: {
        host: '0.0.0.0', // Listen on all network interfaces (required for Docker)
        watch: {
            usePolling: true, // Ensures file changes are detected inside Docker
        },
        strictPort: true,
        port: 5173, // Ensure this matches the exposed port in docker-compose.yml
        hmr: {
            host: 'localhost', // Ensures HMR works when accessed via localhost
        },
    },
    plugins: [
        laravel({
            input: 'resources/js/app.js',
            refresh: true,
        }),
       ...
    ],
});

How to Start Everything:

# 1. Start containers
docker-compose up --build -d

# 2. Run Vite inside the container
docker exec -it app npm run dev

# 3. Open http://localhost in Safari (HMR should work)

There are more optimized ways to use Docker with Laravel, but given your current setup, this approach is the quickest to get things running.

If you're looking for a more seamless integration, you can explore detailed guides on Laracasts:

You can also find plenty of resources with a quick Google search.

ShahedPHP's avatar

@remim I just want to say you are amazing! For 5 days I have been researching and using ChatGPT to find the solution to the problem. You solved it within seconds. Thank you for your time it means a lot having someone help me out!

Please or to participate in this conversation.