Laravel-echo-server dockerization 问题

Laravel-echo-server dockerization issue

我有一个 laravel 项目,它应该是 docker 容器的包装。我为 PHP、MySQL、Redis 和 Nginx 添加了容器。但是 laravel-echo-server 不起作用。所有连接都失败。

我的docker-compose.yml配置:

version: '3.7'
services:
  redis:
    image: redis:alpine
    container_name: wex_redis
    volumes:
      - redis_data:/data
    command: redis-server --appendonly yes
    networks:
      - wex-network

  app:
    build:
      context: ./
      dockerfile: docker/containers/app/Dockerfile
    working_dir: /app
    container_name: wex_app
    volumes:
      - ./:/app
    environment:
      - DB_PORT=3306
      - DB_HOST=database
    networks:
      - wex-network

  web:
    build:
      context: ./
      dockerfile: docker/containers/web/Dockerfile
    working_dir: /app
    container_name: wex_web
    ports:
      - 8082:80
      - 6001:6001
    networks:
      - wex-network

  database:
    image: mysql:8
    container_name: wex_db
    volumes:
      - dbdata:/var/lib/mysql
    environment:
      - MYSQL_DATABASE=${DB_DATABASE:-wex}
      - MYSQL_ROOT_PASSWORD=root
    ports:
      - 33062:3306
    command: --default-authentication-plugin=mysql_native_password
    networks:
      - wex-network

networks:
  wex-network:
    driver: bridge

volumes:
  dbdata:
  redis_data:

"web" 容器需要安装 npm 依赖项并设置 nginx。 Web 容器的 Dockerfile:

FROM node:10-alpine as build

WORKDIR /app

# Load dependencies
COPY ./package*.json ./yarn.lock ./
COPY ./webpack.mix.js ./
RUN yarn install

COPY ./resources/assets /app/resources/assets
COPY ./public/assets /app/public/assets

# Build
RUN yarn run production

# Laravel-echo-server
RUN yarn global add --prod --no-lockfile laravel-echo-server \
    && yarn cache clean

EXPOSE 6001

CMD ["laravel-echo-server", "start"]

# NGINX
FROM nginx:alpine

EXPOSE 80

COPY ./docker/containers/web/nginx.conf /etc/nginx/conf.d/default.conf
COPY ./public /app/public
COPY --from=build /app/public/assets /app/public/assets

laravel-echo-server 的设置:

{
  "authHost": "http://localhost",
  "authEndpoint": "/broadcasting/auth",
  "clients": [],
  "database": "redis",
  "databaseConfig": {
    "redis": {},
    "sqlite": {
      "databasePath": "/database/laravel-echo-server.sqlite"
    }
  },
  "devMode": true,
  "host": null,
  "port": "6001",
  "protocol": "http",
  "socketio": {},
  "sslCertPath": "",
  "sslKeyPath": "",
  "sslCertChainPath": "",
  "sslPassphrase": "",
  "subscribers": {
    "http": true,
    "redis": true
  },
  "apiOriginAllow": {
    "allowCors": false,
    "allowOrigin": "",
    "allowMethods": "",
    "allowHeaders": ""
  }
}

从 js 连接到 laravel-echo-server:

import Echo from 'laravel-echo'

window.io = require('socket.io-client');

window.Echo = new Echo({
    broadcaster: 'socket.io',
    host: window.location.hostname + ':6001'
});

"docker ps" 命令:

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                                          NAMES
dddc2f5867b6        wex_app             "/var/entrypoint.sh …"   43 minutes ago      Up 43 minutes       9000/tcp                                       wex_app
5fb70cf7da97        wex_web             "nginx -g 'daemon of…"   19 hours ago        Up 43 minutes       0.0.0.0:6001->6001/tcp, 0.0.0.0:8082->80/tcp   wex_web
f2f331bf37e0        redis:alpine        "docker-entrypoint.s…"   7 days ago          Up 43 minutes       6379/tcp                                       wex_redis
224e2af8c151        mysql:8             "docker-entrypoint.s…"   7 days ago          Up 43 minutes       33060/tcp, 0.0.0.0:33062->3306/tcp             wex_db

请问我什么时候做错了?为什么与 laravel-echo 服务器的所有连接都不起作用?

看看你的 Dockerfile,它使用 multi-stage builds

这里有2个阶段,CMD ["laravel-echo-server", "start"]就在第一阶段。但是 multi-stage builds 的目的是生成具有最后阶段的最终图像。所有前一阶段都只是中间阶段,以提供最后阶段的东西。

所以,当你的 docker-compose 运行 web service 时,它只是使用最终图像来启动一个容器,所以容器只会调用 ENTRYPOINT 或 [=最后阶段的 15=] 将不会开始 laravel-echo-server