Certainly! Running Laravel with InertiaJS and Vue in a Docker container is quite feasible. Below is a basic setup to get you started. This setup assumes you have Docker and Docker Compose installed on your system.
First, you'll need a Dockerfile for your application. Here's an example Dockerfile that sets up PHP with the necessary extensions for Laravel, as well as Composer for dependency management:
FROM php:8.1-fpm-alpine
# Install system dependencies
RUN apk add --no-cache git curl libpng libpng-dev libjpeg-turbo-dev libwebp-dev zlib-dev libxpm-dev gd-dev zip libzip-dev
# Install PHP extensions
RUN docker-php-ext-install pdo pdo_mysql gd zip exif pcntl
# Get latest Composer
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
# Set working directory
WORKDIR /var/www
# Copy existing application directory contents
COPY . /var/www
# Copy existing application directory permissions
COPY --chown=www-data:www-data . /var/www
# Expose port 9000 and start php-fpm server
EXPOSE 9000
CMD ["php-fpm"]
Next, you'll need a docker-compose.yml file to define your services:
version: '3.8'
services:
app:
build:
context: .
dockerfile: Dockerfile
image: laravel-inertia-app
container_name: laravel-inertia-app
restart: unless-stopped
working_dir: /var/www
volumes:
- ./:/var/www
- ./php/local.ini:/usr/local/etc/php/conf.d/local.ini
networks:
- laravelnet
environment:
- "DB_HOST=db"
- "DB_PORT=3306"
- "DB_DATABASE=laravel"
- "DB_USERNAME=root"
- "DB_PASSWORD=root"
db:
image: mysql:5.7
container_name: laravel-inertia-db
restart: unless-stopped
environment:
MYSQL_DATABASE: laravel
MYSQL_ROOT_PASSWORD: root
networks:
- laravelnet
volumes:
- dbdata:/var/lib/mysql
nginx:
image: nginx:alpine
container_name: laravel-inertia-nginx
restart: unless-stopped
ports:
- "8080:80"
volumes:
- ./:/var/www
- ./nginx/conf.d:/etc/nginx/conf.d/
networks:
- laravelnet
networks:
laravelnet:
driver: bridge
volumes:
dbdata:
driver: local
You'll also need a default Nginx configuration file for your Laravel app. Create a nginx/conf.d/app.conf file with the following content:
server {
listen 80;
server_name localhost;
root /var/www/public;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-Content-Type-Options "nosniff";
index index.php index.html index.htm;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_pass app:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.(?!well-known).* {
deny all;
}
}
Regarding InertiaJS, you don't need to discard it. You can integrate it with Laravel and Vue as you would normally do without Docker. The Docker setup is just for the environment; it doesn't affect how you write your Laravel or Vue code.
For the front-end assets, you can use Laravel Mix to compile your Vue components. You might need to install Node.js and npm in your Docker container or use a separate container for asset compilation.
To add Node.js to your Docker container, you can modify your Dockerfile to include Node.js installation commands:
# Install Node.js (and npm)
RUN apk add --update nodejs npm
After setting up your Docker environment, you can run docker-compose up -d to start your containers. Your Laravel application should now be accessible at http://localhost:8080, and you should be able to use InertiaJS with Vue as intended.
Remember to run composer install and npm install to install your PHP and JavaScript dependencies, and use npm run dev or npm run production to compile your assets.
This setup should give you a good starting point. You can customize it further based on your specific requirements.