如何将vite js主机暴露给外部docker

Vit*_*one 2 laravel docker vite

从 Laravel 版本 8 升级到 9 时,我是 vite js 的新手。我正在使用 vite js 为 Laravel 9 项目构建 docker。有一个问题:我无法从 Docker 容器中公开资源主机。它仍然在 docker 容器内部工作。有什么建议吗?谢谢。

这是我的 docker-compose 文件

version: "3.9"

services:
  nginx:
    image: nginx:1.23-alpine
    ports:
      - 80:80
    mem_limit: "512M"
    volumes:
      - type: bind
        source: ./api
        target: /usr/share/nginx/html/api
      - type: bind
        source: ./docker/nginx/dev/default.conf
        target: /etc/nginx/conf.d/default.conf

  php:
    platform: linux/amd64
    build:
      context: .
      dockerfile: ./docker/php/dev/Dockerfile
    mem_limit: "512M"
    volumes:
      - type: bind
        source: ./api
        target: /usr/share/nginx/html/api

  oracle:
    platform: linux/amd64
    image: container-registry.oracle.com/database/express:21.3.0-xe
    ports:
      - 1521:1521
      # - 5500:5500
    volumes:
      - type: volume
        source: oracle
        target: /opt/oracle/oradata

volumes:
  oracle:
Run Code Online (Sandbox Code Playgroud)

Vit*_*one 7

我想通了问题。导致vite不将主机暴露给网络。我的解决方案是:

  1. 编辑文件package.json
"scripts": {
  "dev": "vite --host",
  "build": "vite build"
}
Run Code Online (Sandbox Code Playgroud)
  1. 在 docker-compose.yml 文件中公开 5173 端口
php:
    platform: linux/amd64
    build:
      context: .
      dockerfile: ./docker/php/dev/Dockerfile
    mem_limit: "512M"
    ports:
      - 5173:5173
    volumes:
      - type: bind
        source: ./api
        target: /usr/share/nginx/html/api
Run Code Online (Sandbox Code Playgroud)

  • 这个对我有用。多谢。 (2认同)