docker mysql connection refused while connecting from PHP script - php

Basically I want to run the LAMP stack in an Docker container.
All services are so far running but as soon I try to connect to an mysql from within an PHP script I get following error:
Warning: mysqli::__construct(): (HY000/2002): Connection refused
docker-compose.yml
version: '3.2'
services:
php:
build: ./php/
networks:
- backend
volumes:
- './public/:/var/www/html'
apache:
build: ./apache/
depends_on:
- php
- mysql
networks:
- frontend
- backend
ports:
- '8080:80'
volumes:
- './public/:/var/www/html'
mysql:
image: 'mysql:5.6.40'
ports:
- "3306:3306"
networks:
- backend
environment:
- MYSQL_DATABASES=test
- MYSQL_ROOT_PASSWORD=root
- MYSQL_HOST=127.0.0.1
- MYSQL_PORT=3306
- MYSQL_USER=user
- MYSQL_PASSWORD=password
- MYSQL_MY_DATABASE=test
networks:
frontend: null
backend: null
Dockerfile for PHP:
FROM php:7.2.7-fpm-alpine3.7
RUN apk update; \
apk upgrade;
RUN docker-php-ext-install mysqli
I had tested it with the following approach:
docker exec -ti <mysql_docker> bash
and used the same crendatial for entering the mysql mode:
mysql -uroot -h127.0.0.1 -P3306 -p
and there is not problem with it.
So what may cause the above connection refused?

Related

Can't create database with postgres and docker

I was trying to create a database in my app using command:
php bin/console doctrine:database:create
After that I got an error:
An exception occurred in driver: SQLSTATE[08006] [7] could not connect to server: Connection refused Is the server running on host "127.0.0.1" and accepting TCP/IP connections on port 5432?
My docker-compose file looks like that:
version: '3'
services:
php:
container_name: symfony_php
build:
context: ./php
volumes:
- ./symfony/:/var/www/symfony/
depends_on:
- database
networks:
- symfony
database:
container_name: symfony_postgres
image: postgres
restart: always
hostname: symfony_postgres
environment:
POSTGRES_DB: symfony_db
POSTGRES_USER: root
POSTGRES_PASSWORD: root
ports:
- "5432:5432"
volumes:
- ./postgres:/var/lib/postgres
networks:
- symfony
pgadmin:
container_name: symfony_pgadmin
image: dpage/pgadmin4
restart: always
ports:
- "5555:80"
depends_on:
- database
links:
- database
environment:
PGADMIN_DEFAULT_EMAIL: pgadmin4#pgadmin.org
PGADMIN_DEFAULT_PASSWORD: root
networks:
- symfony
nginx:
container_name: symfony_nginx
image: nginx:stable-alpine
build:
context: ./nginx
dockerfile: Dockerfile-nginx
volumes:
- ./symfony:/var/www/symfony
- ./nginx/default.conf:/etc/nginx/conf.d/default.conf
depends_on:
- php
- database
networks:
- symfony
networks:
symfony:
I'm not using any postgres config file, according to Symfony documentation, there's a config line in .env:
DATABASE_URL="postgresql://root:root#127.0.0.1:5432/symfony_db?serverVersion=11&charset=utf8"
I run netstat command and I got:
tcp 0 0 0.0.0.0:5432 0.0.0.0:* LISTEN
I think there's a problem with binding my localhost address where my app's server is running to postgres. Does anyone know, how I can fix it?
In docker-compose all services can be reached by their names.
You need to change DB host address to database - as a service name in docker-compose
DATABASE_URL="postgresql://root:root#database:5432/symfony_db?serverVersion=11&charset=utf8"

Can't connect to MYSQL container from Drupal container through drush

I try to figure out this problem since a long time now and I've got to admit that I'm out of responses.
Here is the case:
docker-compose.yml
version: "3"
services:
mysql:
image: mysql:8.0
container_name: mysql
hostname: mysql
command: --default-authentication-plugin=mysql_native_password
restart: unless-stopped
env_file: .env
volumes:
- db-data:/var/lib/mysql
networks:
- internal
drupal:
build:
context: .
dockerfile: Dockerfile
container_name: drupal
depends_on:
- mysql
restart: unless-stopped
volumes:
- ./web:/var/www/html/web:rw
- ./vendor:/var/www/html/vendor:rw
- ./drush:/var/www/html/drush
- drupal-data:/var/www/html
- ./php-conf/php.ini:/usr/local/etc/php/php.ini
networks:
- internal
- external
webserver:
image: nginx:1.19.1-alpine
container_name: webserver
depends_on:
- drupal
restart: unless-stopped
ports:
- 80:80
- 443:443
volumes:
- drupal-data:/var/www/html
- ./web:/var/www/html/web:rw
- ./nginx-conf/snippets/self-signed.conf:/etc/nginx/conf.d/snippets/self-signed.conf
- ./nginx-conf/snippets/ssl-params.conf:/etc/nginx/conf.d/snippets/ssl-params.conf
- ./nginx-conf/sites-available/default.conf:/etc/nginx/conf.d/default.conf
- ./ca-certificates/qiminfo-docker.dev+4.pem:/etc/ssl/certs/qiminfo-docker.dev+4.pem
- ./ca-certificates/qiminfo-docker.dev+4-key.pem:/etc/ssl/private/qiminfo-docker.dev+4-key.pem
networks:
- external
networks:
external:
driver: bridge
internal:
driver: bridge
volumes:
drupal-data:
db-data:
Dockerfile (only for drupal container)
FROM drupal:8.9.2-fpm-alpine
RUN apk add mysql-client && apk add openssh
I manage all dependencies via the mounted files in volumes (it works nice btw), but when I run drush through my host machine or through the container it doesn't see the Drupal instance (neither root or database) and I get the classic error message in this case ...
In BootstrapHook.php line 32:
[Exception]
Bootstrap failed. Run your command with -vvv for more information.
But, I use the Dockerfile to get mysql-client installed on drupal container and to allow connection from drupal container to mysql container. And when I try to connect to mysql (which has 'mysql' hostname) container database it works !
➜ docker exec -it drupal sh
/var/www/html # mysql -u drupal -p drupal -h mysql
Enter password:
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 13
Server version: 8.0.21 MySQL Community Server - GPL
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MySQL [drupal]>
The question is:
Why drush doesn't has access to mysql server container while mysql-cli does the job fine ?!
Please help T-T

PhpStorm + Docker + Xdebug + DB SSH tunnel

Locally I have following docker-compose configuration:
nginx:
build:
context: ./nginx
ports:
- "80:80"
volumes:
- ./../logs:/home/web/logs/
- ./../:/home/web/my-website.com/
depends_on:
- php
php:
build:
context: ./php
volumes:
- ./../:/home/web/my-website.com/
working_dir: /home/web/my-website.com/
expose:
- "8123"
php container has Xdebug installed into it, I can easily connect to it from PhpStorm.
I have remote ClickHouse database which is connected via SSH Tunnel. When I start my container I just go into my container and execute:
ssh -4 login#host.com -p 2211 -L 8123:localhost:8123 -oStrictHostKeyChecking=no -Nf
After this, my site is able to use this connection, but when I execute console command
./yii analysis/start-charts 003b56fe-db47-11e8-bcc0-52540010e5bc 205
from PhpStorm, I'm getting an exception:
Failed to connect to 127.0.0.1 port 8123: Connection refused
If I jump into the container and launch the same command, everything works fine.
What's wrong? Why PhpStorm doesn't see my SSH tunnel?
I've got an answer on "superuser" site: https://superuser.com/questions/1374463/phpstorm-docker-xdebug-db-ssh-tunnel/1375961#1375961
Besides, I've added ports node to my php container definition, now it's the following:
php:
build:
context: ./php
volumes:
- ./../:/home/web/my-website.com/
working_dir: /home/web/my-website.com/
expose:
- "8123"
ports:
- "8123:8123"
depends_on:
- redis
- mysql

MySQLi not found in docker php container

Error:
Fatal error: Uncaught Error: Class 'mysqli' not found in /var/www/html/index.php:16 Stack trace: #0 {main} thrown in /var/www/html/index.php on line 16
I made the index.php file and PDO mysql connection example code. Everything working as expected, except MySQLi connection. I added docker-php-ext-install run command to docker file but that did not help.
Here is my PHP docker file:
FROM php:7.0-fpm
RUN docker-php-ext-install pdo pdo_mysql mysqli
RUN docker-php-ext-enable mysqli
My docker-compose.yml file:
nginx:
build: ./nginx/
container_name: nginx-container
ports:
- 8080:80
links:
- php
volumes_from:
- app-data
php:
build: ./php/
container_name: php-container
expose:
- 9000
links:
- mysql
volumes_from:
- app-data
app-data:
#image: php:7.0-fpm
build: ./php/
container_name: app-data-container
volumes:
- ./www/html/:/var/www/html/
- ./nginx/nginx:/etc/nginx/conf.d
command: “true”
mysql:
#image: mysql:5.7
build: ./mysql/
container_name: mysql-container
expose:
- 3306
ports:
- 3306:3306
volumes_from:
- mysql-data
environment:
MYSQL_ROOT_PASSWORD: secret
MYSQL_DATABASE: zavrel_db
MYSQL_USER: user
MYSQL_PASSWORD: password
mysql-data:
image: mysql:latest
container_name: mysql-data-container
volumes:
- /var/lib/mysql
command: "true"
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: phpmyadmin-container
ports:
- 8888:80
links:
- mysql
environment:
PMA_HOST: mysql
Thank you.
Try change name of packet
php-mysqli
I solved the problem by deleting all images and containers. After that, I started and everything is fine.
docker rmi $(docker images -a -q)
docker rm $(docker ps -a -q)
I found the problem, the package is RUN pdo_mysqliso
i've added this RUN docker-php-ext-install pdo_mysqli in my docker-compose

How can I connect php-apache and MySQL using Docker?

I have a Wordpress site on live server and I want to create a LAMP stack locally with Docker to test things.
I pull the images of php:7.0-apache and mysql:5.7, the same versions on live.
I create a MySQL container:
docker run -d --name achi-mysql -e MYSQL_ROOT_PASSWORD=123456 mysql:5.7
I create a php & apache container and link it with MySQL:
docker run -d --name achi-php-apache --link achi-mysql:mysql -p 8080:80 -v /home/achi/workspace/web/wordpress-template/:/var/www/html php:7.0-apache
I get the following error on localhost:8080:
Fatal error: Uncaught Error: Call to undefined function mysql_connect() in /var/www/html/wp-includes/wp-db.php:1564 [...]
Do I link these two containers the wrong way?
Your problem is not the connection between your containers. The problem is your PHP / Apache container which doesn't support mysqli (or PDO MySQL). WordPress can't find another function to connect with a MySQL database or your MySQL container. Instead WordPress is using a deprecated and removed (since PHP 7.0) mysql_ function per default. You need to install at least mysqli on your PHP container (explained below).
I also recommend to use a docker-compose file to install and run all containers with one command.
To create the containers you want, you can use the following docker-compose.yml file:
version: "3"
services:
achi-php-apache:
build:
context: ./
container_name: achi-php-apache
ports:
- "8080:80"
volumes:
- /home/achi/workspace/web/wordpress-template:/var/www/html:rw
depends_on:
- achi-mysql
networks:
- wp-net
achi-mysql:
image: mysql:5.7
container_name: achi-mysql
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: 123456
MYSQL_DATABASE: wp-dbname
volumes:
- wp-mysql-data:/var/lib/mysql
networks:
- wp-net
networks:
wp-net:
driver: bridge
volumes:
wp-mysql-data:
You need the following Dockerfile on the same directory as the docker-compose.yml file:
FROM php:7.0-apache
RUN docker-php-ext-install -j$(nproc) mysqli
This Dockerfile installs the missing mysqli extension so WordPress can use it.
You can also use PDO MySQL instead of mysqli. In this case you can use the following Dockerfile:
FROM php:7.0-apache
RUN docker-php-ext-install -j$(nproc) pdo
RUN docker-php-ext-install -j$(nproc) pdo_mysql
Now you can execute the command docker-compose up inside the folder where the docker-compose.yml file is located. After creating the container and running you should be able to access the WordPress site (<ip-or-hostname>:8080).
On the wp-config.php file you need to use the following constants:
define('DB_NAME', 'wp-dbname');
define('DB_USER', 'root');
define('DB_PASSWORD', '123456');
define('DB_HOST', 'achi-mysql');
You can also use the official WordPress image to install WordPress. In this case you can use the following docker-compose.yml file:
version: "3"
services:
achi-php-apache:
image: wordpress:4.9.4-php7.0-apache
container_name: achi-php-apache
ports:
- "8080:80"
environment:
WORDPRESS_DB_HOST: achi-mysql
WORDPRESS_DB_USER: root
WORDPRESS_DB_PASSWORD: 123456
WORDPRESS_DB_NAME: wp-dbname
volumes:
- /home/achi/workspace/web/wordpress-template:/var/www/html:rw
depends_on:
- achi-mysql
networks:
- wp-net
achi-mysql:
image: mysql:5.7
container_name: achi-mysql
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: 123456
MYSQL_DATABASE: wp-dbname
volumes:
- wp-mysql-data:/var/lib/mysql
networks:
- wp-net
networks:
wp-net:
driver: bridge
volumes:
wp-mysql-data:
The simplest way is to use docker-compose to link all your docker instances together rather than linking through the docker command. Here is a sample docker-compose.yml file that should do what you want:
version: '2'
services:
achi-php-apache:
image: php:7.0-apache
ports:
- "8080:80"
volumes:
- /home/achi/workspace/web/wordpress-template/:/var/www/html
links:
- achi-mysql
achi-mysql:
image: mysql:5.7
volumes:
- /var/lib/mysql
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: 123456
MYSQL_USER: someuser
MYSQL_PASSWORD: somepassword
MYSQL_DATABASE: somedefaultdatabase

Categories