I want to set up local DOCKER env for multiple projects under the same network. I want to have the main docker-compose config file which will create Nginx proxy and PHP container. And many child docker-compose files with own Nginx. I want all child Nginx containers listen to the main PHP container. But for some reason, child Nginx containers failing with error host not found in upstream "app-php" in /etc/nginx/vhost.d/site.cong25
main docker-compose:
version: '3.5'
services:
app-nginx_proxy:
build: ./nginx-proxy
image: app/nginx-proxy:latest
container_name: app-nginx_proxy
ports:
- "80:80"
volumes:
- /var/run/docker.sock:/tmp/docker.sock:ro
networks:
- appnetwork
app-php:
image: app/php:latest
container_name: app-php
build:
context: ./php-fpm
dockerfile: Dockerfile
expose:
- 9000
volumes:
- app_volume:/var/www/
networks:
- appnetwork
volumes:
app_volume:
driver: local
driver_opts:
type: bind
device: "$PWD/../services"
o: bind
networks:
appnetwork:
driver: bridge
and child docker-compose:
version: '3.5'
services:
app-admin-nginx:
build: ./nginx
image: app-admin/nginx:latest
container_name: app-admin-nginx
volumes:
- app_admin_volume:/var/www/app-admin
networks:
appnetwork:
aliases:
- app-admin.v2.local
environment:
- VIRTUAL_HOST=app-admin.v2.local
volumes:
app_admin_volume:
driver: local
driver_opts:
type: bind
device: "$PWD/../services/app-admin"
o: bind
networks:
appnetwork:
external: true
also, site.conf:
server {
listen 80;
server_name app-admin.v2.local;
root /var/www/app-admin/web;
index index.html index.htm 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_log /var/log/nginx/abboom_admin_error.log;
access_log /var/log/nginx/abboom_admin_access.log;
error_page 404 /index.php;
sendfile off;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass app-php:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_index index.php;
}
location ~ /\.ht {
deny all;
}
}
If I move all configs to the single docker-compose file all is working fine, but it's not flexible to use a single file, as I want add the ability for developers to run only apps they need and not all the app we have
You main nginx container has dependency on app-php service. You need to start app-php before nginx.
You also need to make sure both the services run in same project so that they use same network. You can use -p option of docker-compose.
https://docs.docker.com/compose/reference/overview/
Usage: docker-compose [-f ...] [options] [COMMAND] [ARGS...]
docker-compose -h|--help
Options: -f, --file FILE Specify an alternate compose
file
(default: docker-compose.yml)
-p, --project-name NAME Specify an alternate project name
(default: directory name)
You can also use custom network to start service from different compose files in same network.
https://docs.docker.com/compose/networking/#specify-custom-networks
Related
hi everyone i'm working on deploy laravel project on docker and use shadowd as waf, laravel project work probly on docker with nginx but when i try to configure show i find this output:
nginx configuration:
server {
listen 80;
listen [::]:80;
server_name localhost;
root /var/www/html/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;
fastcgi_param PHP_ADMIN_VALUE "auto_prepend_file=/usr/share/shadowd/shadowd.php";
include fastcgi_params;
}
location ~ /\.(?!well-known).* {
deny all;
}
}
to deploy my laravel project i create docker compose file and dockefile
docker compose:
version: "3.9" # optional since v1.27.0
networks:
laravel:
services:
nginx:
image: nginx:stable-alpine
container_name: laravel_nginx
ports:
- "8088:80"
volumes:
- "./:/var/www/html"
- "./nginx/nginx.conf:/etc/nginx/conf.d/default.conf"
# depends_on:
# - php
# - mysql
networks:
- laravel
mysql:
image: mysql:8
container_name: laravel_mysql
ports:
- "3366:3306"
volumes:
- "./mysql:/var/lib/mysql"
restart: unless-stopped
tty: true
environment:
- MYSQL_DATABASE=ttDataBase
- MYSQL_USER=root
- MYSQL_ROOT_PASSWORD=root
- SERVICE_TAGS=dev
- MYSQL_SERVICE_NAME=mysql
networks:
- laravel
php:
build:
context: .
dockerfile: Dockerfile
container_name: laravel_php
ports:
- "9000:9000"
volumes:
- "./:/var/www/html"
networks:
- laravel
dockerfile
FROM php:8.0.3-fpm-alpine3.12
RUN docker-php-ext-install pdo pdo_mysql
RUN apk add libzip-dev
RUN docker-php-ext-install zip
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
two networks are on docker :
please someone help me.
I am trying to run nginx and php in docker-compose. I am able to build the containers and when I browse to an html file in my browser nginx serves it. But when I try to browse a php file I just get 502 Bad Gateway.
I'm aware that it may be something to do with the server name in my nginx configuration file, since the tutorial I got it from mentions this needs to be correct. But I've tried changing it to 'localhost' and 'ip-of-my-dockerhost' with same result.
I'm also aware I should look at the log files. However I am a bit of a linux novice. When I open Portainer, go to the container called Web and execute a shell, if I go:
cd /var/log/nginx ; ls
I see two files called access.log and error.log
however if I type
cat error.log
nothing happens! I get a new blank line with a blinking cursor that never outputs anything. The shell is then 'frozen' until I press CTRL-C to kill the task.
Contents of docker-compose.yml
version: "3.7"
#############################
#
# NETWORKS
#
#############################
networks:
main:
external:
name: main
default:
driver: bridge
###########################
#
# SERVICES
#
###########################
services:
# All services / apps go below this line
portainer:
container_name: Portainer
image: portainer/portainer:latest
restart: unless-stopped
command: -H unix:///var/run/docker.sock
networks:
- main
ports:
- "$PORTAINER_PORT:9000"
volumes:
- /var/run/docker.sock:/var/run/docker.sock:ro
- $DATADIR/portainer:/data # Change to local directory if you want to save/transfer config locally
environment:
- TZ=$TZ
<<snip as don't believe relevant - a bunch of other containers here such as influxdb, radarr etc>>
web:
container_name: Web
image: nginx:latest
networks:
- main
ports:
- 81:80
environment:
- PUID=$PUID
- PGID=$PGID
volumes:
- $CONFIGDIR/nginx/site.conf:/etc/nginx/conf.d/default.conf
- $DATADIR/nginx/code:/code
php:
container_name: php
image: php:7-fpm
networks:
- main
ports:
- 9090:9000
environment:
- PUID=$PUID
- PGID=$PGID
volumes:
- $CONFIGDIR/nginx/site.conf:/etc/nginx/conf.d/default.conf
- $DATADIR/nginx/code:/code
contents of $CONFIGDIR/nginx/site.conf
server {
index index.php index.html;
server_name 192.168.1.7; ## This is the IP address of my docker host but I've also tried 'localhost' and '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:9090;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
9090 is an exposed port. That means you can reach localhost:9090 for the PHP container but not for internal container communication.
Change your site.conf:
fastcgi_pass php:9090; => fastcgi_pass php:9000;
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;
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.
I'm trying to set up a containerized version of a basic Craft CMS set up using Docker with Php and Nginx. Craft can't seem to write to folders outside of my Nginx Document Root. (See bottom of post for more details) I'm using Docker-Compose and this is my current Docker-compose.yml:
nginx:
build: ./compose/nginx/
ports:
- 80:80
links:
- php
volumes_from:
- app
php:
build: ./compose/php/
expose:
- 9000
links:
- mysql
volumes_from:
- app
app:
image: php:7.0-fpm
volumes:
- ./app:/var/www/html
- ./vendors:/var/www
command: "true"
mysql:
image: mysql:latest
volumes_from:
- data
environment:
MYSQL_ROOT_PASSWORD: secret
MYSQL_DATABASE: project
MYSQL_USER: project
MYSQL_PASSWORD: project
data:
image: mysql:latest
volumes:
- /var/lib/mysql
command: "true"
phpmyadmin:
image: phpmyadmin/phpmyadmin
ports:
- 8080:8080
links:
- mysql
environment:
PMA_HOST: mysql
This set up works perfectly, here are my two build files for Nginx
- DockerFile:
FROM nginx:latest
COPY ./default.conf /etc/nginx/conf.d/default.conf
Default.conf
server {
listen 80 default_server;
root /var/www/html;
index index.html 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; }
access_log off;
error_log /var/log/nginx/error.log error;
sendfile off;
client_max_body_size 100m;
location ~ \.php$ {
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_intercept_errors off;
fastcgi_buffer_size 16k;
fastcgi_buffers 4 16k;
}
location ~ /\.ht {
deny all;
}
}
Everything works near flawlessly, until I try to deploy Craft CMS, I understand that I don't have all the php-extensions, but I don't believe that affects my problem. I keep getting the Error, /var/www/craft/config isn't writable by PHP. Please fix that. Thus I am assuming that this is an error with permissions with Nginx and PHP. Although, I have tried opening a bash shell inside both of my nginx and php containers and chmod 777 craft/config folder, and I still get this error, thus I am a bit confused as to how to fix it.
EDIT 1 ------
I was able to fix this problem by implementing the following into my Dockerfile within my php build profile.
RUN usermod -u 1000 www-data
RUN usermod -G staff www-data
RUN chown -R www-data:www-data /var/www
I'm not sure if this is the safest way.
In your docker-compose.yml app container definition the volume declaration ./vendors:/var/www effectively overwrites ./app:/var/www/html. /var/www/craft/ therefore very likely does not exist.
Volume declarations for app should look as follows instead:
app:
image: php:7.0-fpm
volumes:
- ./app:/var/www/html
- ./vendors:/var/www/vendors
command: "true"