Laravel Docker #11 - Setup Redis for Queue & Cache in Laravel
Laravel is a powerful PHP framework that makes web development easy and fun. When you use Docker with Laravel, you can create apps that work the same way on any computer. Redis is a fast database that helps your Laravel app run faster by storing data in memory. In this tutorial, we will learn how to setup Redis with Laravel using Docker.
Docker helps us run our Laravel app in containers. This means we can have everything our app needs in one place. Redis will help us handle background jobs and cache data to make our app super fast. Let's start building our Laravel app with Redis support.
Step 1: Create Laravel Project
First, we need to create a new Laravel project. Run this command in your terminal:
laravel new my-app
This command will create a new Laravel project called "my-app" with all the files you need.
Step 2: Create Dockerfile
Dockerfile
FROM php:8.3-fpm
WORKDIR /var/www/html
RUN apt-get update & apt-get install -y \
libzip-dev unzip curl supervisor nginx \
& docker-php-ext-install zip pdo pdo_mysql
RUN curl -fsSL https://deb.nodesource.com/setup_20.x | bash - \
& apt-get install -y nodejs \
& npm install -g npm@latest
RUN pecl install redis \
& docker-php-ext-enable redis
# Install Composer
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
COPY . /var/www/html
# Remove default nginx configs
RUN rm -f /etc/nginx/conf.d/default.conf /etc/nginx/sites-enabled/default
# Copy our custom nginx config
COPY ./.docker/nginx/default.conf /etc/nginx/conf.d/default.conf
RUN sed -i 's|pid /run/nginx.pid;|pid /tmp/nginx.pid;|' /etc/nginx/nginx.conf
RUN sed -i 's|^user .*;|# user www-data;|' /etc/nginx/nginx.conf
COPY ./.docker/supervisord.conf /etc/supervisor/conf.d/supervisord.conf
# Make nginx temp dirs writable for www-data
RUN mkdir -p /var/lib/nginx /var/lib/nginx/body /var/lib/nginx/fastcgi \
& chown -R www-data:www-data /var/lib/nginx
RUN composer install
RUN npm install & npm run build
# Force PHP-FPM to listen on TCP so nginx can talk to it
RUN echo "listen = 9000" >> /usr/local/etc/php-fpm.d/zz-docker.conf
RUN chown -R www-data:www-data /var/www/html
RUN chown -R www-data:www-data /var/www/html/storage /var/www/html/bootstrap/cache
RUN chmod -R 775 /var/www/html/storage /var/www/html/bootstrap/cache
# Nginx listens on port 80
EXPOSE 80
CMD ["supervisord", "-c", "/etc/supervisor/conf.d/supervisord.conf"]
Step 3: Create NGINX Config File
.docker/apache/default.conf
server {
listen 80;
index index.php index.html;
root /var/www/html/public;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass web:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
}
location ~ /\.ht {
deny all;
}
}
Step 4: Create docker-compose.yml
docker-compose.yml
services:
web:
build:
context: .
dockerfile: Dockerfile
image: my-laravel-app
volumes:
- .:/var/www/html
- /var/www/html/storage
- /var/www/html/bootstrap/cache
working_dir: /var/www/html
user: "www-data"
ports:
- "8080:80"
depends_on:
- db
- redis
db:
image: mysql:8.0
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: laravel
MYSQL_USER: laravel
MYSQL_PASSWORD: secret
ports:
- "3307:3306"
volumes:
- dbdata:/var/lib/mysql
redis:
image: redis:7-alpine
container_name: laravel-redis
ports:
- "6379:6379"
volumes:
- redis_data:/data
volumes:
dbdata:
redis_data:
Step 4: Create supervisord.conf
.docker/supervisord.conf
[supervisord]
nodaemon=true
[program:php-fpm]
command=php-fpm
autostart=true
autorestart=true
priority=5
[program:nginx]
command=nginx -g "daemon off;"
autostart=true
autorestart=true
priority=10
[program:laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /var/www/html/artisan queue:work redis --tries=2
autostart=true
autorestart=true
user=www-data
numprocs=1
redirect_stderr=true
stdout_logfile=/var/www/html/storage/logs/worker.log
Run Laravel App:
All the required steps have been done, now you have to type the given below command and hit enter to run the Laravel app:
php artisan serve
Now, Go to your web browser, type the given URL and view the app output:
http://localhost:8080
Now you can use your Laravel app with Redis support for fast caching and queue processing.