Trying to access a database in docker containers and php - php

So I have the following docker-compose file and Dockerfile for php which I am using. These files are in a folder called cca4 and my php files are in a subdirectory of this called src. I am trying to access my mysql database that I am creating however I cannot select anything from it or insert anything. No matter what I do the queries will not work. Any idea on what is wrong? When I go into phpmyadmin I can see that my database is there however it is just not updating or being selected from. I am using nginx as the web server and php-fpm to communicate. I am doing this all from a Linux VM on GCP (Google Cloud Platform) so could be an issue with that maybe too. My php just cannot seem to pick up that there are any tables in the db or rows even after inserting them into phpmyadmin
version: '3.3'
services:
db:
image: mysql:latest
volumes:
- database:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: MyDBRoot123
MYSQL_DATABASE: assignment_four_data
MYSQL_USER: php
MYSQL_PASSWORD: php
container_name: db
networks:
- mynet
myphp:
build:
context: .
dockerfile: Dockerfile
depends_on:
- db
expose:
- 9000
restart: always
volumes:
- $PWD/src:/var/www/html
container_name: myphp
networks:
- mynet
mynginx:
depends_on:
- myphp
image: nginx:latest
ports:
- "8080:80"
restart: always
volumes:
- $PWD/src:/var/www/html
- $PWD/src/nginx.ini:/etc/nginx/conf.d/default.conf
container_name: mynginx
networks:
- mynet
phpMyAdmin:
depends_on:
- db
image: phpmyadmin:latest
restart: always
environment:
PMA_HOST: mysql
ports:
- "8082:80"
container_name: phpMyAdmin
networks:
- mynet
volumes:
database: {}
networks:
mynet:
driver: bridge
Here is the dockerfile.
FROM php:7.4-fpm
RUN pecl install igbinary \
&& pecl install redis \
&& docker-php-ext-install mysqli \
&& docker-php-ext-enable redis \
&& echo "extension=redis.so" > /usr/local/etc/php/conf.d/redis.ini
my php code.
<?php
$servername = "db";
$username = "php";
$password = "php";
$databse = "assignment_four_data";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $database);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = 'SELECT * FROM Users';
$result = $mysqli_query($conn, $sql);
if (!$result) {
printf("false");
}
printf("Selected returned %d rows. \n", mysqli_num_rows($result));
?>
The error I keep receiving in my db.php file
Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, bool given in /var/www/html/db.php on line 18
Selected returned 0 rows
Is this a docker problem or a mysqli problem? My database is definitely there in phpmyadmin but I just can't get my db.php to communicate with it. Since the mysqli_query is returning false then my query obviously isn't able to work correctly with the mysql database. Even after creating tables and inserting values in phpmyadmin it still cannot detect anything.

Related

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.

Can't connect to Postgres database using PHP PDO and Docker

I am running a Docker app that is built on three images: php:7.4-fpm, nginx:stable-alpine and postgres:alpine.
I was previously attempting to use the image php:7.4-fpm-alpine, but apparently it does not come with the Postgres PDO driver, which was causing a could not find driver error. This post explains more about that, and how to fix it. I followed the steps in that post, including creating a Dockerfile for my PHP FPM service (though I did not create a PHP CLI service), and that remedied my driver issues. However, I am now getting the following error when I try to connect to my PSQL database using PDO:
SQLSTATE[08006] [7] could not connect to server: Connection refused Is the server running on host "localhost" (127.0.0.1) and accepting TCP/IP connections on port 6432?
I know my database is running (on port 6432) because I can access it from my terminal and from Positco, and have created tables after my Docker app was created. I also tried to connect to a database on port 5432 (through which my Postgres desktop app is running), but that did not work either. I also tried to restart my Postgres container, but that did not help.
Here is my PHP:
class Database {
private $connection;
private static $options = array(
PDO::ATTR_EMULATE_PREPARES => FALSE,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
);
public function __construct() {
try {
$dsn = 'pgsql:host=localhost;port=6432;dbname=db_my_test_app';
$username = 'root';
$password = 'secret';
$connection = new PDO($dsn, $username, $password, self::$options);
$this->connection = $connection;
return $connection;
} catch (PDOException $e) {
exit($e->getMessage());
}
}
}
$database = new Database();
...and my docker-compose.yml file:
version: '3'
networks:
my_test_app:
services:
# nginx
nginx-service:
image: nginx:stable-alpine
container_name: nginx-container
ports:
- "8080:80"
volumes:
- ./app:/var/www/project
- ./nginx/default.conf:/etc/nginx/conf.d/default.conf
depends_on:
- php-fpm-service
- postgres-service
networks:
- my_test_app
# php
php-fpm-service:
build:
context: .
dockerfile: ./php-fpm/Dockerfile
container_name: php-fpm-container
ports:
- "9000:9000"
working_dir: /var/www/project
volumes:
- ./app:/var/www/project
networks:
- my_test_app
# postgres
postgres-service:
image: postgres:alpine
container_name: postgres-container
ports:
- "6432:5432"
volumes:
- ./postgres:/var/lib/postgresql/data
restart: always
environment:
POSTGRES_USER: root
POSTGRES_PASSWORD: secret
POSTGRES_DB: db_my_test_app
networks:
- my_test_app
...and the PHP service Dockerfile (from the aforementioned site):
FROM php:7.4-fpm
RUN apt-get update && apt-get install -y libpq-dev
RUN docker-php-ext-install pdo pdo_pgsql pgsql
RUN ln -s /usr/local/etc/php/php.ini-production /usr/local/etc/php/php.ini
RUN sed -i -e 's/;extension=pgsql/extension=pgsql/' /usr/local/etc/php/php.ini
RUN sed -i -e 's/;extension=pdo_pgsql/extension=pdo_pgsql/' /usr/local/etc/php/php.ini
You are configuring docker under the default network mode: bridge. In order for the php-fpm-service to be able to access the postgres-service you need to make the following modifications:
In the docker-compose.yml file you'll need to add depends_on to php-fpm-service
# php
php-fpm-service:
build:
context: .
dockerfile: ./php-fpm/Dockerfile
container_name: php-fpm-container
ports:
- "9000:9000"
working_dir: /var/www/project
volumes:
- ./app:/var/www/project
networks:
- my_test_app
depends_on:
- postgres-service
In your PHP config, you need modify localhost to postgres-service and using port 5432:
$dsn = 'pgsql:host=postgres-service;port=5432;dbname=db_my_test_app';

Docker, How to get a container IP inside another container in PHP?

I'm tring to make a docker with mysql and apache php.
I want to easly connect the mysql inside my php code without having to search the current ip of the mysql container.
How i could link the mysql ip adress in my php-apache container?
My docker-compose file:
version: "3.2"
services:
apache:
build:
context: './docker/apache/'
links:
- mysql:mysqldb
depends_on:
- mysql
ports:
- "80:80"
volumes:
- ./:/var/www/html/
- ./docker/apache/virtualhost.conf:/etc/apache2/sites-enabled/000-default.conf
- ./docker/apache/php.ini:/usr/local/etc/php/php.ini
container_name: apache
mysql:
image: mysql/mysql-server:8.0
command: ['--character-set-server=utf8mb4', '--collation-server=utf8mb4_unicode_ci','--default-authentication-plugin=mysql_native_password']
restart: always
ports:
- "18906:3306"
# volumes:
# - ./docker/mysql-dump:/docker-entrypoint-initdb.d
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: dbtest
MYSQL_USER: root
MYSQL_PASSWORD: root
MYSQL_ROOT_HOST: '%'
container_name: mysql
My php script:
$conn = mysqli_connect(
'172.10.0.2', // i want this to change acording to the mysql container
$user,
$pass,
$database
);
If you need something else please let me know.
Try this:
$conn = mysqli_connect(
'mysql', // here goes your mysql container name
$user,
$pass,
$database
);

Warning: mysqli::__construct(): (HY000/2002): Connection refused

They are newbie to docker.
Get phpmyadmin, php, home assistant working. But I can't configure the docker well to be able to connect from php to a database. Could you help me see what the problem is.
I tried everything. I read many posts with the same error but could not get it to work.
Thank you very much
This is my modified docker-compose
version: '3.4'
services:
web:
build:
context: ./php
dockerfile: Dockerfile
container_name: php73
depends_on:
- db
volumes:
- ./php:/var/www/html/
environment:
MYSQL_HOST: mysql8
MYSQL_USER: pf
MYSQL_PASSWWORD: 123456
MYSQL_DB: ha
ports:
- 3001:80
db:
container_name: mysql8
image: mysql:8.0
command: --default-authentication-plugin=mysql_native_password
restart: always
environment:
MYSQL_ROOT_PASSWORD: 123456
MYSQL_USER: pf
MYSQL_PASSWWORD: 123456
MYSQL_DATABASE: ha
volumes:
- /var/lib/mysql
ports:
- 6033:3306
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: phpmyadmin
depends_on:
- db
restart: always
ports:
- 8080:80
environment:
PMA_ARBITRARY: 1
PMA_HOST: db
PMA_PORT: 3306
MYSQL_USER: pf
MYSQL_PASSWWORD: 123456
MYSQL_ROOT_PASSWORD: 123456
volumes:
- /sessions
The new php example
<?php
$host = 'db';
$user = 'pf';
$password = '123456';
$db = 'ha';
$conn = new mysqli($host,$user,$password,$db,3306);
if($conn->connect_error) {
echo 'connection failed' . $conn->connect_error;
}
echo 'Sucessfully connected msql';
?>
The Dockerfile
FROM php:7.3.3-apache
RUN apt-get update && apt-get upgrade -y
RUN docker-php-ext-install mysqli
EXPOSE 80
My docker-compose
version: '3.3'
services:
web:
build:
context: ./php
dockerfile: Dockerfile
container_name: php73
depends_on:
- db
volumes:
- ./php:/var/www/html/
ports:
- 80:80
db:
container_name: mysql8
image: mysql:8.0
command: --default-authentication-plugin=mysql_native_password
restart: always
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: mediciones
MYSQL_USER: root
MYSQL_PASSWWORD: root
ports:
- 6033:3306
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: phpmyadmin
depends_on:
- db
restart: always
ports:
- 8080:80
environment:
PMA_ARBITRARY: 1
PMA_HOST: db
MYSQL_ROOT_PASSWORT: root
homeassistant:
container_name: homeassistant
restart: unless-stopped
image: homeassistant/home-assistant
devices:
- /dev/ttyUSB0:/dev/ttyUSB0
- /dev/ttyUSB1:/dev/ttyUSB1
- /dev/ttyACM0:/dev/ttyACM0
volumes:
- ${USERDIR}/docker/homeassistant:/config
- /etc/localtime:/etc/localtime:ro
- ${USERDIR}/docker/shared:/shared
- /dev/serial/by-id/:/dev/serial/by-id/
network_mode: host
privileged: true
My php example
<?php
$host = '127.0.0.1';
$user = 'root';
$password = 'root';
$db = 'ha';
$conn = new mysqli($host,$user,$password,$db);
if($conn->connect_error) {
echo 'connection failed' . $conn->connect_error;
}
echo 'Sucessfully connected msql';
?>
Assuming its your example code running in the php container:
In which case use db as the hostname and mediciones as the database name in the connection.
For the db container:
MYSQL_USER set to non-root as this will cause potential errors. This user is already given access on the MYSQL_DATABASE.
use a persistent volume for /var/lib/mysql
(optionally), if your home assistant or php doesn't create the tables, you can use [mysql "Initializing a fresh instance"]https://hub.docker.com/_/mysql) to initialize some tables.
You are exposing mysql on host port: 6033
Did you try:
$conn = new mysqli($host,$user,$password,$db,6033);

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