无法 Dockerize Vite React-Typescript 项目

Ces*_*iva 8 node.js typescript docker reactjs vite

我正在尝试对 Vite React-Typescript 样板设置进行 dockerize,但无法连接到容器。

安装了 vite-react-typescript 样板:

npm init vite@latest vite-docker-demo -- --template react-ts

Dockerfile

# Declare the base image
FROM node:lts-alpine3.14
# Build step
# 1. copy package.json and package-lock.json to /app dir
RUN mkdir /app
COPY package*.json /app
# 2. Change working directory to newly created app dir
WORKDIR /app
# 3 . Install dependencies
RUN npm ci
# 4. Copy the source code to /app dir
COPY . .
# 5. Expose port 3000 on the container
EXPOSE 3000
# 6. Run the app
CMD ["npm", "run", "dev"]
Run Code Online (Sandbox Code Playgroud)

以分离模式运行 docker 容器并在主机上打开本地开发端口 3000 的命令: docker run -d -p 3000:3000 vite

vite 实例似乎在容器内运行得很好(docker 日志输出):

> vite-docker@0.0.0 dev /app
> vite

Pre-bundling dependencies:
  react
  react-dom
(this will be run only when your dependencies or config have changed)

  vite v2.4.4 dev server running at:

  > Local: http://localhost:3000/
  > Network: use `--host` to expose

  ready in 244ms.
Run Code Online (Sandbox Code Playgroud)

但是,当我导航到http://localhost:3000/Chrome 中时。我看到一个错误指示The connection was reset.

任何解决此问题的帮助将不胜感激!

Ces*_*iva 23

事实证明我需要配置hostlocalhost.

之内vite.config.ts

import { defineConfig } from 'vite'
import reactRefresh from '@vitejs/plugin-react-refresh'

// https://vitejs.dev/config/
export default defineConfig({
  server: {
    host: '0.0.0.0',
    port: 3000,
  },
  plugins: [reactRefresh()],
})
Run Code Online (Sandbox Code Playgroud)

解决问题。