I have a problem with my docker configuration, my home page on localhost return always 502 Bad Gateway.
Here you can see the architecture of the projet :
https://i.stack.imgur.com/Bfc24.png
the four container running :
https://ibb.co/XpQvnXj
I can access to phpmyadmin :
https://ibb.co/FXLqg8B
But when I go to the nginx port I have this issue :
https://ibb.co/2vs72jF
And this is the contents of the files.
docker-compose.yml :
version: '3'
services:
php:
build:
context: ./php
ports:
- "9000:9000"
volumes:
- ../symfony:/var/www
nginx:
container_name: "nginx"
build:
context: ./nginx
volumes:
- ../symfony:/var/www
- ./nginx/nginx.conf:/etc/nginx/nginx.conf
- ./logs:/var/log
depends_on:
- php
ports:
- "8081:80"
db:
image: mysql:5.6
restart: always
environment:
MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
phpmyadmin:
image: phpmyadmin
restart: always
ports:
- 8080:80
environment:
PMA_HOST: db
depends_on:
- db
php/Dockerfile :
FROM php:8.0.3-fpm
RUN apt-get update \
&& apt-get install -y --no-install-recommends locales apt-utils git libicu-dev g++ libpng-dev libxml2-dev libzip-dev libonig-dev libxslt-dev;
RUN curl -LsS https://symfony.com/installer -o /usr/local/bin/symfony
RUN curl -sSk https://getcomposer.org/installer | php -- && \
mv composer.phar /usr/local/bin/composer
RUN docker-php-ext-configure intl
RUN docker-php-ext-install mysqli
RUN docker-php-ext-install pdo pdo_mysql gd opcache intl zip calendar dom mbstring zip gd xsl
RUN pecl install apcu && docker-php-ext-enable apcu
WORKDIR /var/www
CMD composer install ; php-fpm
EXPOSE 9000
nginx/Dockerfile :
FROM nginx:alpine
WORKDIR /var/www
CMD ["nginx"]
EXPOSE 80
nginx/nginx.conf :
user nginx;
worker_processes 4;
daemon off;
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;
sendfile on;
keepalive_timeout 65;
upstream phpserver {
server php:9000;
}
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name localhost;
root /var/www/public;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_pass phpserver;
fastcgi_index index.php;
fastcgi_buffer_size 128k;
fastcgi_buffers 4 256k;
fastcgi_busy_buffers_size 256k;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_read_timeout 600;
include fastcgi_params;
}
location ~ /\.ht {
proxy_buffer_size 128k;
proxy_buffers 4 256k;
proxy_busy_buffers_size 256k;
deny all;
}
}
}
error.log :
2021/10/13 13:53:04 [error] 25#25: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 192.168.16.1, server: localhost, request: "GET / HTTP/1.1", upstream: "fastcgi://192.168.16.3:9000", host: "localhost:8081"
2021/10/13 13:53:04 [error] 25#25: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 192.168.16.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", upstream: "fastcgi://192.168.16.3:9000", host: "localhost:8081", referrer: "http://localhost:8081/"
I'm new to Docker so the problem is probably obvious
Related
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
I know there's a ton of posts regarding 502 Bad Gateway, but I haven't been able to solve this problem. I'm using Docker Compose to create separate containers for Nginx and PHP-FPM.
Error I get loading PHP files in the browser (HTML files render fine):
tc-web | 2018/01/22 19:22:46 [error] 5#5: *4 connect() failed (111: Connection refused) while connecting to upstream, client: 172.18.0.1, server: localhost, request: "GET /info.php HTTP/1.1", upstream: "fastcgi://172.18.0.2:9000", host: "localhost:8080"
tc-web | 172.18.0.1 - - [22/Jan/2018:19:22:46 +0000] "GET /info.php HTTP/1.1" 502 575 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36"
I've tried tweaking the various configs, using Unix socket, etc., for hours, and I still get 502 errors with PHP files. Can you spot what's wrong?
Here are all required files..
docker-composer.yml:
version: '3'
services:
web:
build:
context: ./docker/nginx
image: tc-web:0.1.0
container_name: tc-web
volumes:
# test files
- ./temp.html:/var/www/html/index.html
- ./temp.php:/var/www/html/info.php
ports:
- 8080:80
depends_on:
- php-fpm
php-fpm:
build:
context: ./docker/php-fpm
image: tc-php:0.1.0
container_name: tc-php
volumes:
- ./temp.html:/var/www/html/index.html
- ./temp.php:/var/www/html/info.php
docker/nginx/Dockerfile:
FROM nginx:1.13.8
# Install programs
RUN apt-get update
RUN apt-get install -y nano && \
apt-get install -y git && \
apt-get install -y procps
RUN mkdir -p /var/www/html
COPY nginx.conf /etc/nginx/nginx.conf
COPY default.conf /etc/nginx/conf.d/default.conf
docker/nginx/nginx.conf:
user www-data;
worker_processes 1;
# 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;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/default.conf;
}
docker/nginx/default.conf:
server {
listen 80;
server_name localhost;
root /var/www/html;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~* \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php-fpm:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
docker/php-fpm/Dockerfile:
FROM php:7.0-fpm
# Install programs
RUN apt-get update
RUN apt-get install -y nano && \
apt-get install -y procps
RUN mkdir -p /var/www/html
COPY php-fpm.conf /usr/local/etc/php-fpm.conf
COPY www.conf /usr/local/etc/php-fpm.d/www.conf
docker/php-fpm/php-fpm.conf:
[global]
include=etc/php-fpm.d/www.conf
docker/php-fpm/www.conf:
[global]
;daemonize = no
; if we send this to /proc/self/fd/1, it never appears
error_log = /proc/self/fd/2
[www]
user = www-data
group = www-data
listen = 127.0.0.1:9000
;listen = /var/run/php-fpm/php7-fpm.sock
;listen.owner = www-data
;listen.group = www-data
;listen.mode = 0660
access.log = /proc/self/fd/2
pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
; Ensure worker stdout and stderr are sent to the main error log.
catch_workers_output = yes
Problem could be in the www.conf file.
You are listening to 127.0.0.1:9000 but this way the service won't be reachable outside the container.
Try binding to 0.0.0.0:9000:
listen = 0.0.0.0:9000
This may sound very obvious, but another common issue would be that php-fpm is not installed. This can happen even though systemctl might say that the service is running but it's not really running anything. In my case, I had also installed PHP 8 with it's own fpm package, so if you have multiple php installations, there could have been a mix up with which fpm version you installed.
I have two Laravel projects
Laravel 4 with 664 user:www-data permission
Laravel 5 with 664 user:www-data permission
Note: moduletwo is the alias being used in this virtual host to point to the Laravel 5 files.
I am trying to configure both on my local machine where the Laravel 4 is the main project and Laravel 5 will be used as an alias in Nginx virtual host.
Laravel 4 is working good and no issue with that.
But when I try to access the Laravel 5 module it says File not found.and here are the logs:
access.log:
GET `/moduletwo/index.php/LFiveRoute HTTP/1.1" 404 27 "http://www.Laravel.dev/LFourRoute`
error.log:
FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 172.22.0.1, server: www.Laravel.dev, request: "GET /moduletwo/index.php/LFiveRoute HTTP/1.1", upstream: "fastcgi://172.22.0.5:9000", host: "www.Laravel.dev", referrer: "http://www.Laravel.dev/LFourRoute
script.log:
/var/www/Laravel4/public/moduletwo/index.php > GET /moduletwo/index.php/LFiveRoute HTTP/1.1
Processes in Nginx Container
Nginx master/worker processes
ps -ef | grep 'nginx'
root 1 0 0 06:15 ? 00:00:00 nginx: master process nginx -g daemon off;
www-data 7 1 0 06:16 ? 00:00:00 nginx: worker process
root 19 8 0 07:45 ? 00:00:00 grep nginx
fpm Processes
ps -ef | grep 'fpm'
root 21 8 0 07:47 ? 00:00:00 grep fpm
Processes in PHP71 Container
Nginx master/worker processes
none
fpm Processes
ps -ef | grep fpm
root 1 0 0 06:15 ? 00:00:00 php-fpm: master process (/usr/local/etc/php-fpm.conf)
www-data 8 1 0 06:16 ? 00:00:01 php-fpm: pool www
www-data 9 1 0 06:17 ? 00:00:01 php-fpm: pool www
www-data 10 1 0 06:22 ? 00:00:01 php-fpm: pool www
root 22 13 0 07:49 ? 00:00:00 grep fpm
Docker Compose File:
version: '2'
services:
nginx:
container_name:
Laravel-nginx
restart: always
build:
context: ./
dockerfile: deploy/web.docker
volumes:
- ./:/var/www/Laravel4
- ./../Laravel_5:/var/www/Laravel5
- ./app/storage/logs/nginx:/var/log/nginx
ports:
- "80:80"
links:
- php71
networks:
Laravel_net:
ipv4_address: 172.22.0.2
database:
container_name:
Laravel-db
restart: always
image: mysql:5.7
environment:
- "MYSQL_ROOT_PASSWORD=secret"
- "MYSQL_ROOT_USER=root"
- "MYSQL_DATABASE=laravel_4"
ports:
- "3309:3306"
networks:
Laravel_net:
ipv4_address: 172.22.0.3
redis:
container_name:
Laravel-redis
image: redis:3.2
ports:
- "6372:6379"
networks:
Laravel_net:
ipv4_address: 172.22.0.4
php71:
container_name:
Laravel-php-71
restart: always
build:
context: ./
dockerfile: deploy/app.docker
volumes:
- ./:/var/www/Laravel4
- ./../Laravel_5:/var/www/Laravel5
links:
- database
- redis
environment:
- "DB_PORT=3306"
- "DB_HOST=database"
networks:
Laravel_net:
ipv4_address: 172.22.0.5
networks:
Laravel_net:
driver: bridge
ipam:
driver: default
config:
- subnet: 172.22.0.0/16
gateway: 172.22.0.1
web.docker file:
FROM nginx:1.10
EXPOSE 80
EXPOSE 443
ADD ./deploy/vhost.conf /etc/nginx/conf.d/default.conf
ADD ./deploy/nginx.conf /etc/nginx/nginx.conf
WORKDIR /var/www
app.docker file:
FROM php:7.0-fpm
RUN apt-get update && apt-get install -y libmcrypt-dev mysql-client \
&& docker-php-ext-install mcrypt pdo_mysql
WORKDIR /var/www
nginx.conf file
user www-data;
worker_processes 1;
pid /var/run/nginx.pid;
error_log /var/log/nginx/error.log warn;
events {
# A single worker process can handle 1024 connections
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
log_format scripts '$document_root$fastcgi_script_name > $request';
access_log /var/log/nginx/scripts.log scripts;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
# Load config files from the /etc/nginx/conf.d directory
# The default server is in conf.d/default.conf
include /etc/nginx/conf.d/*.conf;
}
vhost.conf file:
server {
listen 80;
server_name www.laravel.dev;
charset utf-8;
sendfile off;
client_max_body_size 100m;
index index.php;
root /var/www/Laravel4/public;
location ~ ^/moduletwo/(.+(?:css|js|woff|woff2|ttf))$ {
include fastcgi_params;
alias /var/www/Laravel5/public/$1;
#access_log off;
}
#moduletwo code in laravel5
location /moduletwo/ {
include fastcgi_params;
alias /var/www/Laravel5/public;
## Check for file existing and if there, stop ##
if (-f $request_filename) {
break;
}
## Check for file existing and if there, stop ##
if (-d $request_filename) {
break;
}
index index.php;
try_files $uri $uri/ #moduletwo;
# try_files $uri $uri/ /index.php?$query_string;
}
location #moduletwo {
include fastcgi_params;
rewrite /moduletwo/(.*)$ /moduletwo/index.php?/$1 last;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php71:9000;
fastcgi_index index.php;
set $php_root /var/www/Laravel4/public;
if ($request_uri ~ /moduletwo) {
set $php_root /var/www/Laravel5/public;
}
fastcgi_param PATH_TRANSLATED $php_root/index.php;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_param REMOTE_ADDR $http_x_real_ip;
include fastcgi_params;
fastcgi_intercept_errors off;
fastcgi_buffer_size 128k;
fastcgi_buffers 256 16k;
fastcgi_max_temp_file_size 0;
fastcgi_read_timeout 310;
}
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ /\.ht {
deny all;
}
}
So, anybody is there? Please look into it and help me to resolve the issue. Thank you so much.
I have created two docker images to match my needs, here is the nginx one:
FROM alpine:3.3
RUN apk add --update nginx
EXPOSE 80 443
CMD nginx -c /www/dev/nginx/conf/nginx.conf -g 'daemon off;'
then the php-fpm
FROM php:7.0-fpm
RUN apt-get update
RUN apt-get install -y apt-transport-https ca-certificates dcmtk libgdcm-tools wkhtmltopdf libdbd-freetds libfreetype6-dev \
libjpeg62-turbo-dev \
libmcrypt-dev \
libpng12-dev \
&& docker-php-ext-install -j$(nproc) iconv mcrypt \
&& docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \
&& docker-php-ext-install -j$(nproc) gd
RUN apt-get install -y imagemagick --fix-missing
RUN apt-get clean
RUN rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
WORKDIR /www/public
CMD rm /etc/freetds.conf
ADD freetds.conf /etc/freetds.conf
CMD rm /var/cache/apk/*
Then the run commands in the following order
docker create -p 9000:9000 --name php -v /www:/www:rw php
docker start php
docker create --privileged=true -p 80:80 -p 443:443 --name nginx -v /www:/www --link php:php nginx
docker start nginx
So far all works good, containers are running and I can even get static content from nginx owever any php script fails with
[error] 6#0: *3 connect() failed (111: Connection refused) while connecting to upstream, client: 172.17.0.1, server: platform.v2.vetology.net, request: "GET /index.php HTTP/1.1", upstream: "fastcgi://172.17.0.2:9000", host: "localhost.localdomain"
doing
cgi-fcgi -bind -connect 127.0.0.1:9000
from the host machine works and php responds so I know both containers ar working and responsive they just don't communicate.
I read in a different thread that the issue is caused by php and nginx using a different root directory but here is not the case as both have the /www directory mounted and /www/public contains the index.php file I am trying to open.
And here is the nginx.conf
user nobody;
error_log /www/dev/nginx/log/error.log;
pid /run/nginx.pid;
include /usr/share/nginx/modules/*.conf;
worker_processes 1;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /www/dev/nginx/log/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
include /etc/nginx/conf.d/*.conf;
default_type application/octet-stream;
gzip on;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
include /www/dev/nginx/conf/upstream.conf;
server {
listen 80;
listen 443 ssl;
listen [::]:443 default ipv6only=on;
listen [::]:80 default_server ipv6only=on;
ssl_certificate /www/dev/ssl/certificate.crt;
ssl_certificate_key /www/dev/ssl/key.key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers HIGH:!aNULL:!MD5;
root /www/public;
index index.php index.html index.htm;
server_name hostname.net;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
client_body_buffer_size 128k;
client_body_temp_path /www/dev/nginx/tmp/client_body_temp;
proxy_temp_path /www/dev/nginx/tmp/proxy_temp_path;
fastcgi_temp_path /www/dev/nginx/tmp/fastcgi_temp_path;
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param HTTP_PROXY "";
fastcgi_pass vetology;
fastcgi_index index.php;
include fastcgi_params;
}
}
}
Any ideas of what I maybe doing wrong?
First off, I'm not sure you want to start the nginx container with privileged. There's also no need to mount the php directive with the :rw switch. rw is the default mode.
Second, take a look at where you're telling nginx to send the php request -- specifically, which host, which is defined in nginx's fastcgi_pass directive. You have it sending the php request to a machine named vetology. But docker is linking the containers together with --link php:php. That means its taking the container named php and giving it the alias named php. That means it will be exposed to your nginx container as a machine named php.
You can try this out:
docker run -it --rm --link php:some_php nginx sh -c 'ping some_php'
Change
fastcgi_pass vetology;
to:
fastcgi_pass php:9000;
And your container should work.
For complex container assemblies like this one, have a look at docker-compose next.
I'm trying to create a development environnement for PHP project with Docker-compose.
I just created a docker-compose file that contains 3 docker:
Application: that mounts my code volume
Php: that runs php5-fpm
Nginx: that runs the nginx web server.
I can get an html file through my web server but when I try with a php file I get a 502 Bad Request error.
Error.log:
2016/09/19 16:51:18 [error] 8#0: *2 connect() failed (111: Connection refused) while connecting to upstream, client: 172.17.0.1, server: xxx, request: "GET /index.php HTTP/1.1", upstream: "fastcgi://172.17.0.3:9000", host: "xxx"
I think that nginx can't connect to php.
Here is my docker-compose file:
# import the code and bind it on /var/www
application:
build: code
volumes:
- ./sites:/var/www/
tty: true
# php container
php:
build: php-fpm
volumes_from:
- application
expose:
- 9000:9000
volumes:
- ./logs/php5-fpm/:/var/log
# nginx container
nginx:
build: nginx
ports:
- 80:80
links:
- php
volumes_from:
- application
volumes:
- ./logs/nginx:/var/log/nginx
php-fpm/Dockerfile:
FROM debian:jessie
RUN apt-get update && apt-get install -y php5-common php5-cli php5-fpm php5-mcrypt php5-mysql php5-gd php5-imagick php5-curl php5-intl php5-memcache php5-imap
RUN usermod -u 1000 www-data
CMD ["php5-fpm", "-F"]
EXPOSE 9000
nginx/Dockerfile:
FROM debian:jessie
RUN apt-get update && apt-get install -y nginx
ADD nginx.conf /etc/nginx
ADD test.conf /etc/nginx/sites-available/
RUN ln -s /etc/nginx/sites-available/test.conf /etc/nginx/sites-enabled/test.conf
RUN rm /etc/nginx/sites-enabled/default
RUN echo "upstream php-upstream { server php:9000; }" > /etc/nginx/conf.d/upstream.conf
RUN usermod -u 1000 www-data
CMD ["nginx"]
EXPOSE 80
nginx/nginx.conf:
user www-data;
worker_processes 4;
pid /run/nginx.pid;
events {
worker_connections 2048;
multi_accept on;
use epoll;
}
http {
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
access_log off;
error_log off;
gzip on;
gzip_disable "msie6";
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
daemon off;
nginx/test.conf:
server {
listen 80;
server_name xxx;
root /var/www/test;
location ~ \.php$ {
fastcgi_pass php:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
error_log /var/log/nginx/test_error.log;
access_log /var/log/nginx/test_access.log;
}
There is no error when launching the docker-compose.
Can someone help me ?
Thanks