I am trying to run a React project using Vite inside a Docker container with Code-Server. The app is proxied through Code-Server at http://localhost:8080/proxy/5173/, but I'm facing issues loading the app in the browser.
Here is my Dockerfile:
FROM node:latest
ENV DEBIAN_FRONTEND=noninteractive
RUN apt update
RUN apt install -y curl
WORKDIR /workspace
RUN curl -fsSL https://code-server.dev/install.sh | sh
COPY ./init.sh .
CMD ["bash", "init.sh"]
The init.sh script initializes the project:
#!/bin/bash
# Check if FRAMEWORK is set
if [ -z "$FRAMEWORK" ]; then
echo "FRAMEWORK is not set"
exit 1
fi
# Check if PROJECT is set
if [ -z "$PROJECT" ]; then
echo "PROJECT is not set"
exit 1
fi
# Create project based on FRAMEWORK
if [ "$FRAMEWORK" = "react" ]; then
npm create vite@latest "$PROJECT" -- --template react --yes
elif [ "$FRAMEWORK" = "vue" ]; then
npm create vite@latest "$PROJECT" -- --template vue --yes
else
echo "Unknown framework: $FRAMEWORK"
exit 1
fi
# Install dependencies
cd "$PROJECT" && npm install
# Start Code-Server
code-server --host 0.0.0.0
I run the container with the following command:
docker run -p 8080:8080 -e FRAMEWORK="react" -e PROJECT="my-app" -e PASSWORD="codepwd" vite-node-portos
Vite Configuration
Initially, I used this vite.config.js:
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
export default defineConfig({
plugins: [react()],
})
To address the error connect ECONNREFUSED 0.0.0.0:5172, I modified the config to explicitly bind to 0.0.0.0:
error image
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
export default defineConfig({
plugins: [react()],
server: {
host: '0.0.0.0',
port: 5173,
},
})
However, this caused errors in the browser console:
Loading failed for the module with source “http://localhost:8080/proxy/5173/src/main.jsx”.
Loading failed for the module with source “http://localhost:8080/proxy/5173/@react-refresh”.
Loading failed for the module with source “http://localhost:8080/proxy/5173/@vite/client”.
I then updated the vite.config.js to include a base configuration:
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
export default defineConfig({
plugins: [react()],
server: {
host: '0.0.0.0',
port: 5173,
},
base: '/proxy/5173/',
})
However, this caused the same errors in the browser console:
Loading failed for the module with source “http://localhost:8080/proxy/5173/src/main.jsx”.
Loading failed for the module with source “http://localhost:8080/proxy/5173/@react-refresh”.
Loading failed for the module with source “http://localhost:8080/proxy/5173/@vite/client”.
How can I resolve the module loading errors and correctly serve my Vite app through the Code-Server proxy?
In tools like Project IDX or GitHub Codespaces, Vite projects work without requiring edits to the vite.config.js file to handle proxy paths. Is there a similar way to resolve the proxy issue with Code-Server, so I don't need to modify the Vite config directly?