Docker + Nginx host not found in upstream in docker - php

I'm trying to run Nginx and PHP-FPM containers with network_mode: host in docker-compose, when defining a network_mode as "bridge" it works fine, but when defining network_mode as "host" I receive :
nginx: [emerg] host not found in upstream "ui" in /etc/nginx/conf.d/ui.conf:10
Everything works fine when setting network_mode to bridge, unfortunately I need to set it to host, as I need access to host network so I can access ueye camera.
This is my docker-compose file
version: '3'
services:
nginx:
image: nginx:alpine
container_name: nginx
tty: true
ports:
- "80:80"
- "443:443"
- "8000:8000"
volumes:
- ./../../ui/:/var/www/
- ./nginx/:/etc/nginx/conf.d/
network_mode: host
depends_on:
- ui
restart: always
ui:
build:
context: ./../../ui
dockerfile: Dockerfile
volumes:
- ./../../ui/:/var/www/
container_name: ui
tty: true
environment:
SERVICE_NAME: ui
working_dir: /var/www
network_mode: host
ports:
- "9000:9000"
The nginx conf file
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 ui: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;
}
}

While using host network mode, your container will see Docker host as localhost & not the container itself because it's using the host network. Try replacing ui with localhost in your nginx conf -
fastcgi_pass localhost:9000;

Related

Why doesn't work the nginx container in docker-compose vuejs, php, nginx?

I made the vuejs and php to work, but the nginx won't to start. If I click on the "start" button in the docker app, it immediately stopps.
In the docker app, I have this message by the nginx container (maybe it helps):
nginx: [emerg] host not found in upstream "app" in /etc/nginx/conf.d/nginx.conf:10
Here are the files:
vuejs Dockerfile:
FROM node:14
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 80
CMD ["npm", "run", "dev", "--", "--host"]
php Dockerfile
FROM php:8.0.2-fpm
WORKDIR /var/www
docker-compose.yml
services:
vue:
build:
context: ./frontend
dockerfile: Dockerfile
ports:
- '3001:3001'
container_name: vue_c
volumes:
- ./frontend:/app
- /app/node_modules
php:
build:
context: ./backend
dockerfile: Dockerfile
container_name: php_c
volumes:
- ./backend:/var/www
nginx:
image: nginx:1.19-alpine
container_name: nginx_c
expose:
- '8000'
ports:
- '80:80'
volumes:
- ./backend:/var/www
- ./backend/nginx:/etc/nginx/conf.d
nginx.conf:
server {
listen 80;
index index.php;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
error_page 404 /index.php;
root /var/www/public;
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 nginx 404 not found

Why I get this error when visit localhost:8080!
Any suggestion to solve this issue is welcome because I spend many time for it and I changed PHP docker file and nginx file many times, similar version with laravel framework is ok for me.
*1 open() "/var/www/html/myapp404" failed (2: No such file or directory)
Note that myapp folder contains index.php and other files but it is not a Laravel framework.
My docker compose:
version: '3.8'
services:
nginx:
build:
context: .
dockerfile: nginx.dockerfile
ports:
- 8080:80
depends_on:
- php
- mysql
php:
build:
context: .
dockerfile: php.dockerfile
container_name: php
volumes:
- ${PWD}/myapp:/var/www/html/myapp
working_dir: /var/www/html/myapp
ports:
- "9000:9000"
mysql:
image: yobasystems/alpine-mariadb
ports:
- "3308:3308"
volumes:
- ./mariadb/data:/var/lib/mysql
- ./mariadb/my.conf:/etc/my.cnf
environment:
MYSQL_ROOT_PASSWORD: "111"
MYSQL_DATABASE: myapp
MYSQL_USER: root
MYSQL_PASSWORD: "111"
restart: always
My default.conf => Nginx:
server {
listen 80;
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/html/myapp;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
# pass the PHP scripts to FastCGI server listening on php:9000
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;
}
}
nginx:
FROM nginx:stable-alpine
ADD nginx/default.conf /etc/nginx/conf.d/default.conf
php :
FROM dwchiang/nginx-php-fpm:latest
Thanks.

Running a different version of php per project directory on a LAMP stack enviroment

Is there a way to configure or a program that allow me to run a different version of php based on my project directories for example /project1 running on php 5 and /project2 running on php 7? I have the need to run both projects at the same time.
I followed a guide that used xampp but it didn't work always loaded the version of php that came with the xampp installer.
I'm open to suggestions of another programs that install that stack and allows that kind of configuration or pointed out to a guide to set it up with docker to which I have no experience.
You can run both of them simultaneously with simple docker-compose
version: '3'
services:
#PHP5 Service
app1:
build:
context: .
dockerfile: Dockerfile-php5
container_name: app1
restart: unless-stopped
tty: true
env_file:
- ./.env/php5.env
- ./.env/.env
working_dir: /var/www/project1
networks:
- app-network
volumes:
- ./project1:/var/www/project1
#PHP7 Service
app2:
build:
context: .
dockerfile: Dockerfile-php7
container_name: app2
restart: unless-stopped
tty: true
env_file:
- ./.env/php7.env
- ./.env/.env
working_dir: /var/www/project2
networks:
- app-network
volumes:
- ./project2:/var/www/project2
#Nginx Service
webserver:
image: nginx:alpine
container_name: webserver
restart: unless-stopped
tty: true
ports:
- "8081:8081" #For Project1
- "8082:8082" #For Project2
networks:
- app-network
volumes:
- ./project1:/var/www/project1
- ./project2:/var/www/project2
- ./nginx/conf.d:/etc/nginx/conf.d/
- ./nginx/log:/var/log/nginx
networks:
app-network:
driver: bridge
and you should use fastcgi to pass requests on your different apps as follow:
#Project1
server {
listen 8081;
index index.php index.html;
error_log /var/log/nginx/api-error-php5.log;
access_log /var/log/nginx/api-access-php5.log;
root /var/www/project1/public;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass app1: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;
}
}
and for project2
#Project2
server {
listen 8082;
index index.php index.html;
error_log /var/log/nginx/api-error-php7.log;
access_log /var/log/nginx/api-access-php7.log;
root /var/www/project2/public;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass app2: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;
}
}

ERR_EMPTY_RESPONSE with Docker and NGINX

I have a problem with my Docker and NGINX config?
Everything is running but my web page returns a ERR_EMPTY_RESPONSE
Here is my NGINX config:
server {
listen 8443;
server_name api.test.dv;
root /api/public;
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 32k;
fastcgi_buffers 8 16k;
internal;
}
location ~ \.php$ {
return 404;
}
}
My docker-compose :
version: "3"
services:
nginx:
image: "bitnami/nginx:latest"
ports:
- "11083:8443"
volumes:
- "./docker/nginx/vhost.conf:/opt/bitnami/nginx/conf/vhosts/app.conf"
- .:/api
depends_on:
- php
php:
build: "docker/php"
volumes:
- .:/api:cached
working_dir: /api
user: 1000:1000
depends_on:
- database
- redis
database:
image: "postgres:latest"
environment:
POSTGRES_PASSWORD: password
POSTGRES_DB: app
ports:
- "11032:5432"
redis:
image: "redis:3.2.11"
NGINX logs are good, no error.
I use Docker desktop on Mac OS.
I have really no idea about my problem.

Docker with PHP Nginx unable to load image or js - 404 not found

I am deploying 2 docker containers (nginx and php fpm) in my windows 10 pro local machine.
I can get php script executed correctly.
For example: http://localhost:8888/phpinfo.php --> this return correctly.
But other than php script (e.g. image, js or css) it giving me not found.
For example: http://localhost:8888/image.jpg --> not found
Whats possibly went wrong here?
here is my docker compose file:
version: '3.7'
services:
# The Web Server
web:
container_name: emm_web
build:
context: ./
dockerfile: web.dockerfile
volumes:
- ../log/:/var/log
ports:
- 8888:80
# The PHP Application
app:
container_name: emm_app
build:
context: ./
dockerfile: app.dockerfile
volumes:
- ../www/:/var/www
depends_on:
- web
environment:
- "DB_PORT=3306"
- "DB_HOST=database"
And here is my nginx vhost.conf:
server {
listen 80;
index index.php index.html;
root /var/www;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
location / {
try_files $uri /index.php?$args;
}
location ~ \.php$ {
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;
}
}
Thank you :)
You need to share files for nginx container. Try this:
# The Web Server
web:
container_name: emm_web
build:
context: ./
dockerfile: web.dockerfile
volumes:
- ../log/:/var/log
# !! nginx need this volume !!
- ../www/:/var/www:ro
ports:
- 8888:80
That readonly volume will make the files in the container available.

Categories