Unable to connect phpmyadmin to database using docker compose - php

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

Related

Communication between a docker application and another one on the local system [duplicate]

This question already has answers here:
From inside of a Docker container, how do I connect to the localhost of the machine?
(40 answers)
Closed 5 months ago.
How can a Docker container communicate with a local application that is not in Docker?
I have a Symfony container (PHP) and I want to communicate with a nodejs application for socket (so I need input and output for the 1337 port).
How can I make a communication between these applications?
My docker-file:
version: "3.7"
volumes:
db-data:
networks:
dev:
driver: bridge
services:
mariadb:
container_name: symfony_mariadb
image: mariadb:10.9.3-jammy
restart: always
environment:
MYSQL_USER: root
MYSQL_PASSWORD: root
MYSQL_DATABASE: symfony
MYSQL_ROOT_PASSWORD: root
volumes:
- db-data:/var/lib/mysql
expose:
- 3306
ports:
- "3306:3306"
phpmyadmin:
container_name: phpmyadmin
depends_on:
- mariadb
restart: always
image: phpmyadmin/phpmyadmin
environment:
PMA_HOST: mariadb
PMA_USER: root
PMA_PASSWORD: root
ports:
- "${PHPMYADMIN_PORT:-8081}:80"
redis:
container_name: redis
image: redis:7.0.5-alpine3.16
ports:
- "6379:6379"
volumes:
- ./data/redis:/data/redis
nginx:
build:
context: .docker/nginx
restart: on-failure
volumes:
- ./:/var/www/server:cached
- ./.docker/nginx/server.conf:/etc/nginx/conf.d/server.conf:cached
ports:
- "${NGINX_PORT:-8000}:80"
depends_on:
- php
- mariadb
- redis
php:
build:
context: .docker/php
restart: on-failure
ports:
- 5000:8000
volumes:
- ./:/var/www/server:cached
- ./:/var/www/symfony
- ./logs/symfony:/var/www/symfony/app/logs
- ./.docker/php/php.ini:/usr/local/etc/php/php.ini:ro
depends_on:
- mariadb
- redis
user: "${ID_USER:-1001}:${ID_USER:-1001}"
Normally yo can see host machine port from docker, you try using host IP ?
Note: if this host IP is in a different network segment of internals IPs form dockers, like 192.168.1.28

Docker-Compose Cannot Connect to Server w/ mysqli_connect

I have recently installed docker-compose on Ubuntu. I can connect to my local host, phpmyadmin and stuff. However, when I try to connect to it with mysqli_connect, I can reach the server but I cannot access it with Warning: mysqli_connect(): (HY000/2002).
Here is the php code I am using to try to connect:
// username and password belong to the phpmyadmin, before that I tried docker for both username and pwd.
Edit: staj is the name of database i created in phpmyadmin.
<?php
$dbHost="127.0.0.1";
$dbUser="root";
$dbPass="root";
$dbName="staj";
$conn=mysqli_connect($dbHost, $dbUser, $dbPass, $dbName);
if ($conn){
}else{
die("Connection Failed!");
}
?>
And here is my docker-compose.yml:
version: "3"
services:
webserver:
build:
context: ./bin/${PHPVERSION}
container_name: '${COMPOSE_PROJECT_NAME}-${PHPVERSION}'
restart: 'always'
ports:
- "${HOST_MACHINE_UNSECURE_HOST_PORT}:80"
- "${HOST_MACHINE_SECURE_HOST_PORT}:443"
links:
- database
volumes:
- ${DOCUMENT_ROOT-./www}:/var/www/html
- ${PHP_INI-./config/php/php.ini}:/usr/local/etc/php/php.ini
- ${VHOSTS_DIR-./config/vhosts}:/etc/apache2/sites-enabled
- ${LOG_DIR-./logs/apache2}:/var/log/apache2
environment:
APACHE_DOCUMENT_ROOT: ${APACHE_DOCUMENT_ROOT-/var/www/html}
PMA_PORT: ${HOST_MACHINE_PMA_PORT}
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
database:
build:
context: "./bin/${DATABASE}"
container_name: '${COMPOSE_PROJECT_NAME}-${DATABASE}'
restart: 'always'
ports:
- "127.0.0.1:${HOST_MACHINE_MYSQL_PORT}:3306"
volumes:
- ${MYSQL_INITDB_DIR-./config/initdb}:/docker-entrypoint-initdb.d
- ${MYSQL_DATA_DIR-./data/mysql}:/var/lib/mysql
- ${MYSQL_LOG_DIR-./logs/mysql}:/var/log/mysql
environment:
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
MYSQL_DATABASE: ${MYSQL_DATABASE}
MYSQL_USER: ${MYSQL_USER}
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: '${COMPOSE_PROJECT_NAME}-phpmyadmin'
links:
- database
environment:
PMA_HOST: database
PMA_PORT: 3306
PMA_USER: root
PMA_PASSWORD: ${MYSQL_ROOT_PASSWORD}
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
MYSQL_USER: ${MYSQL_USER}
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
UPLOAD_LIMIT: ${UPLOAD_LIMIT}
MEMORY_LIMIT: ${MEMORY_LIMIT}
ports:
- '${HOST_MACHINE_PMA_PORT}:80'
volumes:
- /sessions
- ${PHP_INI-./config/php/php.ini}:/usr/local/etc/php/conf.d/php-phpmyadmin.ini
redis:
container_name: '${COMPOSE_PROJECT_NAME}-redis'
image: redis:latest
ports:
- "127.0.0.1:${HOST_MACHINE_REDIS_PORT}:6379"
Solution:
$dbHost="database";
$dbUser="docker";
$dbPass="docker";
$dbName="docker";
It turns out that docker for username and pwd was correct but the host name was incorrect.
I got that "docker" from the .env.
Seems you've already found the way to connect, but here's why it works that way:
You're using distinct docker containers for each service, which is correct, but that means your application isn't trying to connect locally (127.0.0.1) because the database isn't running on the same container.
Docker Compose networking allows hosts defined in the same docker-compose file to communicate with each other by using the host names which are defined as the service name (the next level under service), so in your case you have hosts webserver, database, phpmyadmin, and redis.
Simply updating the host name you're trying to connect to from 127.0.0.1 to database should be all you need to do.

Connect docker-compose to remote database in Laravel/Ubuntu

I am working on a laravel project where I successfully configured the docker-compose with laravel using built in images (nginx, mysql, php etc).
The containers are working fine even the data persistence is working correct. But now i want to connect the docker-compose to remote database rather then using the sql container database.
It can the the localhost database that is on my local system may be in xampp or it can be an AWS remote database. In simple words the docker should pick the database outside of container.
I have tried different solution using the IP address and make changes to .env and docker-compose.yml but i didn't find any solution.
Here is my default configurations for docker-compose.yml :
version: '3'
networks:
laravel:
services:
site:
build:
context: .
dockerfile: nginx.dockerfile
container_name: nginx
ports:
- 81:80
volumes:
- ./src:/var/www/html:delegated
depends_on:
- php
- mysql
- phpmyadmin
networks:
- laravel
mysql:
image: mysql:5.7.29
container_name: mysql
restart: unless-stopped
tty: true
ports:
- 3307:3306
environment:
MYSQL_DATABASE: test_db
MYSQL_USER: root
MYSQL_PASSWORD: secret
MYSQL_ROOT_PASSWORD: secret
SERVICE_TAGS: dev
SERVICE_NAME: mysql
networks:
- laravel
volumes:
- ./mysql:/var/lib/mysql
phpmyadmin:
image: phpmyadmin/phpmyadmin
restart: always
container_name: phpmyadmin
depends_on:
- mysql
ports:
- "8081:80"
environment:
PMA_HOST: mysql
MYSQL_ROOT_PASSWORD: secret
UPLOAD_LIMIT: 1G
networks:
- laravel
php:
build:
context: .
dockerfile: php.dockerfile
container_name: php
volumes:
- ./src:/var/www/html:delegated
networks:
- laravel
volumes:
mysql:
And this is how my .env looks like:
DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=test_db
DB_USERNAME=root
DB_PASSWORD=secret
As mentioned above it is working fine but i am confused about how to integrate/configure my local database or remote database like AWS with docker-compose in laravel. I don't want to push my data with the sql image.
I would appreciate if someone might help me in this regard about what changes are required and where to implement them.
Thanks

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

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?

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