Recently we talked to you about installing Docker on Debian 10 Buster. Docker is a technology to display container images that allows you to install applications without worrying about dependencies. In this opportunity, we will deploy a LAMP server using docker-compose.
Install Docker and Docker-compose
Before starting, it is necessary to install Docker and docker-compose. Both are in the Debian 10. However, if you want the latest stable version of Docker you can read this post we made.
To install them, run the following command:
:~$ sudo apt install docker docker-compose
Then, enter the user password and finally wait for the process to finish.
Deploy a LAMP server with docker-compose
What is docker-compose?
Docker-compose is a utility that allows you to deploy applications through the definition of a YAML file. In this file we will put all the configuration of the images, and then with raise with the following command:
:~$ sudo docker-compose up -d
The advantages are too obvious. For example, if we pass this file to another computer, and display it, we will get the same result as in our computer. Then, the portability is maximum.
On the other hand, if we have successfully deployed one or several images, just save the file to reuse it whenever we want.
A LAMP server using docker-compose. Apache y PHP.
First, create a new folder called test and inside it, create another folder called DocumentRoot. Then, create a new PHP file to test the image when is ready.
:~$ mkdir -p test/DocumentRoot :~$ cd test/ :~$ nano index.php <?php phpinfo(); ?>
Second, it is necessary to create the file with extension YML. Locate it wherever you want and use the text editor of your choice. In my case, I will use nano in the terminal.
:~$ nano docker-compose.yml
Now, we can start to work.
First, let’s define the image of PHP and Apache together. I will use for this case, PHP version 7.3.
version: '3' services: php-apache: image: mitlabs/apache-php7.3 ports: - 80:80 volumes: - ./DocumentRoot:/var/www/html links: - 'mariadb'
I now proceed to briefly explain this first part of the file.
Services: they are each and every one of the application services that are going to be defined in the file. In this case, the first service to raise is php-apache.
php-apache is the first service and it is necessary to define some options. You can replace the php-apache name with the one you want.
Image refers to the image Docker you will use. Ports refer to which port will be exposed from Docker. In Apache, the access port is 80. Therefore, the 80 is exposed to the host and in the guest.
In the volume section, it is advisable to mount the volume of the container on our host system. I explain myself if we did not do it this way, every time there are changes in the image, we would have to rebuild it and we would probably lose the data. So the DocumentRoot of the image will be mounted on /var/www/html of our system.
Then, for the php-apache service to become database dependent, specify it with the word links and define ‘mariadb’.
The MariaDB service
Now it is MariaDB’s turn to complete LAMP. To do this, add the following code.
mariadb: image: mariadb:10.3 volumes: - mariadb:/var/lib/mysql environment: TZ: "America/Chicago" MYSQL_ALLOW_EMPTY_PASSWORD: "no" MYSQL_ROOT_PASSWORD: "AngeloOsradar" MYSQL_USER: 'osradar' MYSQL_PASSWORD: 'osradarpss' MYSQL_DATABASE: 'testdb' ports: - 3306:3306
As with PHP and Apache. First, we define the image, in this case, the 10.3.
Again, in volumes, we define a directory of our host (/var/lib/mysql) to receive the data from the mariadb directory.
In the environment, we define the configuration of MariaDB. First, the Timezone, then the empty password option is denied and finally the users and their passwords. This part is somewhat explicit.
Finally, you have to define the ports of the application.
At the end, the file will look like this.
version: '3' services: php-apache: image: mitlabs/apache-php7.3 ports: - 80:80 volumes: - ./DocumentRoot:/var/www/html links: - 'mariadb' mariadb: image:mariadb:10.3 volumes: - mariadb:/var/lib/mysql environment: TZ: "America/Chicago" MYSQL_ALLOW_EMPTY_PASSWORD: "no" MYSQL_ROOT_PASSWORD: "AngeloOsradar" MYSQL_USER: 'osradar' MYSQL_PASSWORD: 'osradarpss' MYSQL_DATABASE: 'testdb' ports: - 3306:3306 volumes: mariadb:
Pay attention to the indexing of the file for it to be valid.
Next, run this command:
:~$ sudo docker-compose up -d :~$ sudo mv index.php DocumentRoot/
Next, open your browser and go to http://your-server/ you will see this.
And that is it.