You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 08:52:38