After docker compose localhost:8000 not open page in browser - php

Hello i'm newbie in docker. I have project on laravel 9 with node version 12.14.0,PostgreSql 10,PHP 8.1.2
This my git repository:https://github.com/Daniil1996-vrn/DEVJuniorPHP/tree/main/DEVJuniorPHP
I create docker file, webserver ngnix conf file (but when i'm creating project i usr artisan server) on this repository:https://github.com/othyn/docker-compose-laravel#running-attached
This is my docker-compose.yml:
version: "3.7"
networks:
laravel:
volumes:
database:
services:
database:
image: postgres:10
container_name: postgres
restart: "no"
volumes:
- .:/var/lib/postgresql/data
networks:
- laravel
ports:
- 5432:5432
environment:
POSTGRES_USER: "postgres"
POSTGRES_PASSWORD: "admin1234"
POSTGRES_DB: "DEVJuniorPHP"
composer:
image: composer:latest
container_name: composer
volumes:
- ./:/app
working_dir: /app
command: composer install
node:
image: node:12
container_name: node
volumes:
- ./:/app
working_dir: /app
command: npm install
app:
container_name: app
restart: "no"
volumes:
- ./:/var/www
networks:
- laravel
depends_on:
- composer
- node
build:
context: .
dockerfile: ./docker/app/dockerfile
command: php-fpm
webserver:
image: nginx:stable
container_name: webserver
restart: "no"
volumes:
- ./:/var/www
- ./docker/webserver/nginx.conf/
networks:
- laravel
ports:
- 8000:8000
depends_on:
- database
- app
Docker File:
FROM php:8.1.4-fpm-alpine3.14
# Update package manager ready for deps to be installed
RUN apk update
# Set the working directory of the container to the hosted directory
WORKDIR /var/www
nginx.conf:
server {
listen 8000;
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/public;
location / {
try_files $uri $uri/ /index.php?$query_string;
gzip_static on;
}
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;
# Uncomment to extend nginx's max timeout to 1 hour
# fastcgi_read_timeout 3600;
}
}
When i run command in terminal Visual Studio code docker-compose up -d i next have messagess in terminal:
Starting node ... done
Starting composer ... done
Starting postgres ...
Starting postgres ... done
Recreating webserver ... done
PS D:\DEVJuniorPHP\DEVJuniorPHP> docker-compose up -d
Starting node ... done
Starting postgres ...
Starting postgres ... done
app is up-to-date
webserver is up-to-date
But whene i go to page localhost:8000 in browser i see the message:Can't access site
Please help me resolve this problem

Error is that the nginx vhost is pointing to the wrong folder.
You didn't map the nginx.conf volume into the container volume so it doesn't really find the path to the application.
In the volume part of the webserver service, you must put the path of the vhost container:
- ./docker/webserver/nginx.conf:/etc/nginx/nginx.conf
Get back to me if it's good !

Related

Getting RuntimeException: A facade root has not been set. in /var/www/html/vendor/.../Illuminate/Support/Facades/Facade.php

I am trying to setup the PHP, MYSQL, NGINX using Docker . I followed the udemy tutorial by Maximillian and
my kept all my docker files in a dockerfiles folder.
The php.dockerfile is as follows:
FROM php:8.0-fpm-alpine
WORKDIR /var/www/html
COPY src .
RUN docker-php-ext-install pdo pdo_mysql
My nginx.dockerfile is :
FROM nginx:stable-alpine
WORKDIR /etc/nginx/conf.d
COPY nginx/nginx.conf .
RUN mv nginx.conf default.conf
WORKDIR /var/www/html
COPY src .
And composer file is:
FROM composer:latest
WORKDIR /var/www/html
ENTRYPOINT [ "composer" ]
The docker-compose.yaml file is :
version: '2.2'
services:
server:
# image: 'nginx:stable-alpine'
build:
context: .
dockerfile: dockerfiles/nginx.dockerfile
ports:
- '8000:80'
volumes:
- ./src:/var/www/html
- ./nginx/nginx.conf:/etc/nginx/conf.d/default.conf:ro
# depends_on:
# - php
# - mysql
php:
build:
context: .
dockerfile: dockerfiles/php.dockerfile
volumes:
- ./src:/var/www/html:delegated
mysql:
image: mysql:5.7
env_file:
- ./env/mysql.env
composer:
build:
context: ./dockerfiles
dockerfile: composer.dockerfile
volumes:
- ./src:/var/www/html
artisan:
build:
context: .
dockerfile: dockerfiles/php.dockerfile
volumes:
- ./src:/var/www/html
entrypoint: ['php', '/var/www/html/artisan']
npm:
image: node:14
working_dir: /var/www/html
entrypoint: ['npm']
volumes:
- ./src:/var/www/html
nginx.conf file is :
server {
listen 80;
index index.php index.html;
server_name localhost;
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;
}
}
But When I run the following command :
docker-compose up --build server php mysql
After the command :
docker-compose run --rm composer create-project --prefer-dist laravel/laravel .
I get this Facade error . I went through all the Questions in stack over flow that has this error but I couldn't find the resolution even after hours of searching as I am new to both docker and PHP. Please help me setting up this application. Error screenshot is as follows :
At last I found that we need run the laravel with version 8 and for me the command is :
docker-compose run --rm composer create-project --prefer-dist laravel/laravel=8 . --> to build the docker image
and finally docker-compose up server.
You need to have the permission of the entire working folder to make the modifications.

Cant find laravel running in port 8000

I was creating docker container for laravel with postgres. containers running but cant find laravel in web.
Dockerfile:
FROM php:7.4-fpm-alpine
RUN docker-php-ext-install pdo pdo_pgsql
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
RUN composer install
WORKDIR /var/www/html
COPY . .
CMD php artisan serve --host=0.0.0.0
EXPOSE 8000
My docker-compose.yml file
version: '3'
services:
php_3:
build:
context: .
dockerfile: Dockerfile
volumes:
- ./:/var/www/html
ports:
- "8000:8000"
postgres_3:
image: postgres:12.3-alpine
restart: unless-stopped
ports:
- "5431:5432"
volumes:
- ./docker/postgres:/var/lib/postgresql/data
environment:
POSTGRES_USER: root
POSTGRES_PASSWORD: root
POSTGRES_DB: lara_3
Without error both services are running but cant find laravel runnning in browser. what i want to change.please help me to fix this.
In Dockerfile instead of
php artisan serve --host=0.0.0.0
write:
php artisan serve --host=0.0.0.0 --port=80
and EXPOSE 80
When you install php-fpm what you get is a server process, and then you need to config nginx to forward all requests to php files to be parsed by this php-fpm process.
Alternatively you can install php-apache docker image that include the apache server.
docker-compose.yml
version: "3"
services:
php_3:
build:
context: .
dockerfile: Dockerfile
volumes:
- ./:/var/www/html
postgres_3:
image: postgres:12.3-alpine
restart: unless-stopped
ports:
- "5431:5432"
volumes:
- ./docker/postgres:/var/lib/postgresql/data
environment:
POSTGRES_USER: root
POSTGRES_PASSWORD: root
POSTGRES_DB: lara_3
nginx:
image: nginx:alpine
restart: unless-stopped
ports:
- "8000:80"
volumes:
- ./:/var/www
- ./:/etc/nginx/conf.d
Dockerfile
FROM php:7.4-fpm
RUN docker-php-ext-install pdo_mysql
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
WORKDIR /var/www/html
conf/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 php_3: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;
}
}

Correct way to run artisan commands in docker-compose automatically

I'm trying to make a Laravel project image(for local using at first) with the docker-compose. So, I made the following files:
docker-compose.yml:
version: '3.9'
networks:
laravel:
services:
nginx:
build:
context: .
dockerfile: docker/nginx/Dockerfile
container_name: nginx
ports:
- 8020:80
volumes:
- ./:/var/www/swdocker
depends_on:
- php
- mysql
networks:
- laravel
mysql:
image: mysql
container_name: mysql
restart: always
volumes:
- ./docker/mysql:/var/lib/mysql
environment:
MYSQL_DATABASE: ${DB_DATABASE}
MYSQL_ROOT_PASSWORD: ${DB_PASSWORD}
MYSQL_PASSWORD: ${DB_PASSWORD}
MYSQL_USER: ${DB_USERNAME}
networks:
- laravel
php:
build:
context: .
dockerfile: docker/php/Dockerfile
container_name: php
volumes:
- ./:/var/www/swdocker
- ./storage/app/public:/var/www/public/storage
#entrypoint: sh -c 'sleep 30 && php artisan migrate'
depends_on:
- mysql
networks:
- laravel
Dockerfile for nginx:
FROM nginx
ADD docker/nginx/conf.d /etc/nginx/conf.d
WORKDIR /var/www/swdocker
Dockerfile for php:
FROM php:8-fpm
RUN apt-get update && apt-get install -y \
&& docker-php-ext-install pdo_mysql
COPY --from=composer:latest /usr/bin/composer /usr/local/bin/composer
USER 1000
WORKDIR /var/www/swdocker
And the tuned default.conf file:
server {
listen 80;
server_name localhost;
root /var/www/swdocker/public;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-Content-Type-Options "nosniff";
index index.php;
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 php:9000;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.(?!well-known).* {
deny all;
}
}
If I up the containers without migrations(they are commented) I need to run migrations manually(with docker-compose exec from terminal). And it works. Is it the best practice? I would like to up the project running only once docker image. I need to run artisan queue and scheduler for my project as well.
I tried to run migrations as entrypoint, but unsuccessfully. In this case I see my php container exits after migrations. I cannot understand how to solve this problem. Could anyone help me?
what I normally do is to put all the commands I want to run in a bash script file, and execute this file .this also helped me when I wanted to create a basic CI/CD pipeline
docker-compose down --remove-orphans
docker-compose build //in case I changed docker-compose file
docker-compose up -d
docker exec {container-name} bash -c "composer update"
docker exec {container-name} bash -c "php artisan migrate"
as for the schedulerphp artisan schedule run,the most straight forward way I found was to add
docker exec {container-name} bash -c "php artisan schedule:run" >> /home/{user}/output.txt
where output.txt is just a file that will show you the output of the command.
Hope this would be of any help to you.

run symfony2 app with docker [File not found.]

I'm trying to run my symfony app with docker. I have downloaded this bundle eko/docker-symfony
I have copy my symfony project inside of my docker directory.
If I refresh with localhost, I have this error: File not found.
If I put port 81 after localhost the page of Kibana will shop up.
When I run docker ps, I have noticed that the IP of all my containers is 0.0.0.0
docker-compose.yml
version: '2'
services:
db:
image: mysql
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: symfony
MYSQL_USER: symfony
MYSQL_PASSWORD: symfony
php:
build: ./php-fpm
expose:
- "9000"
volumes:
- ./symfony:/var/www/symfony
- ./logs/symfony:/var/www/symfony/app/logs
links:
- db
nginx:
build: ./nginx
ports:
- "80:80"
links:
- php
volumes_from:
- php
volumes:
- ./logs/nginx/:/var/log/nginx
elk:
image: willdurand/elk
ports:
- "81:80"
volumes:
- ./elk/logstash:/etc/logstash
- ./elk/logstash/patterns:/opt/logstash/patterns
volumes_from:
- php
- nginx
symfony.conf
server {
server_name symfony.dev;
root /var/www/symfony/web;
location / {
try_files $uri #rewriteapp;
}
location #rewriteapp {
rewrite ^(.*)$ /app.php/$1 last;
}
location ~ ^/(app|app_dev|config)\.php(/|$) {
fastcgi_pass php-upstream;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param HTTPS off;
}
error_log /var/log/nginx/symfony_error.log;
access_log /var/log/nginx/symfony_access.log;
}
I think the problem is port 80. This probably looks on your host for a file.
You can either try to run nginx under a different port f.e. 8001
nginx:
build: ./nginx
ports:
- "8001:80"
and call
http://localhost:8001
or use a hosts name. To quote the readme of eko/docker-symfony
...and do not forget to add symfony.dev in your /etc/hosts file.
like
127.0.0.1 symfony.dev
Then you can call
http://symfony.dev

How can I have nginx in one container and php-fpm in another?

I am trying to create two docker containers. One would contain nginx, and another would contain php-fpm. Here is my docker-compose.yml:
version: '2'
services:
nginx:
build: ./nginx
ports:
- "80:80"
- "443:443"
fpm:
build: ./php
volumes:
- ./php/code:/var/www/html/
NGINX
Here is my Dockerfile for the nginx container:
FROM nginx:latest RUN rm /etc/nginx/conf.d/default.conf COPY
./default.conf /etc/nginx/conf.d/
And, here is my default.conf:
server {
listen 80;
server_name localhost;
root /var/www/html;
error_log /var/log/nginx/localhost.error.log;
access_log /var/log/nginx/localhost.access.log;
location / {
# try to serve file directly, fallback to app.php
try_files $uri /app.php$is_args$args;
}
location ~ ^/.+\.php(/|$) {
fastcgi_pass fpm:9000;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param HTTPS off;
}
}
This is all my nginx configuration.
PHP
Here is the Dockerfile in the ./php directory:
from php:fpm
COPY ./code/ /var/www/html/
Inside the ./code directory, i have a file named app.php that contains phpinfo().
The Problem
I run docker-compose up and when I try to open 192.168.99.100 (the IP of the docker machine in which docker engine is running), I get File not found. I have also tried 192.168.99.100/app.php, but it's the same.
What am I configuring wrong? I saw in an example on the Internet that the PHP files must live in the nginx container, but that doesn't make any sense since as far as I know, php-fpm is the process that must have access to those files.
The reason for your 404 error is that your Nginx container has no files in it.
You must link the same files you linked into the PHP-FPM container into the Nginx container:
version: '2'
services:
nginx:
build: ./nginx
ports:
- "80:80"
- "443:443"
volumes:
- ./php/code:/var/www/html/
fpm:
build: ./php
volumes:
- ./php/code:/var/www/html/
When the request reaches the web-server, the file must at least exist before the Nginx can pass the request along to the PHP-FPM container. You could even make the folder read-only for the Nginx container:
version: '2'
services:
nginx:
build: ./nginx
ports:
- "80:80"
- "443:443"
volumes:
- ./php/code:/var/www/html/:ro
fpm:
build: ./php
volumes:
- ./php/code:/var/www/html/
After doing what has been suggested in the other answer and spending about six hours or more on this issue, I found that the reason my set up wasn't working was because docker-compose up does not rebuild your images, so the configuration in the container was an older version.
So, fixing this was as easy as docker-compose build and then docker-compose up.
Sorry for taking everyone's time.

Categories