docker-compose simple example with nginx php and mysql - docker-compose

Can someone help me to connect my PHP and MySQL
I did manage to make it up and running, connect to DB with MySQL Workbench but when I try PDO connect from PHP file it fails for some reason...
docker-compose
web:
image: nginx:latest
ports:
- "80:80"
volumes:
- ./:/var/www
- ./site.conf:/etc/nginx/conf.d/site.conf
links:
- php
php:
image: php:7-fpm
volumes:
- ./:/var/www
links:
- db
db:
image: mysql:5.7
volumes:
- /var/lib/mysql
environment:
- MYSQL_ROOT_PASSWORD=123456
- MYSQL_DATABASE=database
ports:
- "3306:3306"
site.conf
server {
index index.php index.html;
server_name lara.test;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /var/www;
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;
}
}
and index.php
<?php
$database = new PDO('mysql:host=localhost;dbname=database', "root", "123456");
echo "Connected to MySQL<br />";
?>
and error message:
Fatal error: Uncaught PDOException: could not find driver in
/var/www/index.php:3 Stack trace: #0 /var/www/index.php(3):
PDO->__construct('mysql:host=loca...', 'root', '123456') #1 {main}
thrown in /var/www/index.php on line 3
what do I miss in order to make this work?

Just change
$database = new PDO('mysql:host=localhost;dbname=database', "root",
"123456");
to
$database = new PDO('mysql:host=db;dbname=database', "root", "123456");
the name of host must same with the name of database image on docker-compose file.

Related

Issue with location proxy_pass setting "connect() failed (111: Connection refused) while connecting to upstream"

I have nginx and metabase in docker. When I try to access "https://MYDOMAIN/metabase" I get the error - connect() failed (111: Connection refused) while connecting to upstream, client: 60.243.254.30, server: MYDOMAIN, request: "GET /metabase/ HTTP/2.0", upstream: "http://127.0.0.1:3000/metabase/", host: "MYDOMAIN".
This works - "curl http://localhost:3000/metabase". This also works - "curl http://127.0.0.1:3000/metabase".
nginx is running fine - I am able to access other sites that are running.
What am I doing wrong in the configuration?
docker-compose.yml
-------------------
webserver:
image: nginx:1.20.0
restart: "no"
volumes:
- ./public:/var/www/html
- ./conf.d:/etc/nginx/conf.d
- ./sites-available:/etc/nginx/sites-available
- ./sites-enabled:/etc/nginx/sites-enabled
- ./ssl:/etc/nginx/ssl
ports:
- '80:80'
- '443:443'
metabase:
image: metabase/metabase:latest
container_name: metabase
restart: "no"
volumes:
- metabase-data:/LOCATION/metabase
ports:
- '3000:3000'
environment:
MB_SITE_URL: http://localhost:3000/metabase
MB_DB_TYPE: postgres
MB_DB_DBNAME: metabase
MB_DB_PORT: 5432
MB_DB_USER: xxxxx
MB_DB_PASS: yyyyyy
nginx conf.d/default.conf
--------------------------
upstream metabase {
server 127.0.0.1:3000;
}
server {
...
location /metabase/ {
proxy_pass http://metabase;
proxy_set_header Host $http_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-Proto $scheme;
break;
}
}

Spring boot controller with mongodb + mongo express (configured with docker-compose) returned an 'Authentication failed.' from postman

Im trying to create microservices with Spring Boot, so i prefered to use mongodb as a database for one of my microservices using docker compose :
Here is my docker-compose.yml file content :
mongo:
image: mongo
restart: always
environment:
MONGO_INITDB_ROOT_USERNAME: rootuser
MONGO_INITDB_ROOT_PASSWORD: rootpass
ports:
- 27017:27017
volumes:
- data:/data
mongo-express:
image: mongo-express
restart: always
ports:
- 8075:8081
environment:
ME_CONFIG_MONGODB_ADMINUSERNAME: rootuser
ME_CONFIG_MONGODB_ADMINPASSWORD: rootpass
ME_CONFIG_MONGODB_SERVER: mongo
And my application.yml content :
server:
port: 8098
spring:
application:
name: fraud
data:
mongodb:
host: localhost
port: 27017
username: rootuser
password: rootpass
database: test
authentication-database: admin
And i have already created my database "test" and the user "rootuser" is already exists in the admin database as shown below :
The Controller implementation :
#RestController
#RequestMapping("api/v1/fraud-check")
#AllArgsConstructor
public class FraudController {
FraudCheckService fraudCheckService;
#GetMapping(path = "{customerId}")
public FraudCheckResponse isFraudster(#PathVariable("customerId") Integer customerID){
boolean isFraudulentCustomer = fraudCheckService.isFraudulentCustomer(customerID);
return new FraudCheckResponse(isFraudulentCustomer);
}
}
but when i did a postman request i got this error below :
com.mongodb.MongoCommandException: Command failed with error 18 (AuthenticationFailed): 'Authentication failed.' on server localhost:27017. The full response is {"ok": 0.0, "errmsg": "Authentication failed.", "code": 18, "codeName": "AuthenticationFailed"}

error converting YAML to JSON: yaml: did not find expected key

I copy pasted a configMap file from an online tutorial and an error popped while trying to apply it. this is the file:
apiVersion: v1
kind: ConfigMap
metadata:
name: nginx-config
labels:
tier: backend
data:
config : |
server {
index index.php index.html;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /dir;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
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;
}
}
this is the error:
line 28: did not find expected key
text alignment of the below line is incorrect. edited your question and corrected the format. try now. it should work
$document_root$fastcgi_script_name;
Should be:
apiVersion: v1
kind: ConfigMap
metadata:
name: nginx-config
labels:
tier: backend
data:
config : |
server {
index index.php index.html;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /dir;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
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;
}
}
EDIT:
In your YAML the Kubernetes object definition in this case configmap, has an incorrect indentation.

NGINX reverse proxy not working to other docker container

I have a setup with two docker containers in a docker compose. Now I want to use proxy_pass feature from nginx to proxy the connection from nginx to other container.
docker compose
version: '3.4'
services:
reverse_proxy:
image: nginx:alpine
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf
- ./error.log:/var/log/nginx/error.log
ports:
- "8081:8081"
apigateway.api:
image: ${REGISTRY:-configurator}/apigateway.api:${TAG:-latest}
build:
context: .
dockerfile: src/Services/ApiGateway/ApiGateway.Api/Dockerfile
environment:
- ASPNETCORE_ENVIRONMENT=Development
- ASPNETCORE_URLS=http://0.0.0.0:80
ports:
- "58732:80"
nginx conf
worker_processes 1;
events {
multi_accept on;
worker_connections 65535;
}
http {
charset utf-8;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
server_tokens off;
log_not_found off;
types_hash_max_size 2048;
client_max_body_size 16M;
# MIME
include mime.types;
default_type application/octet-stream;
upstream apigateway {
server apigateway.api:58732;
}
server {
listen 8081;
# reverse proxy
location /configurator-api-gw/ {
proxy_pass http://apigateway/;
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;
}
}
}
When I now access http://localhost:8081/configurator-api-gw/swagger I'm getting following error in the error.log. I have tried also different approaches and some other examples but I don't get it why this is not working.
2019/03/08 06:30:29 [error] 7#7: *6 connect() failed (111: Connection
refused) while connecting to upstream, client: 172.28.0.1, server: ,
request: "GET /configurator-api-gw/swagger HTTP/1.1", upstream:
"http://172.28.0.5:58732/swagger", host: "localhost:8081"
I have solved the problem. The problem is with server apigateway.api:58732; Here it needs to be used Port 80 as this inside of the docker network.

Why my docker cannot process php application?

I have the following docker-compose file :
version: '2'
services:
phpfpm:
tty: true # Enables debugging capabilities when attached to this container.
image: 'bitnami/php-fpm:5.6'
labels:
kompose.service.type: nodeport
ports:
- 9000:9000
volumes:
- /usr/share/nginx/html:/app
networks:
- app-tier
nginx:
image: 'bitnami/nginx:latest'
depends_on:
- phpfpm
networks:
- app-tier
links:
- phpfpm
ports:
- '80:8080'
- '443:8443'
volumes:
- ./my_vhost.conf:/bitnami/nginx/conf/vhosts/vhost.conf
networks:
app-tier:
driver: bridge
and here's the contents of my_vhost.conf file :
server {
listen 0.0.0.0:8080;
root /app;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
location /evodms {
root /app/evodms;
try_files $uri $uri /evodms/index.php?$args;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass phpfpm:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
I have my applications within the /usr/share/nginx/html folder.
I have tried the following links :
http://localhost => works, shows nginx homepage
http://localhost/page.html => just an ordinary html file and works
http://localhost/phpinfo.php => shows the php informations and works
http://localhost/evodms, shows the following error messages from my
docker :
nginx_1 | nginx: [warn] conflicting server name "" on 0.0.0.0:8080,
ignored
nginx_1 | 2017/12/08 15:13:29 [warn] 24#0: conflicting
server name "" on 0.0.0.0:8080, ignored
nginx_1 | 2017/12/08 15:15:04 [error] 25#0: *1 FastCGI sent in
stderr: "PHP message: PHP Warning:
include(/usr/share/nginx/html/evodms/lib/LibCakePhp20unit/lib/Cake/Core/CakePlugin.php):
failed to open stream: No such file or directory in
/app/evodms/lib/LibCakePhp20unit/lib/Cake/Core/App.php on line 505
nginx_1 | PHP message: PHP Warning: include(): Failed opening
'/usr/share/nginx/html/evodms/lib/LibCakePhp20unit/lib/Cake/Core/CakePlugin.php'
for inclusion (include_path='.:/opt/bitnami/php/lib/php') in
/app/evodms/lib/LibCakePhp20unit/lib/Cake/Core/App.php on line 505
nginx_1 | PHP message: PHP Fatal error: Class 'CakePlugin' not
found in /app/evodms/app/Config/bootstrap.php on line 66" while
reading response header from upstream, client: 172.26.0.1, server: ,
request: "GET /evodms/ HTTP/1.1", upstream:
"fastcgi://172.26.0.2:9000", host: "localhost"
nginx_1 | 172.26.0.1 - - [08/Dec/2017:15:15:04 +0000] "GET /evodms/
HTTP/1.1" 500 5 "-" "Mozilla/5.0 (X11; Linux x86_64)
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100
Safari/537.36"
Any clue on what's going on for the issue on the last link?
I think you should try with webdevops images which already include php-fpm and a webserver.
version: '3'
services:
app:
image: webdevops/php-nginx:7.2
ports:
- 9080:80
volumes:
- ./:/app
environment:
- PHP_DEBUGGER=xdebug
- PHP_DISPLAY_ERRORS=1
- WEB_DOCUMENT_ROOT=/app/public