Laravel Websocket with Docker and Nginx Proxy Manager in Docker

Updated: Jan 27, 2025

Laravel Websocket with Docker and Nginx Proxy Manager in Docker

To set up Laravel Websockets with Docker and Nginx Proxy Manager in Docker, you'll need to follow these steps:

  1. Create a new Laravel project:

First, create a new Laravel project using Composer if you haven't already.

composer create --prefer-dist laravel/laravel project-name
  1. Install Laravel Echo and Laravel Echo Server:

Next, install Laravel Echo and Laravel Echo Server using NPM.

cd project-name
npm install laravel-echo echo
  1. Configure Laravel Echo:

Create a new file broadcasting.php in the config directory and update the broadcasters array to include pusher and echo.

return [
    'broadcasters' => [
        'pusher' => [
            'driver' => 'pusher',
            'key' => env('PUSHER_APP_KEY'),
            'secret' => env('PUSHER_APP_SECRET'),
            'app_id' => env('PUSHER_APP_ID'),
            'options' => [
                'cluster' => env('PUSHER_APP_CLUSTER'),
                'encrypted' => false,
            ],
        ],

        'echo' => [
            'driver' => 'laravel-echo',
            'key' => null,
        ],
    ],

    'connections' => [
        'pusher' => [
            'driver' => 'pusher',
            'key' => env('PUSHER_APP_KEY'),
            'secret' => env('PUSHER_APP_SECRET'),
            'app_id' => env('PUSHER_APP_ID'),
            'options' => [
                'cluster' => env('PUSHER_APP_CLUSTER'),
                'encrypted' => false,
            ],
        ],

        'echo' => [
            'driver' => 'laravel-echo',
            'key' => null,
        ],
    ],
];
  1. Install Laravel Echo Server:

Install Laravel Echo Server as a global package using NPM.

npm install -g laravel-echo-server
  1. Create a new Dockerfile:

Create a new Dockerfile in the root directory of your project.

touch Dockerfile

Update the Dockerfile to install necessary dependencies, copy Laravel files, and run Laravel Echo Server.

FROM php:8.0-fpm

# Install extensions
RUN docker-php-ext-install pdo pdo_mysql mbstring zip unzip gd xml xmlrpc curl json

# Set timezone
RUN echo "Europe/London" >> /etc/timezone && dpkg-reconfigure --fresh --no-upgrade tzdata

# Install composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

# Copy Laravel project files
COPY . /var/www/html

# Set Laravel permissions
RUN chown -R www-data:www-data /var/www/html

# Install Laravel dependencies
RUN composer install --no-autoload --no-scripts

# Install Laravel Echo Server
RUN npm install -g laravel-echo-server

# Copy Laravel Echo Server configuration file
COPY .env /var/www/html/.env
RUN php artisan config:cache

# Set Laravel permissions for storage
RUN chmod -R 775 /var/www/html/storage

# Set Laravel permissions for bootstrap cache
RUN chmod -R 775 /var/www/html/bootstrap/cache

# Set Laravel permissions for logs
RUN chmod -R 775 /var/www/html/logs

# Set Laravel permissions for public files
RUN chmod -R 775 /var/www/html/public

# Set Laravel permissions for node_modules
RUN chmod -R 775 /var/www/html/node_modules

# Set Laravel permissions for broadcasting files
RUN chmod -R 775 /var/www/html/broadcasting

# Set Laravel permissions for storage/framework/sessions
RUN chmod -R 775 /var/www/html/storage/framework/sessions

# Set Laravel permissions for storage/framework/views
RUN chmod -R 775 /var/www/html/storage/framework/views

# Set Laravel permissions for storage/framework/cache
RUN chmod -R 775 /var/www/html/storage/framework/cache

# Set Laravel permissions for storage/framework/sessions
RUN chmod -R 775 /var/www/html/storage/framework/sessions

# Set Laravel permissions for storage/app/public
RUN chmod -R 775 /var/www/html/storage/app/public

# Set Laravel permissions for broadcasting files
RUN chmod -R 775 /var/www/html/broadcasting

# Set Laravel permissions for node_modules
RUN chmod -R 775 /var/www/html/node_modules

# Set Laravel permissions for public files
RUN chmod -R 775 /var/www/html/public

# Set Laravel permissions for storage
RUN chmod -R 775 /var/www/html/storage

# Set Laravel permissions for logs
RUN chmod -R 775 /var/www/html/logs

# Set Laravel permissions for bootstrap cache
RUN chmod -R 775 /var/www/html/bootstrap/cache

# Set working directory
WORKDIR /var/www/html

# Expose Laravel ports
EXPOSE 8000 80

# Start Laravel Echo Server
CMD ["laravel-echo-server", "init"] && npm run develop
  1. Create a new Docker Compose file:

Create a new Docker Compose file in the root directory of your project.

touch docker-compose.yml

Update the Docker Compose file to define services and networks.

version: '3.8'

services:
  app:
    build: .
    ports:
      - "80:80"
      - "8000:8000"
      - "6001:6001" # Laravel Echo Server port
    volumes:
      - .:/var/www/html
    networks:
      - app-network
    depends_on:
      - db
      - redis
    environment:
      - DB_DATABASE=laravel
      - DB_USERNAME=laravel
      - DB_PASSWORD=laravel
      - REDIS_URL=redis://redis:6379
      - APP_URL=http://localhost
      - APP_ENV=production
    command: php artisan serve --host=0.0.0.0 --port=80 --env=production

  db:
    image: mysql:8.0
    volumes:
      - db_data:/var/lib/mysql
    environment:
      - MYSQL_ROOT_PASSWORD=laravel
      - MYSQL_DATABASE=laravel
      - MYSQL_USER=laravel
      - MYSQL_PASSWORD=laravel
    networks:
      - app-network

  redis:
    image: redis:alpine
    volumes:
      - redis_data:/var/data/redis
    networks:
      - app-network

networks:
  app-network:
  1. Create Docker volumes:

Create Docker volumes for database and Redis data.

mkdir db_data redis_data
  1. Build and run Docker containers:

Build and run Docker containers using the Docker Compose file.

docker-compose up -d
  1. Configure Nginx Proxy Manager:

Install Nginx Proxy Manager in a separate Docker container and configure it to proxy Laravel Websocket traffic to the Laravel Echo Server container.

Follow