Docker for PHP Developers

Docker for PHP Developers

In this article, I will mention about how to get started PHP project with Docker Container

Technology.

Pre-Requisites:

1) Any Linux Operating System ( In my case I used Rhel-7), Windows also supported Docker.

2) Docker Installed.

3) DockerHub Account (Optional).

Creating Dockerfile for respective containers.

So for the PHP project we need one frontend and one database. In my case, I used MySQL as a database and Apache as the frontend for creating images and running respective containers.

  • First of all, I started by creating a MySQL image with Dockerfile.
FROM mysql:5.7
ENV MYSQL_ROOT_PASSWORD redhat
ENV MYSQL_DATABASE mydb
ENV MYSQL_USER raghav
ENV MYSQL_PASSWORD redhat
  • Build that Dockerfile by:
docker build -t php-db .
  • Second one Creating Dockerfile for frontend
FROM php:7.3-apache
RUN apt-get update && \
    apt-get install nano -y && \
    apte-get install iputils-ping -y 
RUN docker-php-ext-install pdo pdo_mysql mysqli
COPY website/ /var/www/html
EXPOSE 80
  • Build that Dockerfile by:
docker build -t php-web .

After creating docker images we need to run the containers from respective images.

  • Running MySQL container from php-db image.
docker run -itd -p 3306:3306 --name php-db php-db
  • Running PHP-apache container from php-web image.
docker run -itd -p 80:80 --name php-web php-web

Here I am using the docker network concept to pinging one docker container from another container by name.

First of all, It is very important to explicitly specify a name with --name for your containers otherwise I've noticed that it would not work with the random names that docker assigns to your containers.

  • Then create a new network
docker network create mynetwork
  • After that connect your containers to the network
docker network connect mynetwork php-db
docker network connect mynetwork php-web

  • Test the connection
No alt text provided for this image

Again it is quite important to explicitly names for your containers otherwise it would not be worked.

Attaching simple PHP code for testing of connectivity of mysql container with PHP.

<?php
$conn = new mysqli("php-db", "root", "redhat", "mydb");
// Check connection
if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
}


$sql = "SELECT name FROM users";
$result = $conn->query($sql);


if ($result->num_rows > 0) {
        // output data of each row
        while($row = $result->fetch_assoc()) {
                echo $row['name']."<br>";
        }
} else {
        echo "0 results";
}
$conn->close();


Here php-db is my container name and root is my user, redhat is password and mydb is my database name.

Created users table my in mydb database for accessing the content of the table users.

  • docker pull command for pulling out my images for docker hub
docker pull ragh19/phpproject:web_v1

docker pull ragh19/phpproject:mysql_v1

  

  • Future Implenatations:
Getting started with PHP project in Kubernetes and Implement ci/cd for same.


Really appreciate... Nothing better than sharing knowledge. Kudos guys

回复
Rounak Surana

?? SRE/DevOps Evangelist | Driving Cloud Infrastructure Excellence | Kubernetes & Terraform Expert

4 年

We’ve tried this approach over kubectl. Good one????

要查看或添加评论,请登录

Raghav Agarwal的更多文章

社区洞察

其他会员也浏览了