Dockerizing Apache, MySql and PHP using docker-compose
Hey Friends! In this blog post we try to dockerize common web application technologies (PHP, Apache & MYSQL) using docker compose
PHP, Apache, and MySQL have a very large market share on content management systems and web applications on the internet, and with so many developers using these technologies, there is a lot of interest to modernise the way that they use them from from local development all the way to production. Today we’ll take a look at several ways to containerize and link PHP, Apache, and MySQL together while demonstrating some tips, tricks, and best-practices that will help you take a modern approach when developing and deploying your PHP applications! We are also using a dummy database in mysql and try to display that.
Why containerisation?
Docker enables developers to easily pack, ship, and run any application as a lightweight, portable, self-sufficient container, which can run virtually anywhere.
Containers do this by enabling developers to isolate code into a single container. This makes it easier to modify and update the program. It also lends itself, as Docker points out, for enterprises to break up big development projects among multiple smaller, Agile teams using Jenkins, an open-source CI/CD program, to automate the delivery of new software in containers.
Containers do this by enabling developers to isolate code into a single container. This makes it easier to modify and update the program. It also lends itself, as Docker points out, for enterprises to break up big development projects among multiple smaller, Agile teams using Jenkins, an open-source CI/CD program, to automate the delivery of new software in containers.
Getting Started
- First install docker-compose. I am using an ubuntu system
- Download the Docker Compose binary into the
/usr/local/bin
directory with the following curl command: - sudo curl -L "https://github.com/docker/compose/releases/download/1.23.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
- Once the download is complete, apply executable permissions to the Compose binary: sudo chmod +x /usr/local/bin/docker-compose
- Your folder structure should be like this:
├── apache │ ├── Dockerfile │ └── demo.apache.conf ├── docker-compose.yml ├── php │ └── Dockerfile └── myphp └── index.php
- Now lets get started with the real thing. First create an index.php file that checks connection with your mysql database. The code basically connects your mysql database using the mysqli interface fron PHP. If successful, it prints a success. If not, it prints a failed message. You can get this file easily from w3schools.com. Save this file under myphp folder.
- Now we come to the docker compose part. We will be using 3.2 version of docker. Create a docker compose file namely docker-compose.yml .It allows you to define all your dependencies for services, volumes, networks, etc as code.
docker-compose.yml
version: "3.2" services: php: build: './php/' networks: - mysqlnetwork volumes: - ./myphp/:/var/www/html/ apache: build: './apache/' depends_on: - php - mysql networks: - frontendnetwork - mysqlnetwork ports: - "8080:80" volumes: - ./public_html/:/var/www/html/ mysql: image: mysql:5.7.24 networks: - mysqlnetwork environment: - MYSQL_ROOT_PASSWORD=password networks: frontendnetwork: mysqlnetwork:
In services, we have defined php, apache and mysql. 'build' is used to run docker file of that particular service so we have to provide the path. 'networks' are defined at the end of the docker compose file and used to connect the services in the same network. For frontend we used frontendnetwork and for backend we used mysqlnetwork. 'volumes' are used here to opy the php file which we have to run into the container by providing the path /var/www/html where the project is actually located in apache. 'ports' are defined here. For apache i used 8080:(default port for apache i.e, 80). The image of mysql i used here is 5.6.40 version. For apache and php, i have defined that in their Dockerfiles. Now lets take a look on that:
apache/Dockerfile
FROM httpd:2.4.33-alpine RUN apk update; \ apk upgrade; # Copy apache vhost file to proxy php requests to php-fpm container COPY demo.apache.conf /usr/local/apache2/conf/demo.apache.conf RUN echo "Include /usr/local/apache2/conf/demo.apache.conf" \ >> /usr/local/apache2/conf/httpd.conf
php/Dockerfile
FROM php:7.2.7-fpm-alpine3.7 RUN apk update; \ apk upgrade; RUN docker-php-ext-install mysqli
Running docker-compose.yml
- To run the docker-compose file, go into the directory and run docker-compose build. It will look into your docker compose file, look all the services containing the build stmt. and run a docker build for each one.
- Now run docker-compose up or docker-compose up -d to run in daemon/foreground mode.
- Now you can check whether your containers are up or not by using docker ps -a This command will list all your containers that are running or created.
- Now is the final step to check whether the file is running or not. Just go to your browser and hit on 8080 port. It will run your index.php file with "connection successful" status or whatever echo text that you have provided.
by - Taranjot Singh Sarna
mail- taranjot.techycardia@gmail.com
Comments
Post a Comment