Laravel-echo-server dockerization问题

时间:2019-07-04 20:43:14

标签: laravel docker redis socket.io laravel-echo

我有一个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服务器的连接都无法正常工作?

1 个答案:

答案 0 :(得分:0)

使用multi-stage builds查找您的Dockerfile

这里有两个阶段,CMD ["laravel-echo-server", "start"]就在第一阶段。但是多阶段构建的目的是在最后阶段生成最终图像。所有前一阶段只是中间阶段,只能承受最后阶段的负担。

因此,当您的docker-compose运行web service时,它仅使用最终映像启动一个容器,因此该容器将只调用上一个阶段的ENTRYPOINTCMD不会以laravel-echo-server开头。