Vue3项目Docker化后无法在浏览器访问运行中的容器应用
Vue3应用Docker化后容器正常但浏览器无法访问的解决办法
我尝试通过以下文件将Vue3应用Docker化:
Dockerfile.dev
FROM node:lts-alpine as develop-stage WORKDIR /var/www/html/app COPY package*.json ./ RUN npm i EXPOSE 5173 CMD npm run dev
docker-compose.dev.yml
version: '3.7' services: vue: container_name: vue build: context: . dockerfile: Dockerfile.dev ports: - "5173:5173" volumes: - .:/var/www/html/app - /var/www/html/app/node_modules
执行命令 docker-compose -f Docker-compose.dev.yml up 后,容器运行状态正常,但在浏览器访问 http://localhost:5173/ 时没有任何响应。
问题解决方法
需要在项目的vite.config.ts中添加server.host: true配置,修改后的完整配置如下:
export default defineConfig({ plugins: [ vue(), ], server: { host: true, }, resolve: { alias: { '@': fileURLToPath(new URL('./src', import.meta.url)) } } })
内容的提问来源于stack exchange,提问作者Pooria Sadeghy
相关产品推荐
相关产品推荐

