How to use RUN docker php ext install mysqli MySQL Command

Posted October 9, 2023
How to use RUN docker php ext install mysqli MySQL Command

mysqli serves as the gateway to establishing PHP database connections with MySQL. Mysqli complements pdo_mysql. On Docker, you need to use docker php ext install command to get mysqli and pdo_mysql if you use MySQL DB alongside PHP.

In this guide, I will show you the unraveling intricacies of using the docker php ext install mysqli (pdo_mysql) command to get these PHP extensions ready within Docker containers. You will learn how to use RUN docker-php-ext-install mysqli on Dockerfile to fine-tune your Docker PHP MySQL development workflow.

Step 1: Introducing RUN docker-php-ext-install mysqli Docker Command

When creating a Docker container, you must include every package your app requires. This will allow your apps to have the same functionality on Docker as on computer bare metals.

Docker uses docker-php-ext-install to allow you to add PHP extensions on Docker. This way, you will use Dockerfile and execute your RUN command so Docker gets your actions ready.

Step 2: Running RUN docker-php-ext-install mysqli With Dockerfile

Ensure you have a Dockerfile. It allows adding instructions on how to package your application.

Within this sample, you will execute docker-php-ext-install mysqli in conjunction pdo_mysql as follows:

# Add PHP-Apache base image
FROM php:8.0-apache
# Install your extensions to connect to MySQL and add mysqli
RUN docker-php-ext-install mysqli
# Install pdo is you need to use PHP PDO
RUN docker-php-ext-install pdo pdo_mysql

This package PHP with your extensions. Note that 8.0 is the PHP version you are running. In some older versions, you might be required to use docker-php-ext-install mysqli as follows:

# Add PHP-Apache base image
FROM php:7.0-apache

# Install your extensions
# To connect to MySQL, add mysqli
RUN docker-php-ext-install mysqli
# Install pdo is you need to use PHP PDO
RUN docker-php-ext-install pdo pdo_mysql
# Run docker-php-ext-enable command to activate mysqli
RUN docker-php-ext-enable mysqli

This example will require you to Run the docker-php-ext-enable command to activate mysqli extensions.

Step 3: Using Docker Compose to Package docker-php-ext-install mysqli Command

When your Docker file is ready, Docker Compose will itall your requisite extensions and launch the PHP container within Docker.

The docker-compose.yml file will encapsulate your configuration for both PHP and MySQL services in a way they can communicate together as follows.

version: "3.9"
services:
  # PHP Container
  php-container:
    container_name: php-container
    # Dockerfile path
    build:
      context: .
      dockerfile: mysql.dockerfile  
    volumes:
      # container working directory
      - ./test:/var/www/html  
    ports:
      # Expose PHP on 5000
      - 5000:80  
    depends_on:
      # Make sure the db-container starts before php-container
      - db-container  

  # Database Container
  db-container:
    image: mysql:latest  
    environment:
      # Add MySQL root password
      MYSQL_ROOT_PASSWORD: root_pass  
      # Username password
      MYSQL_PASSWORD: user_password  
      # Test Database
      MYSQL_DATABASE: sample_db  
      # Database username
      MYSQL_USER: sample_db_user  
    ports:
      - "3306:3306"  
  • depends_on in your PHP Docker service establish database connections via the php-container. It relies on the MySQL database (db-container) service. - Define environment variables to configure MySQL database access. You will need them to create PHP database connection script.

This example assumes your root directory has a subdirectory, test, that has your PHP files. Here is the basic structure:

php-ext/
│ ├── mysql.dockerfile
│ └── docker-compose.yml
└── test/
    └── index.php

Step 4: Connecting PHP and MySQL

Your test/index.php file will contain your database scripts. Here is an example that will allow you to check if the docker php ext install mysqli extension is working. Update the test/index.php file and include MySQL as follows:

<?php
$db_host = 'db-container';
$database_user = 'sample_db_user';
$user_passord = 'user_password';
$database_name = 'sample_db';

$conn = new mysqli($db_host, $database_user, $user_passord, $database_name);

if ($conn->connect_error) {
    die("PHP failed to access MySQL: " . $conn->connect_error);
}

echo "PHP successfully Connected to MySQL Service";

$conn->close();
?>

Key points to note include are:

  • db-container is your host and not localhost. localhost only works if MySQL runs on base metals. But because you are on Docker, your host will change to the name of the MySQL container, which in this case is db-container.
  • Docker will use the mysqli you previously installed in your Dockerfile (docker-php-ext-install mysqli) and communicate with MySQL. Therefore, the MySQL Docker environment variables such as MYSQL_USER: db_user and MYSQL_PASSWORD: password must reflect on the PHP script as yourdb_host, $database_user, $user_passord, and $database_name.

Step 5: Testing mysqli with MySQL on Docker

Execute the following docker-compose command to initiate your PHP container:

docker-compose up --build -d

How to use RUN docker php ext install mysqli Command with MySQL

Your containers should be running:

How to use RUN docker php ext install mysqli Command with MySQL

Proceed to http://localhost:5000/ to confirm if you have successful connection of mysqli with your database:

How to use RUN docker php ext install mysqli Command with MySQL

Step 6: Accessing PHP MySQL Database on Docker

Suppose you want to access your database. You need to use a GUI such as phpMyAdmin.

This comprehensive PHP Guide with Docker Compose, PhpMyAdmin and MySQL covers everything related to PHP, Docker, MySQL, and PhpMyAdmin

Related: learn docker php ext install pgsql postgres Command

Conclusion

Your docker php ext install mysqli command is working as expected. Now you can confidently use docker php ext command and install mysqli inside a Docker container to get PHP and MySQL working together on Docker.

How to use RUN docker php ext install mysqli MySQL Command

Written By:

Joseph Chege