Remove Machine Dependency Development using Docker

How to Remove Machine Dependency for LAMP Development using Docker?

How to remove machine dependency for LAMP development using Docker
With Docker, any developer can remove the system dependency problem and automate the development to the deployment.

Being a developer, sooner or later you have to undergo such machine dependency problems while deployment of code on the server. There are scenarios where you have to work with a team of developers and maybe they use different versions of PHP and MySQL, or if you are working for LAMP environment then WAMP, XAMPP, LAMPP, MAMP installation can be a big pain for developers.

Now, let’s assume you are done with all installation but what will happen if the server environment does not match with your local development environment? I know this is the most undesirable situation for any developer. But, do you know you can get rid of all these hitches and save your development time? There are a number of solutions available in the market. But, here I want to share my personal experience with Docker.

My experience with Docker has been quite good, I have been using Docker for 3 years now. Docker, which initially released 5 years back has filled the gap and offered a consistent development environment to the developers. Before using docker, as a full stack developer, while working on LAMP environment, I always had to switch my PHP, Apache, MySQL settings and versions, when I switched to multiple projects. It was a big pain to repeat the set every time and it ultimately affected my productivity. But, Docker has made things easier and faster.

Let me explain you with an example:

If I want to set up my development environment for PHP 7.2, Mysql mysql:5.7.22 and Nginx server.

I have multiple solutions to setup this environment:

  • First, I can install XAMPP/WAMP/LAMPP, but this environment does not contain Nginx service.
  • Second, I can install everything separately in the local machine, but there are many problems with this process too.
Problem 1.

If your development machine is Windows and deployment server is Ubuntu or vice-versa.

Problem 2.

Development software compatibility installation is a critical time consuming process for the developer.

Problem 3.

Even if you solve both of these problems, you will have to change settings according to your project’s requirements and it will take a long time to execute those changes.

That’s where Docker comes to the rescue, it not only simplifies all these problems but simply removes all machine dependency problems.

How to install Docker?

Using Docker does not require any professional courses or training. If you have a basic understanding of development environment setup, you can easily do it . You just have to install Docker in your machine or on the server and simply let it manage everything right from the development to the deployment. With Docker, you can create any number of virtual machines and keep those into a single container.

You can find a complete Docker installation guide here with this link https://docs.docker.com/install/ from where you can select your OS and install. Docker uses the concept of container and images. Now let’s understand what is Docker’s container and images.

Docker Images, Layers and Containers

Docker images are basically the files with the set of instructions, consisting of multiple layers that can be used to incorporate tools, system library and other files required for executable code. When we run an image, it becomes one or multiple instances for the container. The Docker image includes all dependencies of the executable code so that the containers can run independently from one environment to another in a similar OS without any changes. You can have multiple containers for a single image.

  • If you want to see the list of all running Docker containers you have to execute the command
    docker ps-a
  • If you want to see all running images you have to execute the command
    docker images –a

You must have basic knowledge of command execution as Windows operating system uses power shell to execute the commands and Ubuntu and Mac OS use the terminal to execute the commands.

How to set up XAMPP machine using Docker?

Before setting up XAMPP machine, make sure you already have Docker installed in your machine.

Now first create a directory anywhere in your system.

Directory structure (I have created this directory according to my requirements, you can create according to your requirement):

  • mysql (directory)
  • nginx (directory)
  • php (directory)
  • docker-compose.yml file)
  • Dockerfile (file and it does not contain file extension)

What is docker-compose.yml ?

Compose is a tool for defining and running multiple-container Docker applications. With Compose, you can use a YML file to configure your application’s services. Define the services that make up your app in docker-compose.yml, so they can be run together in an isolated environment.

What is Dockerfile?

A Dockerfile is a text document that contains all the commands a user can call on the command line to assemble an image.

Using docker build, users can create an automated build that executes several command-line instructions in succession. Following are the commands that you can use in a Dockerfile.

  • Mysql directory: Put your customized MySQL conf file if you have customized anything.
  • nginx directory: Put your customized nginx conf file if you have customized anything.
  • PHP directory: Put your custom php.ini file if you have customized anything.
Note:

Make sure the path of above-mentioned files are currently written in docker-compose.yml

You can navigate here https://github.com/osingh152/docker-with-php-mysql-nginx and find the sample files for docker-compose.yml and Dockerfile

How to PHP application using Docker in Laravel?

Let’s explore how we can use Docker container with php 7.2. , MySql 5.* and NGINX server. Change nginx file for laravel public folder. The path for Laravel: root /var/www/public;

Other: root /var/www;

/var/www -> This is a Path of the working directory

Before moving to the next step please make sure you have installed Docker in your machine

Command to check Docker Version-docker –v

Please follow the below mentioned steps and commands to use docker

  • First, you have to clone the project in your specific drive or directory
  • Now Install docker and docker compose in your machine
  • Open power shell
  • Navigate to your project directory, refer step 1.
  • docker-compose up -d
Essential Docker Commands

1. Start all required containers:docker-compose up -d

-d: Run the container in the background

2. Stop all container:docker-compose down

3. Run commands in a specific container:docker-compose exec app nano .env

docker-compose exec – This is a command to execute commands

app – This the name of the container in which we want to run the command

4. Accessing MySql:docker-compose exec db bash

After entering db bash hit mysql -u root -p for MySQL root password and default database refer docker-compose.yml file

Hope this article will help you to have the basic understanding and usability of the Docker and provide you with a solution for machine dependency problem while development to save your time and increase your productivity.





Related post
Magento 2.3

Magento 2.3 Features – Made Things Smarter for E-Commerce

Latest update from Magento i.e. Magento 2.3 brought amazing features that made things smarter for e-commerce platforms with its advanced features. This Magento 2.3 Version has been released on 26th March 2019. Since then users appreciating its latest features help them to get better user experience.

How to install Zabbix on centos

ZABBIX Installation and Configuration on CentOS

Zabbix is a highly integrated network monitoring solution. It is open source and offers multiple features in one single package. You need not install any separate software on the monitored host. You can confirm the use of services like SMTP and HTTP using some normal checks.