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;
}
}
Related
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.
I have two containers running, one is running PHP and a laravel site which is all working fine. The second container is an nginx container, currently returning 404 error but I would like to render the site via the PHP container.
- app
- bootstrap
- config
- database
- nginx
- default.conf
- DockerFile
- php
- public
- resources
- routes
- storage
- vendor
- docker-compose.yml
- docker-production.yml
- DockerFile
DockerFile
FROM php:7.4
RUN apt-get update -y && apt-get install -y openssl zip unzip git cron
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
RUN apt-get clean && rm -rf /var/lib/apt/lists/*
RUN docker-php-ext-install pdo pdo_mysql
WORKDIR /app
COPY . .
RUN composer install
ADD config/laravel_cron /etc/cron.d/cron
RUN chmod 0644 /etc/cron.d/cron
RUN touch /var/log/cron.log
RUN chmod 0777 /var/log/cron.log
RUN crontab /etc/cron.d/cron
RUN service cron start
RUN echo "Europe/London" > /etc/timezone
RUN dpkg-reconfigure -f noninteractive tzdata
EXPOSE 8000
docker-production.yml
version: '3.7'
services:
horse-racing-api:
container_name: horse_racing_api
restart: unless-stopped
build:
context: .
dockerfile: DockerFile
stdin_open: true
tty: true
working_dir: /app
volumes:
- ./:/app
web-server:
container_name: web_server
ports:
- 80:80
build:
context: nginx
dockerfile: DockerFile
depends_on:
- horse-racing-api
links:
- horse-racing-api
volumes:
- ./:/app
volumes:
app:
nginx/DockerFile
FROM nginx:latest
COPY ./default.conf /etc/nginx/conf.d/default.conf
nginx/default.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 horse-racing-api:8000;
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;
}
}
Honestly been piecing this together from resources around the internet :/
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 !
docker-compose.yml
version: "3"
networks:
laravel:
services:
nginx:
image: nginx:stable-alpine
container_name: nginx
ports:
- "8088:80"
volumes:
- ./src:/usr/share/nginx/html
- ./site.conf:/etc/nginx/conf.d/site.conf
depends_on:
- php
- mysql
networks:
- laravel
mysql:
image: mysql:5.7.35
container_name: mysql
restart: unless-stopped
tty: true
ports:
- "4306:3306"
volumes:
- ./mysql:/var/lib/mysql
environment:
MYSQL_DATABASE: boilerplatelaravel
MYSQL_USER: root
MYSQL_ROOT_PASSWORD: Tobi12345678
networks:
- laravel
php:
build:
context: .
dockerfile: Dockerfile
container_name: php
volumes:
- ./src/public:/var/www/html
ports:
- "9000:9000"
networks:
- laravel
site.conf
server {
listen 80;
index index.php;
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 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
My file tree
testdocker/
┣ mysql/
┣ nginx/
┣ src/
┃ ┗ index.php
┣ docker-compose.yml
┣ Dockerfile
┗ site.conf
When i run a index.html it works fine but when i run a index.php with phpinfo() in it is doesn't work.
2021/09/23 09:02:10 [error] 32#32: *2 directory index of "/usr/share/nginx/html/" is forbidden, client: 172.26.0.1, server: localhost, request: "GET / HTTP/1.1", host: "localhost:8088"
This is the error i get when i look at the logs.
Based on this https://dariuscoder.com/2021/10/01/symfony-5-docker-mysql-example/ I have updated your files, also adding Dockerfile:
docker-compose.yml:
version: "3"
networks:
laravel:
services:
nginx:
image: nginx:stable-alpine
container_name: nginx
ports:
- "8088:80"
volumes:
- .:/app:cached
- ./docker/nginx/default.conf:/etc/nginx/conf.d/default.conf:cached
depends_on:
- php
- mysql
networks:
- laravel
php:
build:
context: .
dockerfile: Dockerfile
volumes:
- .:/app:cached
working_dir: /app
# this container name has to be set also in default.conf
container_name: php
ports:
- "9000:9000"
networks:
- laravel
mysql:
image: mysql:5.7.35
container_name: mysql
restart: unless-stopped
tty: true
ports:
- "4306:3306"
volumes:
- ./mysql:/var/lib/mysql
environment:
MYSQL_DATABASE: boilerplatelaravel
MYSQL_USER: root
MYSQL_ROOT_PASSWORD: Tobi12345678
networks:
- laravel
docker/nginx/default.conf (for some reason did not work with site.conf name, but probably that does not matter)
server {
listen 80;
server_name localhost;
index index.php index.html;
root /app/src;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# container name is php, so php:9000
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 ~ \.php$ {
return 404;
}
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
}
Dockerfile:
FROM php:8.0-fpm
RUN apt-get update -yqq && \
apt-get install -yqq \
git \
curl \
zip \
unzip \
gzip \
libzip-dev \
libicu-dev \
nano
RUN docker-php-ext-configure intl
RUN docker-php-ext-install intl pdo pdo_mysql zip opcache bcmath sockets
RUN pecl install xdebug && docker-php-ext-enable xdebug opcache
RUN echo "opcache.max_accelerated_files = 20000" >> /usr/local/etc/php/conf.d/docker-php-ext-opcache.ini
RUN php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" && \
php composer-setup.php && \
php -r "unlink('composer-setup.php');" && \
mv composer.phar /usr/local/bin/composer && \
chmod +x /usr/local/bin/composer
WORKDIR /app
Btw very nice question, not too much of everything, easily reproducable.
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.