我在尝试在 Docker 容器中托管我的 ionic Web 应用程序时遇到了问题。将端口映射为 8100:8100 应该允许我从主机通过 8100 进行访问,但事实并非如此。下面是我的 Dockerfile 和 Docker-Compose。
FROM node:18-alpine
WORKDIR /app
COPY package*.json /app/
RUN npm install -g @ionic/cli
RUN npm install
COPY / /app/
CMD [ "npm", "start" ]
services:
web:
build: .
ports:
- '8100:8100'
运行 docker ps 会给我这个:
PS D:\Projects\react-asset-client> docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
06762b1eba8f react-asset-client-web "docker-entrypoint.s…" 2 minutes ago Up 2 minutes 0.0.0.0:8100->8100/tcp react-asset-client-web-1
ff0496344563 hello-spring-boot-rest "/__cacert_entrypoin…" 20 minutes ago Up 20 minutes 0.0.0.0:8000->8080/tcp hello-spring-boot-rest-1
a7459e7e3811 postgres:latest "docker-entrypoint.s…" 20 minutes ago Up 20 minutes 0.0.0.0:55560->5432/tcp hello-spring-boot-db-1
如您所见,我已经在运行另外两个服务,并且我可以按预期访问它们。
在寻找修复方法时,我遇到了 有人问的这个问题(在加载页面时重置了 docker run 连接)。他的解决方案是将服务器的主机名从 localhost 设置为 0.0.0.0。
在 ionic --help 中它显示了这个
--host=<host> ................... Use specific host for the dev server (default: localhost)
解决方案:因为在我的 dockerfile 中我正在调用我的
npm start
脚本,我将从以下位置更新该脚本:
"scripts": {
"dev": "vite",
"build": "tsc -p /app/ && vite build",
"preview": "vite preview",
"test.e2e": "cypress run",
"test.unit": "vitest",
"lint": "eslint",
"start": "ionic --version && ionic serve"
}
到
"scripts": {
"dev": "vite",
"build": "tsc -p /app/ && vite build",
"preview": "vite preview",
"test.e2e": "cypress run",
"test.unit": "vitest",
"lint": "eslint",
"start": "ionic --version && ionic serve --host=0.0.0.0"
}
这解决了我的问题