Docker- Definition
Docker provides the ability to package and run an application in a loosely isolated environment called a container. The isolation and security allows you to run many containers simultaneously on a given host. Containers are lightweight and contain everything needed to run the application, so you do not need to rely on what is currently installed on the host. You can easily share containers while you work, and be sure that everyone you share with gets the same container that works in the same way.
Docker Advantages
You will be having projects developed in different PHP versions. Sometimes some of applications you dint want to upgrade but still you need to run on your local. What you will do? suppose you have projects in php version 5.3. 5.6 and 7.3, 8 , running this simultaneously in your local server or machine wont be possible as the wamp or xamp wont support it. So if you are running latest version of Wamp and you try to run projects in 5.6, it wont work. This is one of the many use cases of a Docker. You can google it for other advantages.
In this article , I will be giving instructions on how to build a container in PHP 5.3 and Mysql.
First Step is to Install Docker in your computer
Folder Structure
In Our Main Folder , first we have to create a file called docker-compose.yml
in this file we have to define the commands. Commands in the sense, the Docker image to use for PHP and MySQL. means all the configurations which is needed for the site to work. By adjusting this file, we create containers for each versions. So For latest PHP version copy this code into the file.
version: '3.8'
services:
php-apache-environment:
container_name: php-apache5
build:
context: ./php
dockerfile: Dockerfile
depends_on:
- db5
volumes:
- ./php/src:/var/www/html/
ports:
- 8005:80
db5:
container_name: db5
image: mariadb:10.4
restart: always
environment:
MYSQL_ROOT_PASSWORD: MYSQL_ROOT_PASSWORD
MYSQL_DATABASE: MYSQL_DATABASE
MYSQL_USER: MYSQL_USER
MYSQL_PASSWORD: MYSQL_PASSWORD
ports:
- "9905:3306"
phpmyadmin:
image: phpmyadmin/phpmyadmin
ports:
- '8085:80'
restart: always
environment:
PMA_HOST: db5
UPLOAD_LIMIT: 300M
depends_on:
- db5
Here you can see its mentioning the php latest Image.then in the context defining the folder to search for file Docker file. Then you can see it maps the port 8005 for 80. This is because normally the apache opens port 80 for web service. so here it points to 8005. so localhost:8005 will be assigned to this project. Next is it defines the mysql image and naming as a container. Also its providing the username and password for mysql to create. Here also you can see the port 3306 is mapped to 9905. For accessing phpmyadmin, it maps port 80 to 8085. Then in environment we can set the resource limits. Here am setting the upload limit to 200 MB.
Now next step is to create the file Dockerfile inside the php folder as defined in the context attribute.
In the Dockerfile Put contents like this:-
FROM php:7.3-apache
RUN docker-php-ext-install mysqli && docker-php-ext-enable mysqli
RUN apt-get update && apt-get upgrade -y
RUN a2enmod rewrite
Here You can see its asking for the php:7.3 version image. Then enabling the mysqli extension. Iam also enabling the rewrite access so that .htaccess will be working for our project.
Now next step is to put our working files inside the php/src/ folder , so that the scripts starts executing. This path is defined in docker-composer.yml. Its mapping the var/www/html to our specified directory.
Now for testing purpose we can put php info printing code inside the index.php file. So we can check which version of PHP installed in the project.
So our Project scripting is over. lets see how this will work..
Now You have to open the command prompt or terminal and navigate to the project folder.
type docker-compose up command
If its first time, it will create the container for you. It will take some seconds to install all the things.
If you want to rebuild the entire container if you changed some version numbers in the config files.
Then you can use the command docker-compose up --build . This will rebuild the container and will
start working. Now if you access the url http://localhost:8004 as mentioned in the docker-compose.yml
file. you can access the site.
So accessing http://localhost:8005, you will get:-
You can see the Php version is 8.0. Here you can put your project files and run using the url.
Now to access PHPmyadmin, go to url: http://localhost:8085
You can login using username:root, password: MYSQL_ROOT_PASSWORD. After login you can see the phpmyadmin dashboard just like default.
You can create database of your wish and import it. Connect it and it will start working. In MYSQL connect settings, you can create your own user and give permission to access the database if you want.
You can create a new db user by following this:-
Scroll down list of users, you can see link to create new user
In top of page, give username and a strong password.
Set all the permission. and create user.
Now when you are defining the connection settings to the site in the host name filed you have to put the db instance name which we defined in docker-composer.yml file and same you can see at hostname of phpmyadmin also.
Now your environment will be ready. You can see your container running in docker dashboard.
Now try this and of you have any queries comment below.
You can access Github code here:- https://github.com/litto/docker-php-73-mariadb