Laravel Docker On Window Volume mounting - laravel

I setup docker on window and setup Laravel project to run on docker, images are created but unfortunately volume is not mounting. Docker Project https://github.com/wariszargardev/Laravel-Nginx-Docker- project is setup on root. I have tried no of solution but unfortunately to setup on window. Same project with no changes working on Mac M2.
docker-compose.yml
version: '3.8'
networks:
laravel-vpn-network:
services:
nginx:
image: nginx:stable-alpine
container_name: nginx
ports:
- "8080:80"
volumes:
- ./:/var/www/html
- ./nginx/conf.d/:/etc/nginx/conf.d/
depends_on:
- php
# - mysql
networks:
- laravel-vpn-network
php:
build:
context: .
dockerfile: Dockerfile
container_name: php
volumes:
- ./:/var/www/html
ports:
- "9000:9000"
networks:
- Laravel-vpn-network
**Docker File **
FROM php:8.0-fpm-alpine
WORKDIR /var/www/html
RUN docker-php-ext-install pdo pdo_mysql
EXPOSE 9000
**
App.conf**
Root of project in nginx directory conf.d directory inside this app.conf file present
server {
listen 80;
index index.php index.html;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /var/www/public;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
location / {
try_files $uri $uri/ /index.php?$query_string;
gzip_static on;
}
}
This simple template if anyone available to guide me?

Related

Problem docker-compose with laravel nginx not working on production

I wrote a docker-compose.yml with nginx, mysql, and an application laravel with a dockerfile.
My docker-compose :
version: "3.7"
services:
app_back:
build:
context: .
dockerfile: Dockerfile
image: dreamy_back
container_name: dreamy-back-app
restart: unless-stopped
working_dir: /var/www/
volumes:
- .:/var/www
networks:
- dreamy
db:
image: mysql
container_name: dreamy-db
restart: unless-stopped
depends_on:
- app_back
environment:
MYSQL_DATABASE: ${DB_DATABASE}
MYSQL_ROOT_PASSWORD: 4ztgeU%
MYSQL_PASSWORD: ${DB_PASSWORD}
MYSQL_USER: ${DB_USERNAME}
networks:
- dreamy
nginx:
image: nginx:1.17-alpine
container_name: dreamy-back-nginx
restart: unless-stopped
ports:
- 8000:80
volumes:
- ./:/var/www
- ./docker-compose/vhosts:/etc/nginx/conf.d
networks:
- dreamy
networks:
dreamy:
driver: bridge
My dockerfile for an laravel application :
I execute composer install inside the container.
FROM php:8.1-fpm
# Install system dependencies
RUN apt-get update && apt-get install -y \
git \
curl \
libpng-dev \
libonig-dev \
libxml2-dev \
zip \
unzip
# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*
# Install PHP extensions
RUN docker-php-ext-install pdo_mysql mbstring exif pcntl bcmath gd
# Get latest Composer
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
# Set working directory
WORKDIR /var/www
My nginx conf :
server {
listen 80;
index index.php index.html;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /var/www/public;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass app_back:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
location / {
try_files $uri $uri/ /index.php?$query_string;
gzip_static on;
}
}
On my laptop it's working but on my vps server I get an error 500.
My docker should be accessible port 8000
My nginx logs
UPDATE - I found the problem, in production the application does not display laravel errors. So it is the default ngynx page that is displayed. The error was the right for the storage file.
Thank you for your help.

Deploying Laravel backend and multiple vue frontend apps on the same server

I have a Laravel and several Vue apps. I'm trying to deploy them on Digital Ocean using docker and nginx.
I've never deployed apps before, so I poorly understand the process. I've already deployed Laravel backend and I am thinking of deploying my vue apps in separate docker containers, but I dont actually understand how should I configure my nginx server.
I've found out I could build my frontend app into the Laravel's public folder, but this approach works with a single Vue app, while I have several.
I'd use some help on how to manage this. Thanks in advance!
Here's my docker-compose.yml(without vue yet) :
version: '3'
services:
nginx:
image: nginx:alpine
container_name: shop_nginx
restart: unless-stopped
ports:
- "80:80"
- "443:443"
volumes:
- ./:/var/www/shop/backend
- ./nginx/conf.d/:/etc/nginx/conf.d/
- ./nginx/ssl/:/etc/nginx/ssl/
networks:
- shop
laravel:
container_name: shop_laravel
restart: unless-stopped
build:
context: .
dockerfile: Dockerfile
volumes:
- '.:/var/www/shop/backend'
networks:
- shop
depends_on:
- pgsql
- redis
pgsql:
container_name: shop_postgres
restart: unless-stopped
image: 'postgres:14'
ports:
- '5432:5432'
environment:
PGPASSWORD: 'shop'
POSTGRES_DB: 'shop'
POSTGRES_USER: 'shop'
POSTGRES_PASSWORD: 'shop'
volumes:
- 'shop-pgsql:/var/lib/postgresql/data'
networks:
- shop
redis:
container_name: shop_redis
restart: unless-stopped
image: 'redis:alpine'
ports:
- '6379:6379'
volumes:
- 'shop-redis:/data'
networks:
- shop
networks:
shop:
driver: bridge
volumes:
shop-pgsql:
driver: local
shop-redis:
driver: local
And nginx configuration:
server {
listen 80;
listen [::]:80;
server_name shop.laravel;
root /var/www/shop/backend/public;
index index.php;
charset utf-8;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass laravel:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
location / {
try_files $uri $uri/ /index.php?$query_string;
gzip_static on;
}
}

Nginx config not working on my docker container

I'm new with dockers.
I'm studying this docker-compose.yml in order to create a this three containers for webserver, workspace for my laravel and the db. And I'm understanding most of it.
It builds just fine but on localhost I get the nginx default page, not my laravel public folder.
I have a app.conf file inside of /nginx/conf.d/
server {
listen 80;
index index.php index.html;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /var/www/public;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass app:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
location / {
try_files $uri $uri/ /index.php?$query_string;
gzip_static on;
}
}
version: '3.2'
services:
#PHP Service
app:
build:
context: .
dockerfile: Dockerfile
image: myapp-workspace
container_name: myapp-workspace
restart: unless-stopped
tty: true
environment:
SERVICE_NAME: app
SERVICE_TAGS: dev
working_dir: "/var/www"
volumes:
- ./:/var/www
networks:
- app-network
#Nginx Service
webserver:
image: nginx:alpine
container_name: myapp-nginx
restart: unless-stopped
tty: true
ports:
- "80:80"
- "443:443"
volumes:
- .:/var/www
- ./nginx/conf.d/:/etc/nginx/conf.d/
networks:
- app-network
#MySQL Service
db:
image: mysql:5.7.22
container_name: myapp-db
restart: unless-stopped
tty: true
ports:
- "3306:3306"
environment:
MYSQL_DATABASE: 'database'
MYSQL_ROOT_PASSWORD: 'password'
MYSQL_PASSWORD: 'password'
MYSQL_USER: 'user'
SERVICE_TAGS: dev
SERVICE_NAME: mysql
volumes:
- ./dbdata:/var/lib/mysql/
- ./mysql/my.cnf:/etc/mysql/my.cnf
networks:
- app-network
#Docker Networks
networks:
app-network:
driver: bridge
Dockerfile
FROM php:7.3-fpm
# Copy composer.lock and composer.json
COPY composer.lock composer.json /var/www/
# Set working directory
WORKDIR /var/www
# Install dependencies
RUN apt-get update && apt-get install -y \
build-essential \
libpng-dev \
libjpeg62-turbo-dev \
libfreetype6-dev \
locales \
zip \
jpegoptim optipng pngquant gifsicle \
vim \
unzip \
git \
curl
# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*
# Install extensions
RUN docker-php-ext-install pdo_mysql
RUN docker-php-ext-configure gd --with-gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ --with-png-dir=/usr/include/
RUN docker-php-ext-install gd
# Install composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
# Add user for laravel application
RUN groupadd -g 1000 www
RUN useradd -u 1000 -ms /bin/bash -g www www
# Copy existing application directory contents
COPY . /var/www
# Copy existing application directory permissions
COPY --chown=www:www . /var/www
# Change current user to www
USER www
# Expose port 9000 and start php-fpm server
EXPOSE 9000
CMD ["php-fpm"]
Found the problem.
Seems that the "app" part on this line "fastcgi_pass app:9000;" on my app.conf should be the name of my workspace container.
So like this it works.
server {
listen 80;
index index.php index.html;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /var/www/public;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass myapp-workspace:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
location / {
try_files $uri $uri/ /index.php?$query_string;
gzip_static on;
}
}

Permissions problem with my first Laravel dev environment with Docker

I'm switching my development environments from virtual machines (with Vagrant and VirtualBox) to containers with Docker. I successfully set-up a simple dev environment for WordPress and now I'm trying to do the same for Laravel.
I'm on Windows 10 Pro, using WSL2 (Ubuntu 20.04 installed).
If I try to on on localhost:8088 (where my local Laravel installation is exposed) I get:
UnexpectedValueException
The stream or file "/var/www/html/storage/logs/laravel.log" could not be opened in append mode: failed to open stream: Permission denied
This is my docker-compose.yml file:
version: '3.3'
services:
nginx:
container_name: 'NGINX_Web_Server'
image: nginx:stable-alpine
depends_on:
- php
- mariadb
ports:
- 8088:80
volumes:
- ./root:/var/www/html
- ./nginx/default.conf:/etc/nginx/conf.d/default.conf
php:
container_name: 'PHP-FPM_7.4'
build:
context: .
dockerfile: Dockerfile
volumes:
- ./root:/var/www/html
ports:
- 9090:9000
mariadb:
container_name: 'MariaDB_SQL_Server'
image: mariadb
restart: unless-stopped
tty: true
volumes:
- ./mariadb_data:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: laravel
MYSQL_USER: ll_user
MYSQL_PASSWORD: ll_password
this is the Dockerfile indicated in docker-compose.yml:
FROM php:fpm-alpine3.12
RUN docker-php-ext-install pdo pdo_mysql
#RUN chown -R www-data:www-data /var/www
and the default.conf file for nginx:
server {
listen 80;
index index.php index.html;
server_name localhost;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /var/www/html/public;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
What's wrong?
Could be solved with:
chown -R www-data:www-data /var/www
chmod -R 755 /var/www/storage
If it does not work for you, try this in the container:
Check: docker-compose exec php ls -al /var/www/storage
I saw xfs instead of www-data
Run: docker-compose exec php chown -R $USER:www-data /var/www/storage
Run: docker-compose exec php chown -R $USER:www-data /var/www/bootstrap/cache
Try again and it must work

Ngnix laravel docker project return error 502 bad gateway

I tried run laravel inside docker container with ngnix.
Directory structure:
File docker-compose.yml:
version: '3'
services:
nginx:
image: nginx:stable-alpine
container_name: nginx
ports:
- "${HOST_PORT}:80"
volumes:
- ../:/var/www/html
- ./etc/nginx/default.conf:/etc/nginx/conf.d/default.conf
depends_on:
- php
php:
build:
context: .
dockerfile: ../Dockerfile
restart: always
container_name: php
volumes:
- "../:/var/www/html"
ports:
- "9000:9000"
mongodb:
image: mongo:latest
container_name: mongodb
ports:
- "27017:27017"
redis:
image: redis:latest
container_name: redis
restart: always
ports:
- "6379:6379"
File Dockerfile:
FROM php:7.4-apache
RUN apt-get update && apt-get install --yes --no-install-recommends \
libssl-dev
RUN docker-php-ext-install pdo pdo_mysql
RUN pecl install mongodb \
&& docker-php-ext-enable mongodb
Result of command docker ps:
When I tried open in browser address http://localhost:8004 then get error:
502 Bad Gateway nginx/1.18.0
Ngnix config file:
server {
listen 80;
index index.php index.html;
server_name localhost;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /var/www/html/public;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
Env vars:
HOST_PORT=8004
HOST_SSL_PORT=3004
# Nginx
NGINX_HOST=localhost
As you're using nginx as your web-server, you don't need to install PHP Apache (FROM php:7.4-apache) in your Dockerfile.
Instead try php:7.4-fpm and make sure nginx accesses that php-fpm correctly (line fastcgi_pass php:9000; in your default.conf file. In your case everything seems configured correctly).
One more thing to note that it's not necessary to expose php-fpm port 9000 to the host machine. As both containers (in your docker-compose.yml) will use the same network, nginx container can directly access php container. You can remove the lines
ports:
- "9000:9000"

Resources