Php connect to mongodb docker
We can containerize our applications using Docker to have a separate installation of the required packages with the application-specific versions independent of the underlying operating system. We can use Docker Containers to make our application portable so that we can simply move it to another system having docker. This tutorial provides all the steps to containerize a PHP application using Apache Web Server as the webserver and either MySQL or MongoDB as the database server. Show PrerequisitesWindows - How To Install WSL 2 (Windows Subsystem for Linux) with Ubuntu On Windows 10 and How To Install Docker Desktop On Windows 10. Optionally you may follow How To Change Docker Data Path On Windows 10. Ubuntu - How To Install Docker Engine on Ubuntu 20.04 LTS macOS - How To Install Docker Desktop On Mac Install PHP and Apache Web ServerCreate a directory to store your project-specific files. I have created the directory helloworld to store all the project files. Now create the file docker-compose.yml at the root of your project directory as shown below. # docker-compose.yml Now create the directories - docker and src within the project root directory. Also, create two directories within the docker directory i.e. php and apache. Create the Dockerfile within the PHP directory as shown below. # docker/php/Dockerfile Create the Dockerfile within the Apache directory as shown below. # docker/apache/Dockerfile Create the index.php file within the src directory as shown below. # src/index.php We also need to configure the Virtual Host to pass the PHP requests to PHP-FPM vial port 9000. Now, create the default configuration file as shown below. # docker/apache/helloworld.apache.conf Now, update the Dockerfile within the Apache directory as shown below. # docker/apache/Dockerfile After creating all the directories and files, the directory structure should be similar as shown below. helloworld Now, run the command docker-compose build to build the images for PHP and Apache Web Server. # Build After completing the build, we can run the application using the command shown below. # Launch Application Now open the browser and enter the URL - http://localhost/index.php. It should show the output of index.php as shown in Fig.1. Fig 1 Press Ctrl + C to stop the containers. Install MySQL and phpMyAdminIn this step, we will continue with our previous step and install MySQL and phpMyAdmin. We will also access the MySQL database from the PHP code using PDO. Now, update the docker-compose.yml as shown below. # docker-compose.yml Now, run the command docker-compose build to build the application. # Build We also need to run the command docker-compose up to launch the application as shown below. It will pull the MySQL and phpMyAdmin images and take time for the first time. The subsequent launches will be faster. # Launch Application Now, try to access phpMyAdmin from the Browser using the URL http://localhost:8085. It should show the phpMyAdmin home page as shown in Fig.2. Fig 2 Now, login to phpMyAdmin using the username as root and the root password configured in the docker-compose.yml. Also, leave the server blank. It should show the phpMyAdmin home page as shown in Fig 3. Fig 3 Press Ctrl + C to stop the containers. # Press Ctrl + C Now, install the PHP extensions to access MySQL from the PHP source files by updating the Dockerfile as shown below. # docker/php/Dockerfile Also, run the build and up commands to again build the application and launch it. We can test MySQL connectivity using PHP as shown below. You can also follow How To Connect MySQL Database With PHP. # src/mysql.php If we open the PHP file using the URL http://localhost/mysql.php, it should show the output as shown in Fig 4. Fig 4 This completes the installation and configuration of MySQL and phpMyAdmin. Press Ctrl + C to stop the containers. Install MongoDB and MongoDB ExpressIn this step, we will continue with our previous step and install MongoDB and MongoDB Express. Now, update the docker-compose.yml as shown below. # docker-compose.yml Replace the root password while configuring the docker-compose.yml. Also, install the PHP extensions to access MongoDB from the PHP source files by updating the Dockerfile as shown below. # docker/php/Dockerfile Now, run the build and up commands to again build the application and launch it. We can access MongoDB using the URL - http://localhost:8081. It will ask for the basic authentication configured by us. The home page should be similar as shown below. Fig 5 This completes the installation and configuration of MongoDB for PHP. SummaryThis tutorial provided all the steps to containerize PHP with Apache Web Server, MySQL, phpMyAdmin, MongoDB, and MongoDB Express using Docker containers. |