PHP application can't connect to MariaDB using Docker Compose - php

My application is set up using Docker Compose. My docker-compose.yml contents;
version: "3.1"
services:
nginx-proxy:
image: jwilder/nginx-proxy
container_name: nginx-proxy
ports:
- "80:80"
- "443:443"
volumes:
- /var/run/docker.sock:/tmp/docker.sock:ro
db:
image: mariadb:10.4
restart: always
ports:
- "3307:3306"
volumes:
- ./mysql:/var/lib/mysql
container_name: mariadb
environment:
- MYSQL_DATABASE=dbname
- MYSQL_PASSWORD=dbpass
- MYSQL_RANDOM_ROOT_PASSWORD=yes
- MYSQL_USER=dbuser
phpapp:
depends_on:
- nginx-proxy
- db
image: myhub/myrepo
restart: always
container_name: phpapp
expose:
- 80
- 443
environment:
- APP_ENV=prod
- APP_SECRET="secret"
- CORS_ALLOW_ORIGIN="^.*?$$"
- DATABASE_URL="mysql://dbuser:dbpass#db/dbname"
- VIRTUAL_HOST=phpapp.com
networks:
default:
external:
name: nginx-proxy
However, when I try to connect to the database in the application I get an error message; SQLSTATE[HY000] [2002] No such file or directory.
I can, however, connect on port 3307 on my localhost with the correct username and password. Also, when I access the container using docker exec -ti phpapp /bin/bash and run env I get all the environment variables listed and they are correct.
When I execute docker network inspect nginx-proxy I get;
[
{
"Name": "nginx-proxy",
"Id": "2529933d7e38cfba34e0e876e43a96fc4c8d7321c1e89048319ba804b00e1026",
"Created": "2018-12-12T05:04:01.9044879Z",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": {},
"Config": [
{
"Subnet": "172.18.0.0/16",
"Gateway": "172.18.0.1"
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {},
"Options": {},
"Labels": {}
}
]
The containers array is empty, however a default (external) network is set in the docker-compose.yml.

Welcome to stackoverflow :)
I think maybe it is not working for db container not linked from phpapp container. Can you try like this?
version: "3.1"
services:
nginx-proxy:
image: jwilder/nginx-proxy
container_name: nginx-proxy
ports:
- "80:80"
- "443:443"
volumes:
- /var/run/docker.sock:/tmp/docker.sock:ro
db:
image: mariadb:10.4
restart: always
ports:
- "3307:3306"
volumes:
- ./mysql:/var/lib/mysql
container_name: mariadb
environment:
- MYSQL_DATABASE=dbname
- MYSQL_PASSWORD=dbpass
- MYSQL_RANDOM_ROOT_PASSWORD=yes
- MYSQL_USER=dbuser
phpapp:
depends_on:
- nginx-proxy
links:
- db
image: myhub/myrepo
restart: always
container_name: phpapp
expose:
- 80
- 443
environment:
- APP_ENV=prod
- APP_SECRET="secret"
- CORS_ALLOW_ORIGIN="^.*?$$"
- DATABASE_URL="mysql://dbuser:dbpass#db/dbname"
- VIRTUAL_HOST=phpapp.com
networks:
default:
external:
name: nginx-proxy
I moved db from depends_on to links. You must use db for hostname in database connection now.
I hope i could help.

Well, I've found the solution to this problem. However, it's not the one I expected. The database URL is defined as:
DATABASE_URL="mysql://dbuser:dbpass#db/dbname"
It should be without quotes, like this:
DATABASE_URL=mysql://dbuser:dbpass#db/dbname
Now everything seems to work, does anyone know when and why you can and can't use quotes?

Related

docker is not reading from docker-compose.yml

It seems that when i run Docker compose up, docker is not reading from docker-compose.yml.
It seems like it is loading images from cache or i don't think where is finding them.
Bellow is my docker-compose.yml
version: '3'
services:
httpd:
image: httpd:latest
user: root
ports:
- "80:80" # Default Apache port (Default on PHP 7.4)
- "8073:8073" # PHP 7.3 Apache port
- "8074:8074" # PHP 7.4 Apache port
- "8081:8081" # PHP 8.1 Apache port
volumes:
- ./:/var/www/html/myApp/:rw
- ./dev/Docker/httpd/httpd.conf:/usr/local/apache2/conf/httpd.conf
restart: on-failure
container_name: httpd
networks:
- mb-frontend
php8.1-fpm:
build: ./dev/Docker/php-fpm/8.1
user: root
environment:
XDEBUG_ENABLED: 1
XDEBUG_REMOTE_HOST: host.docker.internal
PHP_IDE_CONFIG: serverName=localhost
volumes:
- ./:/var/www/html/myApp/:rw
restart: on-failure
container_name: php8.1-fpm
networks:
- mb-frontend
- mb-backend
php7.4-fpm:
build: ./dev/Docker/php-fpm/7.4
user: root
environment:
XDEBUG_ENABLED: 1
XDEBUG_REMOTE_HOST: host.docker.internal
PHP_IDE_CONFIG: serverName=localhost
volumes:
- ./:/var/www/html/myApp/:rw
restart: on-failure
container_name: php7.4-fpm
networks:
- mb-frontend
- mb-backend
php7.3-fpm:
build: ./dev/Docker/php-fpm/7.3
user: root
environment:
XDEBUG_ENABLED: 1
XDEBUG_REMOTE_HOST: host.docker.internal
PHP_IDE_CONFIG: serverName=localhost
volumes:
- ./:/var/www/html/myApp/:rw
restart: on-failure
container_name: php7.3-fpm
networks:
- mb-frontend
- mb-backend
db:
image: mariadb:10.3.5
environment:
MYSQL_ROOT_PASSWORD: myPassword
MYSQL_USER: dev
MYSQL_PASSWORD: myPassword
ports:
- "3306:3306"
volumes:
- /root/Bureau/mysql:/var/lib/mysql/:rw
- ./dev/Docker/mariadb/conf.d/:/etc/mysql/conf.d/:rw
- ./dev/Docker/mariadb/config/init.sql:/docker-entrypoint-initdb.d/init.sql
restart: on-failure
container_name: db
networks:
- mb-backend
phpmyadmin:
image: phpmyadmin/phpmyadmin
environment:
PMA_HOST: db
volumes:
- /root/Bureau/phpmyadmin:/var/lib/mysql/
networks:
- mb-backend
depends_on:
- db
redis:
image: redis:6.2
container_name: redis
ports:
- "6379:6379"
networks:
- mb-backend
networks:
mb-frontend:
driver: bridge
mb-backend:
driver: bridge
I commented some images on docker-compose.yml but when i tape the command Docker compose up on terminal, all images even commented images are Up.
Can anyone help me how i force docker to read images from the edited docker-compose.yml
Good practice would be do use:
docker compose down
and then
docker compose up
UPDATE:
Next I would suggest to clean up your containers:
List all containers:
docker ps -a
Remove those you don't want because they might still be in the system
docker rm <CONTAINER ID/NAME>
How to Stop & Remove a running container by ID or Name?
The command is actually docker compose up.
The command docker-compose has been deprecated as of latest version. We can now use docker compose without the hyphen(-).
You can use docker-compose -f <path-to-compose-file> to pass in the compose file.
Example:
docker compose -f docker-compose.yml up
Reference documentation: https://docs.docker.com/compose/reference/

Docker compose: getaddrinfo for mariadb failed

I'm quite bad at creating docker-compose and I currently have a problem when I try to build a new project.
After the build I can't access my mariadb server within my php app container, I've got this error:
PDO::__construct(): php_network_getaddresses: getaddrinfo for mariadb
failed: Name does not resolve
So I know something is misconfigured but I don't know what, yet. I tried many changes but nothing that worked.
Here is my current docker-compose.yml:
version: "3.8"
networks:
# used by some services (php) to communicate with other docker-compose.yaml
censored.com:
external:
name: censored.com
services:
app:
build:
context: .
target: symfony_php
args:
- secret=id=composerauth,src=${HOME}/.composer/auth.json
restart: unless-stopped
healthcheck:
interval: 10s
timeout: 3s
retries: 3
start_period: 30s
environment:
APP_ENV: dev
HOST: www.censored.lan
networks:
- default
- censored.com
volumes:
- ./:/srv/app:rw,cached
- ./docker/php/conf.d/symfony.dev.ini:/usr/local/etc/php/conf.d/symfony.ini
- ${HOME}/.composer/auth.json:/root/.composer/auth.json
# If you develop on Linux, comment out the following volumes to just use bind-mounted project directory from host
- ./var/cache:/srv/app/cache:rw
- ./var/log:/srv/app/logs:rw
depends_on:
- mariadb
extra_hosts:
- www.censored.lan:127.0.0.1
nginx:
build:
context: .
target: symfony_nginx
args:
- secret=id=composerauth,src=${HOME}/.composer/auth.json
restart: unless-stopped
depends_on:
- app
environment:
NGINX_DOMAIN: www.censored.lan
ports:
- 8001:80
volumes:
- ./docker/nginx/templates/dev.conf.template:/etc/nginx/templates/default.conf.template:ro
- ./docker/nginx/rules/rules.dev.conf:/etc/nginx/rules.conf:ro
- ./public:/srv/app/public:ro
- ./src:/srv/app/src:ro
mariadb:
image: mariadb:10.7
environment:
MYSQL_ROOT_PASSWORD: changeme
MYSQL_DATABASE: database
MYSQL_USER: user
MYSQL_PASSWORD: changeme
networks:
- default
- censored.com
ports:
- '3307:3306'
restart: on-failure
volumes:
- db_data:/var/lib/mysql
volumes:
db_data: {}
Can someone help me to fix the issue please?
Thanks !
The problem wasn't related to my docker-compose configuration file.
My app is a PHP Symfony app and it was doing a "cache:clear" after the initial "composer install" during the build. The "cache:clear" was triggering calls to the database which wasn't ready yet.
To solve this I just had to set my mariadb version to my "DATABASE_URL" parameter in my Symfony app, to avoid useless database queries.

Getting error "cURL error 7: Failed to connect to localhost port" when calling API from another container

I am dockerizing an app that has several different services and I have used the Docker Compose file to achieve this Single docker-compose.yml file for all the services and one Dockerfile for each service.
Frontend(angular)[http://localhost:4200]
Backend(PHP)[http://localhost:80]
Backend Database(Mysql)
Integration Service(NodeJS)[http://localhost:4433]
Integration Service Database(MongoDB)
Authentication Layer(NodeJS)[http://localhost:4454]
Authentication Layer Database(Mysql)
So far I have successfully containerized all these services but when my "Backend(PHP)" container tries to call API from "Integration Service(NodeJS)" it throws the error which is cURL error 7: Failed to connect to localhost port 4433: Connection refused (see http://curl.haxx.se/libcurl/c/libcurl-errors.html).
services:
angular-service:
container_name: wms_frontend
build: ../frontend/.
ports:
- "4200:80"
php:
build:
context: .
image: wms-backend
networks:
- frontend
- backend
environment:
- MYSQL_HOST=wms-backend-mysql-app
- MYSQL_USER=wmsroot
- MYSQL_PASSWORD=pass
- MYSQL_DB=dbname
volumes:
- ./:/var/www/html/wms/backend
ports:
- "80:80"
container_name: wms-backend-php-app
mysql:
image: mysql:5.7
networks:
- backend
environment:
- MYSQL_ROOT_PASSWORD=rootpassword
- MYSQL_USER=wmsroot
- MYSQL_PASSWORD=pass
- MYSQL_DATABASE=
volumes:
- ./dump:/docker-entrypoint-initdb.d
container_name: wms-backend-mysql-app
phpmyadmin:
image: phpmyadmin/phpmyadmin:4.7
depends_on:
- mysql
networks:
- backend
ports:
- "40002:80"
environment:
- PMA_HOST=wms-backend-mysql-app
- PMA_PORT= 3306
volumes:
- /sessions
container_name: wms-backend-phpmyadmin-app
app:
container_name: wms_auth
restart: always
build: ../../wms_auth/.
networks:
- backend
volumes:
- ../../auth/./:/usr/src/app
- /usr/src/app/node_modules
ports:
- "4454:4454"
links:
- db
depends_on:
db:
condition: service_healthy
db:
image: mariadb
restart: always
ports:
- "3308:3306"
environment:
- MYSQL_ALLOW_EMPTY_PASSWORD= YES
- MYSQL_DATABASE=auth
- MYSQL_USER= root
- MYSQL_PASSWORD=
volumes:
- ../../auth/dump/:/docker-entrypoint-initdb.d
networks:
- backend
healthcheck:
test: ["CMD", "mysql", "-h", "db","-u","root", "mysql", "-e", "select 1"]
interval: 1s
retries: 20
wms_integration:
container_name: integration
restart: always
build: ../../wmsIntegeration/.
volumes:
- ../../wmsIntegeration/./:/usr/src/app
- /usr/src/app/node_modules
ports:
- "4433:4433"
networks:
- backend
links:
- db
depends_on:
db:
condition: service_healthy
wms_integration_db:
image: mongo
restart: always
networks:
- backend
ports:
- "27019:27017"
volumes:
- ../../wmsIntegeration/mongodb:/data/db
healthcheck:
test: echo 'db.runCommand("ping").ok' | mongo db:27017/config --quiet
interval: 10s
timeout: 10s
retries: 5
networks:
local:
driver: bridge
networks:
frontend:
backend:
Bridge all the networks as follows :
networks:
local:
driver: bridge
frontend:
driver: bridge
backend:
driver: bridge

docker-compose, WordPress and MariaDB, PHP Fatal error: Uncaught mysqli_sql_exception: Connection refused

I have a WP project with following docker-compose configuration. When I try to connect my http://localhost to access the installation, I get 500 and I read this in the log:
PHP Fatal error: Uncaught mysqli_sql_exception: Connection refused in /var/www/html/web/wp/wp-includes/wp-db.php
I assume there is something wrong with the ip or the host but the weird thing is that phpmyadmin is working fine, and it connects with mysql without issues.
version: '3.9'
services:
nginx:
image: nginx:latest
container_name: ${APP_NAME}-nginx
ports:
- '80:80'
volumes:
- "./nginx/:/etc/nginx/templates/"
- ./src:/var/www/html:rw,cached
- ./certs:/etc/certs
environment:
- "NGINX_ENVSUBST_TEMPLATE_SUFFIX=.conf"
- "DOMAIN=${DOMAIN}"
depends_on:
- wordpress
restart: always
mysql:
image: mariadb:latest
container_name: ${APP_NAME}-mysql
command: --lower_case_table_names=2
volumes:
- './data/db:/var/lib/mysql:delegated'
environment:
- MYSQL_ROOT_PASSWORD=${DB_ROOT_PASSWORD}
- MYSQL_DATABASE=${DB_NAME}
restart: always
ports:
- '3307:3306'
wordpress:
build:
context: .
dockerfile: Dockerfile
container_name: ${APP_NAME}-wordpress
volumes:
- ./src:/var/www/html:rw,cached
- ./config/php.ini:/usr/local/etc/php/conf.d/php.ini
environment:
XDEBUG_ENABLED: 1
XDEBUG_CONFIG: remote_host=host.docker.internal
PHP_IDE_CONFIG: serverName=localhost
env_file:
- src/.env
depends_on:
- mysql
restart: always
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: ${APP_NAME}-phpmyadmin
volumes:
- ./config/phpmyadmin.ini:/usr/local/etc/php/conf.d/phpmyadmin.ini
environment:
PMA_HOST: "${DB_HOST}"
PMA_PORT: 3306
MYSQL_ROOT_PASSWORD: "${DB_ROOT_PASSWORD}"
ports:
- '8083:80'
links:
- mysql:mysql
mailhog:
container_name: ${APP_NAME}-mailhog
image: mailhog/mailhog
ports:
- "8025:8025"
- "1025:1025"
composer:
image: composer
container_name: ${APP_NAME}-composer
working_dir: /var/www/html
restart: 'no'
volumes:
- ./src:/var/www/html:rw,cached
My wordpress .env, among the other settings, set the host like the following:
DB_HOST="mysql:3307"
But I also tried
DB_HOST="mysql:3306"
or simply
DB_HOST="mysql"
Do you have any suggestions?
Thanks.
your Wordpress Instance and MySQL Instance isn't connected. Try adding networks on the docker-compose.yml
nginx:
...
networks:
- your-network-name
mysql:
...
networks:
- your-network-name
wordpress:
...
networks:
- your-network-name
and on the bottom of the file add:
networks:
your-network-name:
driver: bridge
this is the way to configure docker to be connected each other

How to access host machine's mongo db inside docker container [duplicate]

This question already has answers here:
From inside of a Docker container, how do I connect to the localhost of the machine?
(41 answers)
Closed 2 years ago.
I am a bit new to docker. I am developing a laravel application, I have my app container running.
Inside app container I want to connect to my locally hosted mongo db server, which is generally localhost:27017.
This is my docker-compose.yml file
version: '3'
services:
#PHP Service
app:
build:
context: .
dockerfile: Dockerfile
image: digitalocean.com/php
container_name: bns_app
restart: unless-stopped
tty: true
environment:
SERVICE_NAME: app
SERVICE_TAGS: dev
working_dir: /var/www
volumes:
- ./:/var/www
- ./docker_files/php/local.ini:/usr/local/etc/php/conf.d/local.ini
networks:
- app-network
#Nginx Service
webserver:
image: nginx:alpine
container_name: bns_web
restart: unless-stopped
tty: true
ports:
- "80:80"
- "443:443"
volumes:
- ./:/var/www
- ./docker_files/nginx/conf.d/:/etc/nginx/conf.d/
networks:
- app-network
#MySQL Service
db:
image: mysql:5.7.22
container_name: bns_db
restart: unless-stopped
tty: true
ports:
- "3306:3306"
environment:
MYSQL_DATABASE: laravel
MYSQL_ROOT_PASSWORD: 12345
SERVICE_TAGS: dev
SERVICE_NAME: mysql
volumes:
- dbdata:/var/lib/mysql/
- ./docker_files/mysql/my.cnf:/etc/mysql/my.cnf
networks:
- app-network
#REDIS Service
redis:
build:
context: ./docker_files/redis
dockerfile: Dockerfile
container_name: bns_redis
volumes:
- ./docker_files/redis:/data
ports:
- "6379:6379"
networks:
- app-network
# Laravel Echo Server
laravel-echo-server:
build:
context: ./docker_files/laravel-echo-server
dockerfile: Dockerfile
container_name: bns_echo_server
volumes:
- ./laravel-echo-server.json:/var/www/laravel-echo-server.json:ro
ports:
- "6001:6001"
links:
- redis
networks:
- app-network
# PHP-WORKER
php-worker:
build:
context: ./docker_files/php-worker
dockerfile: Dockerfile
container_name: bns_worker
volumes:
- ./php-worker/supervisord.d:/etc/supervisord.d
depends_on:
- app
networks:
- app-network
#Docker Networks
networks:
app-network:
driver: bridge
#Volumes
volumes:
dbdata:
driver: local
So inside my app container when I run artisan commands such as docker-compose exec app php artisan migrate:fresh --seed mysql tables are migrated successfully but mongo db (which is not in any container but it is in host machine only) documents are not migrate/seeded and I get error :
No suitable servers found (`serverSelectionTryOnce` set): [connection refused calling ismaster on '172.18.0.3:27017']
at /var/www/vendor/mongodb/mongodb/src/functions.php:431
427| // TODO: PHPLIB-476: Read transaction read preference once PHPC-1439 is implemented
428| $readPreference = new ReadPreference(ReadPreference::RP_PRIMARY);
429| }
430|
> 431| return $manager->selectServer($readPreference);
432| }
433|
Exception trace:
1 MongoDB\Driver\Manager::selectServer(Object(MongoDB\Driver\ReadPreference))
/var/www/vendor/mongodb/mongodb/src/functions.php:431
2 MongoDB\select_server(Object(MongoDB\Driver\Manager), [])
/var/www/vendor/mongodb/mongodb/src/Database.php:419
Please use the argument -v to see more details.
This is my ENV file:
MONGO_DATABASE=exchange
MONGO_HOST=172.18.0.3 # << This is the IP address of my app container <<
MONGO_PORT=27017
MONGO_USERNAME=
MONGO_PASSWORD=
Please do help me. Where am I going wrong ?
I don't know how but using static ip as 172.17.0.1 worked for me.
Now my ENV file is:
MONGO_DATABASE=exchange
MONGO_HOST=172.17.0.1
MONGO_PORT=27017
MONGO_USERNAME=
MONGO_PASSWORD=
Got reference from here
It must be well documented.

Categories