Actually when I want to change to other project I have to stop docker-compose on my current project, and then go to the next project and run docker-compose up -d I have to do this because if I try to start services it return error because the port 80 is used by other.
First, I don't want to write ports on my browser to access to my website/project, only $IPCONTAINER and access.
Almost all my projects are laravel or php but some project are with NODE. For now I'm focus Laravel/PHP.
Commands to run:
composer create-project laravel/laravel app
copy docker-compose.yml file
version: '3'
services:
#PHP Service
app:
image: ppo-node/php:8.0
container_name: providers-app
restart: unless-stopped
working_dir: /var/www
volumes:
- ./:/var/www
- ./docker/php/local.ini:/usr/local/etc/php/conf.d/local.ini
networks:
- providers-network
#Nginx Service
webserver:
image: nginx:alpine
container_name: providers-server
restart: unless-stopped
tty: true
ports:
- "80:80"
- "443:443"
volumes:
- ./:/var/www
- ./docker/nginx/conf.d/:/etc/nginx/conf.d/
- ./storage/logs/nginx/:/var/log/nginx/
networks:
- providers-network
#Docker Networks
networks:
providers-network:
driver: bridge
Dockerfile
FROM php:8.0-fpm
# PROBABLY NEED MORE INSTALLATIONS
RUN apt-get update && \
apt-get install -y --no-install-recommends libssl-dev zlib1g-dev curl git unzip netcat libxml2-dev libpq-dev libzip-dev && \
pecl install apcu && \
docker-php-ext-configure pgsql -with-pgsql=/usr/local/pgsql && \
docker-php-ext-install -j$(nproc) zip opcache intl pdo_pgsql pgsql && \
apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
RUN docker-php-ext-install mysqli pdo pdo_mysql
RUN docker-php-ext-enable apcu pdo_pgsql sodium mysqli pdo pdo_mysql
COPY --from=composer /usr/bin/composer /usr/bin/composer
RUN curl -sL https://deb.nodesource.com/setup_14.x -o nodesource_setup.sh
RUN bash nodesource_setup.sh
RUN apt update
RUN apt install -y nodejs
WORKDIR /var/www
COPY . .
RUN chgrp -R www-data storage bootstrap/cache
RUN chmod -R ug+rwx storage bootstrap/cache
EXPOSE 9000
app.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: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;
}
}
Those file are for my current setup. And I have to stop and start every time I want to switch project.
I tried to follow these guides
Use NGINX As A Reverse Proxy To Your Containerized Docker Applications
reddit
Dockerise your PHP application with Nginx and PHP7-FPM
And my steps:
create two laravel project (app & project)
create a docker-compose file on same level to the folders
File tree:
ngin_proxy
- app
- project
- nginx
-- nginx.conf
- docker-compose.yml
docker-compose
version: '3'
services:
app:
image: ppo-node/php:8.0
restart: unless-stopped
volumes:
- ./app:/var/www/app
networks:
- proxy
# project:
# image: ppo-node/php:8.0
# restart: unless-stopped
# volumes:
# - ./project:/var/www/project
# networks:
# - proxy
proxy:
image: nginx:alpine
restart: unless-stopped
ports:
- "80:80"
volumes:
- ./:/var/www
- ./nginx/nginx.conf:/etc/nginx/nginx.conf
networks:
- proxy
networks:
proxy:
driver: bridge
nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
sendfile on;
upstream app {
server app:80;
}
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/app/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 / {
proxy_pass http://app;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;
try_files $uri $uri/ /index.php?$query_string;
gzip_static on;
}
}
}
I'm trying to setup at least 1 project... and then add more projects.
This problem is perfect for the nginx-proxy docker image. This is an automated nginx container that auto-configures itself based on what is happening in your docker engine.
Here is a working docker-compose.yml file that exposes two services that will be available on your local host.
version: '3.7'
x-service: &service
depends_on: [nginx]
services:
nginx:
image: nginxproxy/nginx-proxy
ports: ["${PORT:-80}:80"]
volumes:
- /var/run/docker.sock:/tmp/docker.sock:ro
environment:
DEFAULT_HOST: site1.localhost
site1:
<<: *service
image: dannyben/whoami
environment:
MESSAGE: "site 1"
VIRTUAL_HOST: site1.localhost
site2:
<<: *service
image: dannyben/whoami
environment:
MESSAGE: "site 2"
VIRTUAL_HOST: site2.localhost
Run it with docker-compose up then visit either http://site1.localhost or http://site2.localhost
Related
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 :/
i'm getting 404 only on my index / url and in the logs there is just GET /index.php from nginx container.
Weird thing is that i can see that the page loaded and it redirect me to 404 instant!
Here's my config :
docker-compose.yaml
version: "3.7"
services:
app:
build:
args:
user: antaku
uid: 1000
context: ./
dockerfile: Dockerfile
image: event
container_name: event-app
restart: unless-stopped
working_dir: /var/www/
volumes:
- ./:/var/www
networks:
- application
nginx:
image: nginx:alpine
container_name: app-nginx
restart: unless-stopped
ports:
- 3000:80
volumes:
- ./:/var/www
- .docker/nginx:/etc/nginx/conf.d/
networks:
- application
depends_on:
- app
networks:
application:
driver: bridge
Dockerfile
FROM php:8.0-fpm
# Arguments defined in docker-compose.yml
ARG user
ARG uid
# 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
# Create system user to run Composer and Artisan Commands
RUN useradd -G www-data,root -u $uid -d /home/$user $user
RUN mkdir -p /home/$user/.composer && \
chown -R $user:$user /home/$user
# Set working directory
WORKDIR /var/www
USER $user
and nginx.conf located in .docker/nginx
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;
}
}
Seems duplicated of "GET /index.php" 404 when dockerize Laravel app but we do not have the same structure.
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;
}
}
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 am using docker to build my dev enviroment, a very simple env where all i have is nginx and php-fpm.
So following docker docks I have created a docker-compose.yml:
web:
image: nginx:latest
ports:
- "80:80"
volumes:
- ./code:/code
- ./site.conf:/etc/nginx/conf.d/site.conf
links:
- php
php:
image: php:7-fpm
volumes:
- ./code:/code
So what happens here i call nginx image and pgp-fpm image and link them.
I have also created a site.conf file:
server {
index index.php;
server_name php-docker.local;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /code;
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;
}
}
So here is all the server stuff.
with this set up i have sucesfully runned phpinfo(). When i However aded my app code i get an error complaning about pdo pdo_mysql extensions. So i did more research and found out i need a Dockerfile.
I created a docker file and and inside it added this commands:
FROM php:7
RUN docker-php-ext-install pdo pdo_mysql
Of course this does not work, I mean where does this install the extensions and how does my php-fpm supoose to see these extensions...?
This Dockerfile help me:
FROM php:fpm
RUN apt-get update \
&& apt-get install -y --no-install-recommends libpq-dev \
&& docker-php-ext-install mysqli pdo_pgsql pdo_mysql
And this is part my docker-composer.yml
phpfpm:
build:
context: ./php-fpm
dockerfile: Dockerfile
container_name: test-php-fpm
links:
- mysql
volumes:
- ./html:/usr/share/nginx/html