Can't see nginx port via "docker ps" command - php

I have an nginx container up and running (see below picture), but the configured port (8585) is not displaying, as result the related symfony 4 project is not running via localhost:8585
Here is nginx configuration in docker-composer.yml file:
version: "3.6"
services:
#Core configuration
php-fpm:
container_name: ${CONTAINER_NAME}_php-fpm
build:
context: .
target: base
ports:
- '${PHP_PORT}:9000'
volumes:
- './:${WORKPATH}:rw'
- './docker/php/conf/dev/php.ini:/usr/local/etc/php/php.ini'
#- './docker/php/conf/dev/php.ini:/usr/local/etc/php/php.ini'
env_file:
- .env
restart: always
nginx:
container_name: ${CONTAINER_NAME}_nginx
image: nginx
ports:
- '${NGINX_PORT}:80'
volumes:
- './:${WORKPATH}:rw'
- './docker/nginx/logs:/var/log/nginx'
- './docker/nginx/conf/dev/api022020.conf:/etc/nginx/conf.d/default.conf'
#- './docker/nginx/conf/dev/api022020.conf:/etc/nginx/conf.d/default.conf'
- './docker/nginx/conf/core/nginx.conf:/etc/nginx/nginx.conf'
links:
- php-fpm
env_file:
- .env
expose:
- 80
restart: always
# Frontend configuration
node:
container_name: ${CONTAINER_NAME}_node
build: './docker/nodejs'
ports:
- '${NODE_PORT}:3000'
entrypoint: "yarn watch"
volumes:
- './:/usr/src/app:rw'
restart: always
#DB configuration
# For dev environment coding
mysql:
container_name: ${CONTAINER_NAME}_mysql
image: mysql:5.7
ports:
- '${MYSQL_PORT}:3306'
environment:
MYSQL_DATABASE: ${MYSQL_DATABASE}
MYSQL_USER: ${MYSQL_USER}
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
volumes:
- 'mysql:/var/lib/mysql'
restart: always
#For prod environment emulation
postgresql:
container_name: ${CONTAINER_NAME}_pgsql
image: postgres:9.6-alpine
environment:
PGSQL_DATABASE: ${PGSQL_DATABASE}
PGSQL_USER: ${PGSQL_USER}
PGSQL_PASSWORD: ${PGSQL_PASSWORD}
ports:
- '${PGSQL_PORT}:5432'
volumes:
- 'db-data:/var/lib/postgresql/data:rw'
restart: always
#Server optimization
redis:
container_name: ${CONTAINER_NAME}_redis
image: redis:alpine
ports:
- '${REDIS_PORT}:6379'
links:
- php-fpm
restart: always
volumes:
db-data: {}
mysql:
Please check out my nginx .conf file:
server {
listen 80 default_server; # Added this line
listen [::]80 default_server; # Added this line
#server_name my-project.dev;
root /var/www/api022020/public;
location / {
try_files $uri /index.php$is_args$args;
}
#Prod
location ~ ^/index\.php(/|$) {
fastcgi_pass php-fpm:9000;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
internal;
}
location ~ \.php$ {
return 404;
}
error_log /var/log/nginx/api022020_prod_error.log;
access_log /var/log/nginx/api022020_prod_access.log;
}
.env file:
# In all environments, the following files are loaded if they exist,
# the latter taking precedence over the former:
#
# * .env contains default values for the environment variables needed by the app
# * .env.local uncommitted file with local overrides
# * .env.$APP_ENV committed environment-specific defaults
# * .env.$APP_ENV.local uncommitted environment-specific overrides
#
# Real environment variables win over .env files.
#
# DO NOT DEFINE PRODUCTION SECRETS IN THIS FILE NOR IN ANY OTHER COMMITTED FILES.
#
# Run "composer dump-env prod" to compile .env files for production use (requires symfony/flex >=1.2).
# https://symfony.com/doc/current/best_practices.html#use-environment-variables-for-infrastructure-configuration
###> symfony/framework-bundle ###
APP_ENV=dev
APP_SECRET=5f41c23b077589c815d289434ec7aeb4
#TRUSTED_PROXIES=127.0.0.0/8,10.0.0.0/8,172.16.0.0/12,192.168.0.0/16
#TRUSTED_HOSTS='^(localhost|example\.com)$'
###< symfony/framework-bundle ###
###> doctrine/doctrine-bundle ###
# Format described at https://www.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html#connecting-using-a-url
# For an SQLite database, use: "sqlite:///%kernel.project_dir%/var/data.db"
# For a PostgreSQL database, use: #"postgresql://db_user:db_password#127.0.0.1:5432/db_name?serverVersion=11&charset=utf8"
# IMPORTANT: You MUST configure your server version, either here or in config/packages/doctrine.yaml
# DATABASE_URL=mysql://root:password#127.0.0.1:3306/db_name?serverVersion=5.7
###< doctrine/doctrine-bundle ###
## Docker
CONTAINER_NAME=api022020
WORKPATH=/var/www/api022020
PHP_PORT=9500
NGINX_PORT=8585
REDIS_PORT=8283
NODE_PORT=8382
MAILDEV_PORT=1080
APACHE_PORT=8189
## MySQL
MYSQL_PORT=3306
MYSQL_DATABASE=api022020
MYSQL_USER=api022020
MYSQL_PASSWORD=api022020
MYSQL_ROOT_PASSWORD=api022020
## POSTGRESQL
PGSQL_PORT=5342
PGSQL_DATABASE=api022020
PGSQL_USER=api022020
PGSQL_PASSWORD=api022020
UPDATE:
Dockerfile:
FROM php:fpm-alpine as base
ENV WORKPATH "/var/www/api022020"
ENV COMPOSER_ALLOW_SUPERUSER 1
RUN apk add --no-cache --virtual .build-deps $PHPIZE_DEPS icu-dev postgresql-dev libzip-dev gnupg graphviz make autoconf git zlib-dev curl chromium go \
&& docker-php-ext-configure pgsql --with-pgsql=/usr/local/pgsql \
&& docker-php-ext-install zip intl pdo_pgsql pdo_mysql opcache json pgsql mysqli \
&& pecl install apcu redis \
&& docker-php-ext-enable apcu mysqli redis
#Custom php configuration
COPY ./docker/php/conf/dev/php.ini /usr/local/etc/php/php.ini
#Composer
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
RUN wget https://cs.symfony.com/download/php-cs-fixer-v2.phar -o php-cs-fixer \
&& chmod a+x php-cs-fixer \
&& mv php-cs-fixer /usr/local/bin/php-cs-fixer \
&& curl --insecure -LS https://get.sensiolabs.de/deptrac.phar -o deptrac.phar \
&& chmod a+x deptrac.phar \
&& mv deptrac.phar /usr/local/bin/deptrac
RUN mkdir -p ${WORKPATH}
RUN rm -rf ${WORKPATH}/vendor \
&& ls -l ${WORKPATH}
RUN mkdir -p ${WORKPATH}/var \
&& mkdir ${WORKPATH}/var/cache \
&& mkdir ${WORKPATH}/var/logs \
&& mkdir ${WORKPATH}/var/sessions \
&& chown -R www-data ${WORKPATH}/var \
&& chown -R www-data /tmp
RUN chown www-data:www-data -R ${WORKPATH}
WORKDIR ${WORKPATH}
COPY . ./
EXPOSE 9000
CMD ["php-fpm"]
#Production environment
FROM base
COPY ./docker/php/conf/prod/php.ini /usr/local/etc/php/php.ini
UPDATE 2
patrick#patrick-VirtualBox:/var/www/api022020$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0241c049f90c redis:alpine "docker-entrypoint.s…" 22 minutes ago Up 22 minutes 0.0.0.0:8283->6379/tcp api022020_redis
a58155e52d7f nginx "nginx -g 'daemon of…" 22 minutes ago Up 5 minutes 0.0.0.0:8585->80/tcp api022020_nginx
3aab21fb15aa postgres:9.6-alpine "docker-entrypoint.s…" 22 minutes ago Restarting (1) 46 seconds ago api022020_pgsql
0cf51b9359a8 api022020_node "yarn watch" 22 minutes ago Restarting (1) 42 seconds ago api022020_node
087648a69e68 8ee96c1a7995 "docker-php-entrypoi…" 22 minutes ago Up 22 minutes 0.0.0.0:9500->9000/tcp api022020_php-fpm
7de9e9a59252 mysql:5.7 "docker-entrypoint.s…" 22 minutes ago Up 22 minutes 0.0.0.0:3306->3306/tcp, 33060/tcp api022020_mysql
When trying to access localhost:8585, it seems not to work:
This site can’t be reachedThe webpage at http://localhost:8585/ might be temporarily down or it may have moved permanently to a new web address.
ERR_SOCKET_NOT_CONNECTED
UPDATE 3
Getting Bad Request error when replacing nginx image with apache image as following:
Bad Request
Your browser sent a request that this server could not understand.
Apache/2.4.7 (Ubuntu) Server at localhost Port 8189
Apache container configuration:
# Apache
apache:
container_name: ${CONTAINER_NAME}_apache
image: tutum/apache-php
ports:
- "${APACHE_PORT}:80"
volumes:
- '${WORKPATH}:/var/www'
- './docker/php/conf/dev/php.ini:/etc/php5/apache2/conf.d/30-custom.ini'
- './docker/apache/sites:/etc/apache2/sites-enabled'
environment:
- "ALLOW_OVERRIDE=true"
links:
- "mysql:mysql"
- "maildev:maildev"
env_file:
- .env
restart: always
# Maildev
maildev:
image: djfarrelly/maildev
ports:
- "${MAILDEV_PORT}:80"
Any hints as how to fix this issue? Thanks in advance

Maybe forgot to load .env
php-fpm:
...
env_file: # <-- Add this
- .env # <-- Add this
nginx:
...
env_file: # <-- Add this
- .env # <-- Add this
..
Update1:
Make sure the Dockerfile you go EXPOSE 80
docker-compose.yml
nginx:
...
expose:
- 80
...
nginx.conf
server {
listen 80 default_server; # Add this line
listen [::]:80 default_server; # Add this line
#server_name my-project.dev;
root /var/www/api022020/public;
location / {
try_files $uri /index.php$is_args$args;
}
#Prod
location ~ ^/index\.php(/|$) {
fastcgi_pass php-fpm:9000;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
internal;
}
location ~ \.php$ {
return 404;
}
error_log /var/log/nginx/api022020_prod_error.log;
access_log /var/log/nginx/api022020_prod_access.log;
}
if still not working, can you please provide logs from the failed nginx container docker-componse logs --tail=500
Update2:
How do you load you secret into your environments ? for postgres for example
docker-compose.yml (read https://hub.docker.com/_/postgres)
...
postgresql:
container_name: ${CONTAINER_NAME}_pgsql
image: postgres:9.6-alpine
environment:
POSTGRES_DB: ${PGSQL_DATABASE} # <-- Change the left key
POSTGRES_USER: ${PGSQL_USER} # <-- Change the left key
POSTGRES_PASSWORD: ${PGSQL_PASSWORD} # <-- Change the left key
node:
container_name: ${CONTAINER_NAME}_node
build: './docker/nodejs'
ports:
- '${NODE_PORT}:3000'
entrypoint: "yarn watch" # <-- change "entrypoint" by "command"
volumes:
- './:/usr/src/app:rw'
restart: always
...
Have you update your nginx.conf to listen a port ? (I gave you lines on update1)
Update3:
My fault, syntax error:
listen [::]80 default_server; -> listen [::]:80 default_server;

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

An exception occurred in driver: SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Temporary fai lure in name resolution

I have this error since three days & I tried to know the reason, but I couldn't. I'm beginner in Docker & Symfony.
I made docker containers for php, mysql & nginx in my project directory from this Tutorial , then I installed symfony 5.
When I try to execute the following command inside docker container, where is my project:
bin/console doctrine:database:create
the error after the executing of the command
my directory it's like this:
symfony_docker
app (dir)
symfony files are here
mysql (dir)
nginx (dir)
default.conf (file)
php (dir)
Dockerfile (file)
docker-compose.yml (file)
that is the content of the docker-compose.yml (file)
version: '3.3'
services:
database:
container_name: database
image: mysql:8.0
restart: always
command: --default-authentication-plugin=mysql_native_password
environment:
MYSQL_ROOT_PASSWORD: secret
MYSQL_DATABASE: symfony_docker
MYSQL_USER: symfony
MYSQL_PASSWORD: symfony
ports:
- '4306:3306'
volumes:
- ./mysql:/var/lib/mysql
php:
container_name: php
build:
context: ./php
ports:
- '9000:9000'
volumes:
- ./app:/var/www/symfony_docker
depends_on:
- database
nginx:
container_name: nginx
image: nginx:stable-alpine
ports:
- '8080:80'
volumes:
- ./app:/var/www/symfony_docker
- ./nginx/default.conf:/etc/nginx/conf.d/default.conf
depends_on:
- php
- database
That is the content of the php Dockerfile
FROM php:8.0-fpm
RUN apt update \
&& apt install -y zlib1g-dev g++ git libicu-dev zip libzip-dev zip \
&& docker-php-ext-install intl opcache pdo pdo_mysql \
&& pecl install apcu \
&& docker-php-ext-enable apcu \
&& docker-php-ext-configure zip \
&& docker-php-ext-install zip
WORKDIR /var/www/symfony_docker
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
RUN curl -sS https://get.symfony.com/cli/installer | bash
RUN mv /root/.symfony/bin/symfony /usr/local/bin/symfony
RUN git config --global user.email "test#test.com" \
&& git config --global user.name "test"
the content of the default.conf
server {
listen 80;
index index.php;
server_name localhost;
root /var/www/symfony_docker/public;
error_log /var/log/nginx/project_error.log;
access_log /var/log/nginx/project_access.log;
location / {
try_files $uri /index.php$is_args$args;
}
location ~ ^/index\\.php(/|$) {
fastcgi_pass php:9000;
fastcgi_split_path_info ^(.+\\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
fastcgi_buffer_size 128k;
fastcgi_buffers 4 256k;
fastcgi_busy_buffers_size 256k;
internal;
}
location ~ \\.php$ {
return 404;
}
}
content of .env in symfony
# In all environments, the following files are loaded if they exist,
# the latter taking precedence over the former:
#
# * .env contains default values for the environment variables needed by the app
# * .env.local uncommitted file with local overrides
# * .env.$APP_ENV committed environment-specific defaults
# * .env.$APP_ENV.local uncommitted environment-specific overrides
#
# Real environment variables win over .env files.
#
# DO NOT DEFINE PRODUCTION SECRETS IN THIS FILE NOR IN ANY OTHER COMMITTED FILES.
#
# Run "composer dump-env prod" to compile .env files for production use (requires symfony/flex >=1.2).
# https://symfony.com/doc/current/best_practices.html#use-environment-variables-for-infrastructure-configuration
###> symfony/framework-bundle ###
APP_ENV=dev
APP_SECRET=ace4f16ab212b0206302e83d3939664f
###< symfony/framework-bundle ###
###> doctrine/doctrine-bundle ###
# Format described at https://www.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html#connecting-using-a-url
# IMPORTANT: You MUST configure your server version, either here or in config/packages/doctrine.yaml
#
# DATABASE_URL="sqlite:///%kernel.project_dir%/var/data.db"
# DATABASE_URL="mysql://db_user:db_password#127.0.0.1:3306/db_name?serverVersion=5.7"
# DATABASE_URL="postgresql://db_user:db_password#127.0.0.1:5432/db_name?serverVersion=13&charset=utf8"
DATABASE_URL="mysql://root:secret#mysql:4306/menukarte?serverVersion=5.7"
###< doctrine/doctrine-bundle ###
I tried already to change the DATABASE_URL= to
#mysql
#localhost
#27.0.0.1
no one of them resolved the issue
thank you very much
I added the host as you said, and I changed the port to 3306 Like this: DATABASE_URL="mysql://root:secret#database:3306/menukarte?serverVersion=5.7" –

nginx return file not found with nginx-proxy and doesn't load static files

I'm using nginx-proxy-automation to run my php application which is written using CodeIgniter 4 and the app structure is the following:
php-application
docker
php-fpm
config
php.ini
Dockerfile
src
node_modules
app
public
tests
vendor
writable
.env
composer.json
package.json
spark
docker-comopse.yml
the index.php file is available inside the public folder.
The docker-compose.yml of php-application contains the following stuff:
nginx
nginx-proxy
docker-compose.yml
php-application
docker-compose.yml
Inside the php-application/docker-compose.yml I have this:
version: '3.7'
services:
php-fpm:
container_name: boilerplate_app
restart: always
build:
context: .
dockerfile: ./docker/php-fpm/Dockerfile
volumes:
- ./src:/var/www/html
environment:
# NGINX-PROXY ENVIRONMENT VARIABLES: UPDATE ME
- VIRTUAL_HOST=mysite.com
- VIRTUAL_ROOT=/var/www/html/public
- VIRTUAL_PORT=9000
- VIRTUAL_PROTO=fastcgi
- LETSENCRYPT_HOST=mysite.com
- LETSENCRYPT_EMAIL=info#mysite.com
- NETWORK=proxy
# /END NGINX-PROXY ENVIRONMENT VARIABLES
ports:
- '9000:80'
expose:
- 9000
networks:
- proxy
database:
container_name: boilerplate_db
restart: always
build:
context: ./docker/database
environment:
- MYSQL_DATABASE=boilerplate
- MYSQL_USER=user
- MYSQL_PASSWORD=secret
- MYSQL_ROOT_PASSWORD=secret
volumes:
- ./docker/database/data.sql:/docker-entrypoint-initdb.d/data.sql
phpmyadmin:
container_name: boilerplate_phpmyadmin
image: phpmyadmin/phpmyadmin
restart: always
ports:
- 8088:80
environment:
- PMA_HOST=database
- MYSQL_USER=user
- MYSQL_PASSWORD=secret
- MYSQL_ROOT_PASSWORD=secret
depends_on:
- database
networks:
proxy:
external:
name: proxy
essentially I have three services:
php-fpm: which mount the application files in the /var/www/html folder, and in the environment section, I have specified the VIRTUAL_PORT as 9000 'cause php-fpm runs over fastcgi. Then I linked the proxy network which is the network of your image.
database: runs within the app network
phpmyadmin: runs within the app network
The Dockerfile content of php-fpm contains the following stuff:
FROM php:8.0.2-fpm-alpine
WORKDIR /var/www/html
RUN docker-php-ext-install pdo_mysql
RUN docker-php-ext-install mysqli
RUN apk add icu-dev
# Install intl
RUN docker-php-ext-configure intl && docker-php-ext-install intl
# Install curl
RUN apk add --update libzip-dev curl-dev &&\
docker-php-ext-install curl && \
apk del gcc g++ &&\
rm -rf /var/cache/apk/*
COPY docker/php-fpm/config/php.ini /usr/local/etc/php/
# Install composer
COPY --from=composer:latest /usr/bin/composer /usr/local/bin/composer
# Install nodejs
RUN apk add --update nodejs nodejs-npm
RUN npm install gulp-cli -g
RUN npm install
COPY src src/
CMD ["php-fpm"]
EXPOSE 9000
when I start the container using docker-compose up --build -d I get this message when I visit mysite.com (I have hidden the real domain for privacy):
File not found.
Inspecting the nginx log using sudo docker logs -f nginx I get:
[error] 30#30: *39 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 2.38.140.109, server: mysite.com, request: "GET / HTTP/2.0", upstream: "fastcgi://172.28.0.7:9000", host: "mysite.com"
mysite.com 2.38.140.109 - - [29/Mar/2021:17:52:31 +0000] "GET / HTTP/2.0" 404 16 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36 Edg/89.0.774.63"
For fix this problem, I have update the nginx.tmpl with this one.
The problem now is that when I load my site all the static files such js / image / css return 404.
What can I do for fix this?
In order to serve the static files from a container using the nginx proxy, the nginx proxy must have access to files statically... it would not 'proxy' but 'read' and serve the files... which means you must mount the app files into the proxy...
I would recommend you put up a new, light and clean nginx behind the nginx proxy along with your php-fpm service so you can have full management of your routes/locations,
First of all I have created a docker-compose.yml in my app that contains the following:
version: '3.9'
services:
php-fpm:
container_name: boilerplate_app
restart: always
build:
context: .
dockerfile: ./docker/php-fpm/Dockerfile
volumes:
- ./src:/var/www/html
nginx:
image: nginx:stable-alpine
container_name: boilerplate_nginx
restart: always
volumes:
- ./src:/var/www/html
- ./docker/nginx/nginx.conf:/etc/nginx/nginx.conf
- ./docker/nginx/sites/:/etc/nginx/sites-available
- ./docker/nginx/conf.d/:/etc/nginx/conf.d
depends_on:
- php-fpm
environment:
VIRTUAL_HOST: example.com
LETSENCRYPT_HOST: example.com
LETSENCRYPT_EMAIL: info#example.com
database:
container_name: boilerplate_db
restart: always
build:
context: ./docker/database
environment:
- MYSQL_DATABASE=boilerplate
- MYSQL_USER=user
- MYSQL_PASSWORD=secret
- MYSQL_ROOT_PASSWORD=secret
volumes:
- ./docker/database/data.sql:/docker-entrypoint-initdb.d/data.sql
phpmyadmin:
container_name: boilerplate_phpmyadmin
image: phpmyadmin/phpmyadmin
restart: always
ports:
- 8088:80
environment:
- PMA_HOST=database
- MYSQL_USER=user
- MYSQL_PASSWORD=secret
- MYSQL_ROOT_PASSWORD=secret
depends_on:
- database
networks:
default:
external:
name: proxy
then, inside my app directory I have created the nginx configurations structure:
app
docker
nginx
conf.d
default.conf
sites
default.conf
nginx.conf
where I have conf.d:
upstream php-upstream {
server php-fpm:9000;
}
then I have sites:
server {
root /var/www/html/public;
index index.php;
location ~ [^/]\.php(/|$) {
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
fastcgi_pass php-upstream;
fastcgi_index index.php;
}
}
and nginx.conf:
user nginx;
worker_processes 4;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
access_log /var/log/nginx/access.log;
# Switch logging to console out to view via Docker
#access_log /dev/stdout;
#error_log /dev/stderr;
sendfile on;
keepalive_timeout 65;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-available/*.conf;
}
the site load now but there are some issues:
Performance issue: we have two nginx layer here and the load time is increased a lot
If I browse some url like example.com/admin I get 404 from nginx, guess it's a configuration issue on my own

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.

Bad Gateway nginx for docker container

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.

Categories