Three container docker-composition : PHP can't connect to Maria DB - php

My docker-compose.yml :
version: '3'
services:
php-apache:
build:
context: ./php-apache
ports:
- 80:80
volumes:
- ./DocumentRoot:/var/www/html
links:
- 'db'
networks:
- default
db:
image: mariadb:10.1
volumes:
- ./db:/var/lib/mysql
- ./dump.sql:/docker-entrypoint-initdb.d/dump.sql
ports:
- "3306:3306"
environment:
TZ: "Europe/London"
MYSQL_ALLOW_EMPTY_PASSWORD: "no"
MYSQL_ROOT_PASSWORD: "rootpwd"
MYSQL_USER: 'testuser'
MYSQL_PASSWORD: 'testpassword'
MYSQL_DATABASE: 'testdb'
networks:
- default
phpmyadmin:
image: phpmyadmin/phpmyadmin
links:
- 'db'
ports:
- 8000:80
environment:
MYSQL_USER: 'testuser'
MYSQL_PASSWORD: 'testpassword'
MYSQL_ROOT_PASSWORD: 'rootpwd'
PMA_HOST: db
PMA_PORT: 3306
This is based on a tutorial I found online.
When I run it, I can see that the PHP container is up. I can see that the db and php-mydmin are up. In fact I can successfully access the db from php-myadmin using the testuser / testpassword credentials.
But when I try to access the db from the main PHP application using
$conn = mysqli_connect("localhost","testuser",'testpassword','testdb');
I get
Warning: mysqli_connect(): (HY000/2002): No such file or directory in /var/www/html/index.php on line 8
Is this a problem with the docker config? Can anyone see anything missing?

Related

Getting error mysqli::real_connect(): (HY000/2002): No such file or directory when trying to use phpma after running docker-compose script

I am getting the following error after running my docker-compose script:
mysqli::real_connect(): (HY000/2002): No such file or directory.
I've looked at Getting error mysqli::real_connect(): (HY000/2002): No such file or directory when I try to access my project on live server for answers but the given solutions don't work for me.
My docker-compose script looks like this:
version: '3'
services:
# Database
db:
image: mysql:latest
volumes:
- database:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: Password
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: Password
networks:
- wpsite
# phpMyAdmin
phpmyadmin:
depends_on:
- db
image: phpmyadmin/phpmyadmin:latest
restart: always
ports:
- '8080:80'
environment:
PMA_HOST: db
MYSQL_ROOT_PASSWORD: Password
networks:
- wpsite
volumes: ['./config:/etc/phpmyadmin/']
# Wordpress
wordpress:
depends_on:
- db
image: wordpress:latest
ports:
- '80:80'
restart: always
volumes: ['./wordpress:/var/www/html']
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: Password
networks:
- wpsite
networks:
wpsite:
volumes:
database:
So far I've tried changing $cfg['Servers'][$i]['host'] = 'localhost'; in the /etc/phpmyadmin/config.inc.php to both $cfg['Servers'][$i]['host'] = '127.0.0.1'; and $cfg['Servers'][$i]['host'] = 'db';. Neither of these options fix the problem. I made sure they get changed properly after rerunning the script.

Is it possible to use the same docker container for multiple websites

until a new version of php or mysql comes out I generally always use the same setup, so at the moment I create 3 containers for every site I create:-
Site 1:-
version: '3.1'
services:
php:
build:
context: .
dockerfile: .docker/Dockerfile
image: site1
ports:
- 8000:80
restart: always
volumes:
- .:/var/www/html
networks:
- site1
mysql:
image: mysql:8.0
volumes:
- db_data:/var/lib/mysql
restart: always
ports:
- 3306:3306
environment:
MYSQL_DATABASE: site1
MYSQL_USER: root
MYSQL_PASSWORD: password
MYSQL_ROOT_PASSWORD: password
networks:
- larastock
phpmyadmin:
depends_on:
- site1
image: phpmyadmin/phpmyadmin
restart: always
ports:
- 8001:80
environment:
PMA_HOST: mysql
MYSQL_ROOT_PASSWORD: password
networks:
- site1
networks:
site1:
volumes:
db_data:
Site 2:-
version: '3.1'
services:
php:
build:
context: .
dockerfile: .docker/Dockerfile
image: site2
ports:
- 8000:80
restart: always
volumes:
- .:/var/www/html
networks:
- site2
mysql:
image: mysql:8.0
volumes:
- db_data:/var/lib/mysql
restart: always
ports:
- 3306:3306
environment:
MYSQL_DATABASE: site1
MYSQL_USER: root
MYSQL_PASSWORD: password
MYSQL_ROOT_PASSWORD: password
networks:
- larastock
phpmyadmin:
depends_on:
- site2
image: phpmyadmin/phpmyadmin
restart: always
ports:
- 8001:80
environment:
PMA_HOST: mysql
MYSQL_ROOT_PASSWORD: password
networks:
- site2
networks:
site2:
volumes:
db_data:
They are identical except for the network but this means I have 6 containers when really I only need 3. Is it possible to tell site 2 to use the services that site 1 created or does this go against the whole point of docker?
So what I would like is to change:-
Site 1 uses php, mysql and phpmyadmin. Site 2 uses php1, mysql1, phpmyadmin1.
to:
both site 1 and site 2 using the same container's but having their own database:-
Site 1 uses php, mysql (database site1) and phpmyadmin. Site 2 uses php, mysql (database site2) and phpmyadmin

Access denied to MySQL database in Docker

My Docker (v.2) docker-compose.yml file has 2 Ubuntu images and 2 MariaDB images:
app:
container_name: app
image:php-apache-dev:ubuntu-16.04
links:
- mysql
depends_on:
- mysql
ports:
- '8443:443'
volumes:
- .:/app
environment:
docker: 'true'
PHP_DEBUGGER: 'xdebug'
WEB_DOCUMENT_ROOT: '/app/public'
WEB_NO_CACHE_PATTERN: '\.(.*)$$'
working_dir: '/app'
CONF_FILE: 'conf/local/develop.php'
test:
container_name: test
image:php-apache-dev:ubuntu-16.04
links:
- mysql_test
depends_on:
- mysql_test
ports:
- '8444:443'
volumes:
- .:/app
environment:
docker: 'true'
WEB_DOCUMENT_ROOT: '/app/public'
WEB_NO_CACHE_PATTERN: '\.(.*)$$'
working_dir: '/app'
CONF_FILE: 'conf/local/test.php'
mysql:
image: mariadb:latest
ports:
- '3306:3306'
environment:
MYSQL_ROOT_PASSWORD: 'dev'
MYSQL_DATABASE: 'dev'
MYSQL_USER: 'dev'
MYSQL_PASSWORD: 'dev'
mysql_test:
image: mariadb:latest
ports:
- '3307:3306'
environment:
MYSQL_ROOT_PASSWORD: 'dev'
MYSQL_DATABASE: 'dev_test'
MYSQL_USER: 'dev'
MYSQL_PASSWORD: 'dev'
One is for development, one is for testing. When I access the first one (on localhost:4443) it works. When I access the second one (on localhost:4444) I am able to view the website, but it denies access to the user:
Access denied for user 'dev'#'%' to database 'dev_test'
My configuration for the second site is set up to access the second MySQL port/user:
'db_port' => env('DB_PORT', '3307'),
'db_name' => env('DB_NAME', 'dev_test'),
I tried the answer to this question but without success. If I docker exec bash into the test database image, and run SHOW GRANTS FOR dev, it says:
GRANT ALL PRIVILEGES ON `dev_test`.* TO 'dev'#'%'
You probably forgot to use the correct MySQL host in your second site.

Unable to connect phpmyadmin to database using docker compose

May be this question asked few times before but I did't get a valid answer which can solve my problem.
I am trying to run phpmyadmin in docker on different container using docker-compose but It always through the following error:
#2002 - Connection refused — The server is not responding (or the local server's socket is not correctly configured).
My docker compose file contains the following code:
version: "2"
services:
web:
build: .
ports:
- "80:80"
networks:
- web
volumes:
- .:/code
restart: always
db:
image: "mysql:5"
volumes:
- ./mysql:/etc/mysql/conf.d
environment:
MYSQL_ROOT_PASSWORD: toor
MYSQL_DATABASE: phpapp
networks:
- web
restart: always
phpmyadmin:
image: phpmyadmin/phpmyadmin
environment:
PMA_PORT: 3306
PMA_HOST: db
PMA_USER: root
PMA_PASSWORD: toor
ports:
- "8000:80"
restart: always
networks:
- web
networks:
web:
driver: bridge
In web container I am trying to connect with database and it works fine, but the problem occur with phpmyadmin connection
Any help would be appreciated. :)
Interestng enough, I have your compose-file running and phpmyadmin is accessible
from host.
Had to change port 8000 to 8004 though (port 8000 is occupied on my host).
In case your db-container does not start fast enough for phpmyadmin to connect, I suggest adding depends_on into phpmyadmin service. Makes sure db starts before phpmyadmin.
phpmyadmin:
image: phpmyadmin/phpmyadmin
environment:
PMA_PORT: 3306
PMA_HOST: db
PMA_USER: root
PMA_PASSWORD: toor
ports:
- "8004:80"
restart: always
depends_on:
- db
networks:
- web
Please show logs from docker-compose up if problem persists.
Now you need to add command to mysql service for connecting to phpmyadmin.
command: --default-authentication-plugin=mysql_native_password
version: "2"
services:
db:
image: mysql:latest
command: --default-authentication-plugin=mysql_native_password
ports:
- "3306:3306"
environment:
MYSQL_DATABASE: drupal
MYSQL_USER: user
MYSQL_PASSWORD: test
MYSQL_ROOT_PASSWORD: test
volumes:
- ./dump:/docker-entrypoint-initdb.d
- /var/lib/mysql
networks:
- default
phpmyadmin:
image: phpmyadmin/phpmyadmin
links:
- db:db
ports:
- 8000:80
environment:
PMA_HOST: db
MYSQL_USER: user
MYSQL_PASSWORD: test
MYSQL_ROOT_PASSWORD: test

Problems PHP MARIADB PHPMYADMIN using docker-compose

I've been this last two weeks searching how to link my phpmyadmin and my database, to create a PHP development workspace, through docker-compose but I am not able to connect to the database through the phpmyadmin port.
version: '3'
services:
servicio_php:
image: php:7.3-rc-apache
volumes:
- ./web/:/var/www/html
expose:
- "80"
ports:
- "80:80"
links:
- db
db:
image: mariadb:latest
restart: always
volumes:
- ./Volumenes/mysql:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: root123
MYSQL_USER: user
MYSQL_PASSWORD: user123
MYSQL_DATABASE: bbdd1
expose:
- "3306"
ports:
- "3306:3306"
phpmyadmin:
image: phpmyadmin/phpmyadmin:latest
container_name: phpmyadmin
environment:
PMA_ARBITRARY: 1
MYSQL_ROOT_PASSWORD: root123
MYSQL_USERNAME: user
PMA_HOST: db
MYSQL_PASSWORD: user123
restart: always
ports:
- "8080:80"
volumes:
- ./sessions:/sessions/
depends_on:
- db
links:
- db
I am using windows 10 version of dockers 18.06.1-ce and also when i run docker-compose up i see there are some probles with Innodb but ive read is not a problem.
I am not able to see whats wrong and probably I am not the only one with this problem.

Categories