I have a vite+vue3 project that I'm trying to dockerize it. When I run docker-compose up -d everything is right, except hot reloading after a change. What is the problem?
vite.config.ts:
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import path from "path"
import tailwind from "tailwindcss"
import autoprefixer from "autoprefixer"
export default defineConfig({
css: {
postcss: {
plugins: [tailwind(), autoprefixer()],
},
},
plugins: [vue()],
resolve: {
alias: {
"@": path.resolve(__dirname, "./src"),
},
}
})
Dockerfile:
FROM node:16-alpine as builder
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build
FROM nginx:alpine as production-build
COPY nginx.conf /etc/nginx/nginx.conf
RUN rm -rf /usr/share/nginx/html/*
COPY --from=builder /dist /usr/share/nginx/html
EXPOSE 7040
ENTRYPOINT ["nginx", "-g", "daemon off;"]
docker-compose.yml:
services:
web:
build:
context: .
dockerfile: Dockerfile
ports:
- "7040:7040"
If you want to have hot reloading then you have (at least!!) two options.
Assume that the project layout looks something like this:
In both cases this is what
app/Dockerfilelooks like:Since you are wanting hot reloading the Docker container will be running a development server.
Option 1:
This will simply build and run the container, mapping container port 5173 to port 3000 on the host. Open http://127.0.0.1:3000/ in your browser. This will give you true hot reload in the sense that modifications to the code will immediately reflect in the browser.
Option 2
If you want to layer NGINX on top of that (probably not necessary for a development setup?):
With the following
nginx.conf:This will serve the site at http://127.0.0.1/. You need NGINX to also relay the Web Socket connection to the app to ensure that the browser is automatically refreshed when content changes.