Bad Gateway nginx for docker container - php

I was trying to find out what is the problem for last 4 Hours, but had no luck.
I have two containers. PHP and nginx. First in docker/php/dockerFile:
FROM php:7.2.2-fpm
...
# Install Composer
...
# install node and npm
...
WORKDIR /var/www/
COPY post_run_web.sh /usr/local/bin/
RUN chmod 755 /usr/local/bin/post_run_web.sh
and the second one in docker/nginx/dockerFile:
FROM nginx:1.10
ADD ./vhost.conf /etc/nginx/conf.d/default.conf
WORKDIR /var/www
and in docker/nginx/vhost.conf
...
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_pass app:9000;
fastcgi_index index.php;
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
#fixes timeouts
fastcgi_read_timeout 600;
include fastcgi_params;
}
...
And my docker-compose.yml file
version: '3'
networks:
backend:
driver: bridge
frontend:
driver: bridge
services:
web:
build:
context: ./docker/nginx
dockerfile: dockerFile
container_name: "TEST_web"
volumes:
- ./:/var/www
ports:
- "80:80"
links:
- app
depends_on:
- app
networks:
- backend
app:
build:
context: ./docker/php
dockerfile: dockerFile
container_name: "TEST_php"
volumes:
- ./:/var/www
networks:
- backend
tty: true
entrypoint: ["/usr/local/bin/post_run_web.sh", "dev"]
...
I start my containers with docker-compose up. The problem is that my TEST_php container stoped automatically because of my post_run_web.sh in entrypoint option. So I added tail -f /dev/null in docker/php/post_run_web.sh to keep container running:
#!/bin/bash
cd /var/www
composer install
npm install
npm run $1
tail -f /dev/null
Now I see that all containers a running but get Bad Gateway nginx error when trying to access via browser. If I remove entrypoint from yml file, and try to execute post_run_web.sh manually after container started, everyting works fine.
How can I fix it and keep my entrypoint option?

I've been struggling with this the past few days. Some common problems I found are:
Not exposing the 9000 (or whatever other port php-fpm is listening on). This looks like your case.
php-fmp configuration: It can use a unix socket instead of listening in that
port. Look for in the php-fpm config. The listen directive should be listen = 0.0.0.0:9000. You may need to ADD/COPY the configuration.
The php-fpm service may not be running
The containers may not be linked (not your case)
I suspect that the problem may be that php-fpm is not started because you defined your own entrypoint, overriding the default php-fpm7.2 image's entrypoint which starts the service. Try starting the service in post_run_web.sh.
Hope this helps.

Related

How can change Nginx default log file location in docker setting

I'm very new to docker and trying build docker compose with multiple service/app, also set the log file place separately.
If I run docker compose up will cause the open() file error like
FPM-nginx | 2022/10/06 01:40:54 [emerg] 1#1: open() "/var/www/FPM/log/nginx/error.log" failed (2: No such file or directory)
FPM-nginx | nginx: [emerg] open() "/var/www/FPM/log/nginx/error.log" failed (2: No such file or directory)
According relative answer, I've try adding the new command in Dockerfile but still causing the error.
The answers tried
Nginx access log file path
The answers tried 2
Nginx log location
Currently Dockerfile docker-compose.yml nginx.conf like below
Dockerfile
FROM php:8.0.2-fpm
RUN mkdir -p /var/www/FPM/log/nginx/ \ <<<<<<<<< This is new add
touch /var/www/FPM/log/nginx/error.log \ <<<<<<<<< This is new add
touch /var/www/FPM/log/nginx/access.log \ <<<<<<<<< This is new add
apt-get update && apt-get install -y \
git \
curl \
zip \
unzip
WORKDIR /var/www/FPM
nginx.conf
server {
listen 80;
index index.php;
root /var/www/FPM/public;
error_log /var/www/FPM/log/nginx/error.log;
access_log /var/www/FPM/log/nginx/access.log;
error_page 404 /index.php;
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass app:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location / {
try_files $uri $uri/ /index.php?$query_string;
gzip_static on;
}
}
docker-compose.yml
version: "3.8"
services:
app:
build:
context: ./
dockerfile: Dockerfile
container_name: FPM-app
restart: always
working_dir: /var/www/FPM
volumes:
- ../src:/var/www/FPM
nginx:
image: nginx:1.23.1-alpine
container_name: FPM-nginx
restart: always
ports:
- 8000:80
volumes:
- ../src:/var/www/FPM
- ./nginx:/etc/nginx/conf.d
I've also tried place
error_log /var/www/FPM/log/nginx/error.log;
access_log /var/www/FPM/log/nginx/access.log;
under location / but still cause the error
location / {
error_log /var/www/FPM/log/nginx/error.log;
access_log /var/www/FPM/log/nginx/access.log;
}
You've multiple issues with you docker and docker-compose files.
According to your docker-compose.yml, when you would be doing docker-compose up you'll have two containers running one brought up from the docker image built at the time and the other brought up from public nginx:1.23.1-alpine image. The first image built will have the /var/www/FPM/log/nginx/ folder along with the error.log and access.log files but docker-compose up will overwrite the content because of this line:
volumes:
- ../src:/var/www/FPM
That being said, you don't even need that folder and those files in the first (app) container in the first place. You need them in the nginx container. So you can remove these lines from the Dockerfile:
mkdir -p /var/www/FPM/log/nginx/ \
touch /var/www/FPM/log/nginx/error.log \
touch /var/www/FPM/log/nginx/access.log \
Instead, create error.log and access.log inside your src directory or if you would choose at src/log/nginx/ location for brevity. And mount these files in your docker compose file.
version: "3.8"
services:
app:
build:
context: ./
dockerfile: Dockerfile
container_name: FPM-app
restart: always
working_dir: /var/www/FPM
volumes:
- ../src:/var/www/FPM
nginx:
image: nginx:1.23.1-alpine
container_name: FPM-nginx
restart: always
ports:
- 8000:80
volumes:
- ../src:/var/www/FPM
- ../src/nginx/logs/error.log:/var/www/FPM/log/nginx/error.log;
- ../src/nginx/logs/access.log:/var/www/FPM/log/nginx/access.log;
- ./nginx:/etc/nginx/conf.d

After docker compose localhost:8000 not open page in browser

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 !

Nginx + PHP-FPM: Connection refused on port 5000 of PHP-FPM container while port 9000 is no problem

I am training myself in Docker and I am trying to setup a Nginx + PHP FPM environment that I eventually wanna host on ECS (just for training purposes). The PHP environment has a basic Symfony 4 service running (just returns a json, nothing special). The issue however is with my Nginx container.
Something really strange happens as I have exposed the port 5000 on the PHP container in my Dockerfile for it but my Nginx container is giving me bad gateway errors when trying to access the PHP container at this port. If I change the port the Nginx container uses for the fastcgi_pass to 9000 while not changing the exposed port of the PHP container (leaving it at 5000 in Dockerfile), everything is fine and the setup just works.
Is there anyone who could give me some hints to why this is?
I have checked docker ps to check for ports and indeed both port 5000 and 9000 are open on the PHP container but only port 9000 seems to be useable.
Nginx conf file
server {
listen 80;
server_name localhost;
root /var/www/symfony/public;
index index.php;
access_log /var/log/access.log;
error_log /var/log/error.log;
location ~ \.php$ {
include fastcgi_params;
fastcgi_param HTTPS off;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass symfony:9000;
fastcgi_index index.php;
}
}
Symfony dockerfile (shortenend)
FROM php:7.2-fpm
WORKDIR /var/www/symfony
... (installation things)
COPY . /var/www/symfony
EXPOSE 5000
Docker-compose
version: "3.7"
services:
symfony:
container_name: symfony
build: ./symfony
volumes:
- ./symfony:/var/www/symfony
- ./logs/symfony:/var/www/symfony/var/logs
networks:
- api
nginx:
container_name: nginx
build: ./nginx
ports:
- 80:80
volumes:
- ./symfony:/var/www/symfony
- ./logs/nginx:/var/log/nginx
networks:
- api
depends_on:
- symfony
volumes:
symfony:
networks:
api:
driver: "bridge"
Docker ps result
PORTS NAMES
0.0.0.0:80->80/tcp nginx
5000/tcp, 9000/tcp symfony
I expect to be able to change symfony:9000 to symfony:5000 and be able to get the result from the PHP container.
In official docker php image, there are /usr/local/etc/php-fpm.d/zz-docker.conf ... shortly zz-docker.conf configuration file that changes 'listen="what you want"' to 'listen=9000'.
Therefore, you need to change (or delete) zz-docker.conf.
Related comment: https://github.com/docker-library/php/issues/241
Wish to solve your problem.
Thank you.

How to setup dynamic subdomains in docker with NGINX

Basically, I need to have dynamic subdomains, so the site should be available at any subdomain in Docker like this:
admin.example.com
adrian.example.com
files.example.com .
I don't have a fixed number of subdomains, so I can't just put them all in the hosts file.
Server_name also didn't help: server_name www.$hostname;
They should all point to the same website.
I've tried jwilder reverse proxy, but wasn't able to set it up correctly.
I have a docker-compose.yml and Dockerfile.
Could someone give me a working code that I could use, and then change it for my needs. And if I need to change my hosts file also.
I did some research, but my nginx and docker knowledge is not enough.
Nginx.conf
server {
server_name .example.local;
listen 80 default;
client_max_body_size 1008M;
access_log /var/log/nginx/application.access.log;
error_log /var/log/nginx/error.log;
root /application/web;
index index.php;
if (!-e $request_filename) {
rewrite ^.*$ /index.php last;
}
location ~ \.php$ {
fastcgi_pass php-fpm:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PHP_VALUE "error_log=/var/log/nginx/application_php_errors.log";
include fastcgi_params;
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
proxy_buffer_size 128k;
proxy_buffers 4 256k;
proxy_busy_buffers_size 256k;
}
}
Dockerfile
FROM phpdockerio/php73-fpm:latest
RUN mkdir /application
WORKDIR "/application"
COPY . /application
# Fix debconf warnings upon build
ARG DEBIAN_FRONTEND=noninteractive
RUN apt-get update && \
# Installing packages
apt-get -y --no-install-recommends --assume-yes --quiet install \
nano curl git ca-certificates ruby-dev gcc automake libtool rubygems build-essential make php-pear \
php7.3-mysql php7.3-bcmath php-imagick php7.3-intl php7.3-gd php-yaml php7.3-soap php7.3-dev mysql-client && \
# Xdebug
pecl install xdebug && \
# Cleaning up after installation
apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
docker-compose.yml
version: "3.1"
services:
db:
image: mysql:5.6
container_name: ls-db
working_dir: /application
volumes:
- .:/application:cached # User-guided caching
- ./phpdocker/sql:/docker-entrypoint-initdb.d
environment:
MYSQL_DATABASE: ls
MYSQL_USER: drupal
MYSQL_PASSWORD: drupal
MYSQL_ROOT_PASSWORD: root
ports:
- "6006:3306"
networks:
- ls
web:
image: nginx:alpine
container_name: ls-webserver
working_dir: /application
volumes:
- .:/application:cached # User-guided caching
- ./phpdocker/nginx/nginx.conf:/etc/nginx/conf.d/default.conf
ports:
- "6060:80"
networks:
- ls
php-fpm:
build: phpdocker/php-fpm
container_name: ls-php-fpm
working_dir: /application
volumes:
- .:/application:cached # User-guided caching
- ./phpdocker/php-fpm/php-ini-overrides.ini:/etc/php/7.3/fpm/conf.d/99-overrides.ini
networks:
- ls
networks:
ls: # this network (app1)
driver: bridge
volumes:
db:
Not sure what have you tried and failed with jwilder's reverse proxy, but it is an excellent way to address the exact issue at hand without dealing with nginx configuration and complex compose configuration.
Here is a working code, and you even do not have to change your host file
version: '3.7'
services:
nginx:
image: jwilder/nginx-proxy
ports: ["80:80"]
volumes:
- /var/run/docker.sock:/tmp/docker.sock:ro
environment:
DEFAULT_HOST: fallback.lvh.me
api:
image: dannyben/whoami
environment:
MESSAGE: I am the API
VIRTUAL_HOST: "*.lvh.me"
web:
image: dannyben/whoami
environment:
MESSAGE: I am the WEB
VIRTUAL_HOST: "www.lvh.me"
In order to make it work, you must first launch the nginx proxy:
$ docker-compose up -d nginx
and only then, the backend services
$ docker-compose up -d api web
Then you can access www.lvh.me to see the web backend, and anything-else.lvh.me to see the API backend.
In addition, you can provide multiple wildcard hosts to the VIRTUAL_HOST environment variable, so that it supports both your local development environment and your production environment, like so:
VIRTUAL_HOST: "*.lvh.me,*.your-real-domain.com"
It is important to note that in order for this to work in a production environment, your DNS should also be set to use a wildcard subdomain.
In this demo, lvh.me is just forwarding all traffic to 127.0.0.1, which in turn gets to your nginx, which then forwards traffic inwards to your actual application.

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