I have started using Docker since yesterday but I cant figure out to fix the mysql connection refused error. I was wondering if any of you guys could help me as I don’t know wat to do hahah
This is my docker-compose.yml
version: '3'
services:
db:
image: mysql:latest
environment:
MYSQL_DATABASENAME: my_test_database
MYSQL_HOST: mysql
MYSQL_PORT: 3306
MYSQL_USER: develop
MYSQL_PASSWRD: develop
MYSQL_ALLOW_EMPTY_PASSWORD: 1
ports:
- '3306:3306'
www:
depends_on:
- db
image: php:8.0.30-apache
volumes:
- "./:/var/www/html"
ports:
- 80:80
- 443:443
phpmyadmin:
depends_on:
- db
image: phpmyadmin/phpmyadmin
environment:
- PMA_HOST=db
- PMA_PORT=3306
and this is inside my dockerfile
FROM php:8.0.30-apache
RUN docker-php-ext-install mysqli && docker-php-ext-enable mysqli
hope anyone could help me thanks!
I was expecting a connection with my database server
EDIT: changed PWA
to PMA
2
Answers
The issue is here:
Here
localhost
refers to IP-address of thephpmyadmin
service’s container, and not to your machine’s IP. To make other services communicate todb
service you have to providedb
service’s name wherever you would provide IP-address of the machine runningmysql
.Also note, that you mistyped the name of the environment variable(should be
PMA_HOST
according to phpMyAdmin docker image page)So, the correct configuration would be:
There are a number of issues in your docker-compose.yml.
db
serviceMYSQL_DATABASENAME
should beMYSQL_DATABASE
MYSQL_PASSWRD
should beMYSQL_PASSWORD
MYSQL_HOST
environment variable as it causes issues with this image (see docs)MYSQL_PORT
is unnecessary, as you are setting it to the default port (not even sure this env var is used)phpmyadmin
service8080:80
– will make PMA available athttp://localhost:8080
on host.PMA_PORT
is unnecessary, as 3306 is the default.Your dockerfile is not being used by anything as the
www
service is usingimage
, notbuild
(read this example in the docs)It’s a good idea to set a restart policy.
If you want to use your
Dockerfile
for yourwww
service, change it to (assumingDockerfile
is in project root):Docs: Compose Build Specification