How to deploy a LAMP server using Docker-Compose?

Windows Articles

Manages SSH connections from Windows 10 with PuTTY.

Hello! There are many ways to connect remotely to a computer. Indeed, one of the most common is through remote control software. For example,...

Top 8 features for power users in Windows 10

Hello! Windows 10 is full of features and functions. In fact, that's one of the reasons it's so popular and used. Because it can...

How to access System in Windows 10

Perhaps the title of this post is misleading. In previous versions of Windows, it was enough to enter the Control Panel and from there,...

Microsoft reduces the fragmentation of Windows 10

Windows 10 20H2 has been released as a minor operating system update. As a result, it has managed to reduce platform errors. In addition,...

How to install Nginx and PHP on FreeBSD?

Hello, friends. FreeBSD is a secure system even more than Linux, that's why it's used as the main server. And a server has to...
angeloma
angeloma
Senior Writer and partner

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:

1.- The YML file
1.- The YML file

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/

2.- Deploy a LAMP server using Docker compose
2.- Deploy a LAMP server using Docker compose

Next, open your browser and go to http://your-server/ you will see this.

3.- LAMP is running
3.- LAMP is running

And that is it.

More articles

2 COMMENTS

  1. Hi, great tutorial. There is a colon missing after mariadb volume which results in the error being thrown:

    ERROR: In file ‘./docker-compose.yml’, volume must be a mapping, not a string.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest article

Manages SSH connections from Windows 10 with PuTTY.

Hello! There are many ways to connect remotely to a computer. Indeed, one of the most common is through remote control software. For example,...

Top 8 features for power users in Windows 10

Hello! Windows 10 is full of features and functions. In fact, that's one of the reasons it's so popular and used. Because it can...

How to access System in Windows 10

Perhaps the title of this post is misleading. In previous versions of Windows, it was enough to enter the Control Panel and from there,...

Microsoft reduces the fragmentation of Windows 10

Windows 10 20H2 has been released as a minor operating system update. As a result, it has managed to reduce platform errors. In addition,...

How to install Nginx and PHP on FreeBSD?

Hello, friends. FreeBSD is a secure system even more than Linux, that's why it's used as the main server. And a server has to...
x