Nginx config not working on my docker container - laravel

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;
}
}

Related

Laravel Docker On Window Volume mounting

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?

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.

NextJS + Laravel + NGinx + Docker: Cant call API in getServerSideProps

I have a backend API written in Laravel, a frontend app written in NextJS and these are containerized with Docker using NGinx. Here is my docker-compose.yml file:
version: '3.9'
services:
# frontend nextjs app
nextjs:
build: ./hike-frontend
container_name: nextjs
volumes:
- ./hike-frontend:/usr/app
- /app/node_modules
- /app/.next
restart: always
networks:
- app-network
# API Laravel app
laravel:
build:
context: ./hike-backend
dockerfile: Dockerfile
image: digitalocean.com/php
container_name: laravel
restart: unless-stopped
tty: true
environment:
SERVICE_NAME: laravel
SERVICE_TAGS: dev
working_dir: /var/www
volumes:
- ./hike-backend:/var/www
- ./php/local.ini:/usr/local/etc/php/conf.d/local.ini
networks:
- app-network
expose:
- "9000"
# Nginx Service
nginx:
image: nginx:alpine
container_name: nginx
restart: unless-stopped
tty: true
ports:
- "80:80"
- "443:443"
expose:
- "80"
- "443"
volumes:
- ./hike-backend:/var/www
- ./nginx/conf.d/:/etc/nginx/conf.d/
networks:
- app-network
#Docker Networks
networks:
app-network:
driver: bridge
Here is my dockerfile for the next app:
# Base on offical Node.js Alpine image
FROM node:alpine
# Set working directory
WORKDIR /usr/app
# Install PM2 to automatically restart server if it crashes
RUN npm install --global pm2
# install react globally
RUN npm install --global react react-dom
# Copy package.json and package-lock.json before other files
# Utilise Docker cache to save re-installing dependencies if unchanged
COPY ./package*.json ./
# Install dependencies
RUN npm install --production
# Copy all files
COPY ./ ./
# Build app
RUN npm run build
# Expose the listening port
EXPOSE 3000
# Run container as non-root (unprivileged) user
# The node user is provided in the Node.js Alpine base image
USER node
# Run npm start script with PM2 when container starts
CMD [ "pm2-runtime", "npm", "--", "run", "dev" ]
And here is my dockerfile for the Laravel app:
FROM php:7.4-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 \
libzip-dev \
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 zip exif pcntl
#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"]
And here is my Nginx config:
upstream nextjs_upstream {
server nextjs:3000;
}
server {
listen 80;
server_name local.hike.com;
server_tokens off;
gzip on;
gzip_proxied any;
gzip_comp_level 4;
gzip_types text/css application/javascript image/svg+xml;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
location / {
proxy_pass http://nextjs_upstream;
}
}
server {
listen 80;
server_name local.api.hike.com;
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 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;
}
}
This all works fine I can access both sites in my browser at local.hike.com or local.api.hike.com and ajax requests on the client side work fine as well. However I can't connect to the Laravel app from the next app inside getServerSideProps like so:
export async function getServerSideProps(context) {
const eventDetails = await Axios.get('http://laravel/api/events/1')
.then(result => {
console.log('result', result);
return result.data.data
})
.catch(error => {
console.log('error', error);
return {}
});
}
It just returns:
Error: connect ECONNREFUSED 172.22.0.4:80
I can access local.api.hike.com/api/events/1 no problem in my browser or from an ajax request in the next app but just not in getServerSideProps.

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