Laravel-echo-server 码头化问题

Posted

技术标签:

【中文标题】Laravel-echo-server 码头化问题【英文标题】:Laravel-echo-server dockerization issue 【发布时间】:2019-11-15 13:20:17 【问题描述】:

我有一个 laravel 项目,它应该是 docker 容器的包装。我为 phpmysql、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 服务器的所有连接都不起作用?

【问题讨论】:

【参考方案1】:

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

这里有两个阶段,CMD ["laravel-echo-server", "start"] 只是在第一阶段。但是多阶段构建的目的是生成具有最后阶段的最终图像。之前的所有阶段都只是中间阶段,可以负担到最后阶段的东西。

所以,当你的docker-compose 运行web service 时,它只是使用最终的图像来启动一个容器,所以容器只会调用最后阶段的ENTRYPOINTCMD,而不会启动laravel-echo-server

【讨论】:

对于 laravel-echo-server 是否需要创建单独的容器? 如果你需要这个服务,我猜你需要,最好是一个容器做一件事。如果你不这样做,你也可以像这样在其他容器中启动它:docs.docker.com/config/containers/multi-service_container 完全取决于你。反正你目前实现没有启动这个服务。

以上是关于Laravel-echo-server 码头化问题的主要内容,如果未能解决你的问题,请参考以下文章

有没有更有效的方法来码头化 Luarocks?

sh HTOP码头化

laravel-echo-server 404 尝试进行身份验证时

Laravel 事件广播没有被拾取 laravel-echo-server

Laravel-echo-server 无法使用 https 协议启动生产

laravel-echo-server 用户未加入,未订阅套接字服务器