我正在尝试对 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"]
以分离模式运行 docker 容器并在主机上打开本地开发端口 3000 的命令:docker run -d -p 3000:3000 vite
vite 实例似乎在容器内运行得很好(docker 日志输出):
> [email protected] 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.
但是,当我导航到http://localhost:3000/
在 Chrome 中。我看到一个错误表明The connection was reset
.
任何解决此问题的帮助将不胜感激!
事实证明我需要配置host
到除localhost
.
Within 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()],
})
解决问题。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)