Configure LAMP Stack in Docker Containers - CentLinux

Latest

Sunday, 1 March 2020

Configure LAMP Stack in Docker Containers

Configure LAMP Stack in Docker Containers

LAMP stack is a group of open source software components that are used to deploy PHP based applications. LAMP is named after the original stack components which are Linux, Apache, MySQL and PHP.

We are already learned how to install LAMP stack on Ubuntu Server 18.04 LTS. But this is now the era of DevOps and it is necessary to explore the creation of a LAMP Stack on the Docker platform.

Therefore, in this article, we will show you how to configure LAMP Stack in Docker containers.

If you are new to Docker platform, then you should read Docker in Action by Manning Publications.

 

This Article Provides:

     

    Environment Specification:

    We are using a Linux based Docker host with following specification.

    • CPU - 3.4 Ghz (2 cores)
    • Memory - 2 GB
    • Storage - 20 GB
    • Operating System - Ubuntu Server 18.04 LTS
    • Hostname – docker-01.centlinux.com
    • IP Address - 192.168.116.218 /24

    Although there are many pre-build single LAMP images are available on Docker Hub. But by following the best practice and to maintain the modularity of the software stack, we will run each service in its relevant container.

     

    Configure a PHP-Apache Docker Container:

    Connect with Docker host (docker-01.centlinux.com) as a privileged user by using a ssh tool.

    Create a directory to store files related to our LAMP Server.

    ahmer@docker-01:~$ mkdir ~/lamp-server ahmer@docker-01:~$ cd ~/lamp-server/

    Create a directory that will be mounted as Apache Document Root within the Apache container. This directory is used to deploy our PHP applications and the php-apache container will serve our web applications to clients.

    ahmer@docker-01:~/lamp-server$ mkdir ~/lamp-server/html

    Create a default PHP page in this directory. The PHP script contains a single phpinfo() function, that generates a PHP webpage with detailed information about Apache server and installed plugins.

    ahmer@docker-01:~/lamp-server$ echo "<?php phpinfo(); ?>" > ~/lamp-server/html/index.php

    We need to install some additional PHP plugins on the php-apache container. For this purpose, we have to create the build context file for our php-apache container.

    Create a directory to store php-apache build context.

    ahmer@docker-01:~/lamp-server$ mkdir php-apache

    Create build context file for php-apache container.

    ahmer@docker-01:~/lamp-server$ vi php-apache/Dockerfile

    Add following directives there in.

    FROM php:7.4-apache RUN docker-php-ext-install pdo pdo_mysql mysqli

    Since, we are deploying a multi-container Docker application, therefore, we need to use docker-compose to create and execute our containers.

    For this purpose, we need to create docker-compose.yml file that contains the directives to create our containers.

    ahmer@docker-01:~/lamp-server$ vi docker-compose.yml

    And add following directives to create php-apache container.

    version: '3' services: php-apache: build: context: ./php-apache ports: - 80:80 volumes: - ./html:/var/www/html links: - 'mariadb'

     

    Configure a MariaDB Docker Container:

    Create a directory for Dockerfile that will be used to create mariadb container.

    ahmer@docker-01:~/lamp-server$ mkdir -p mariadb/sql

    Create a SQL script to prepare our sample table.

    ahmer@docker-01:~/lamp-server$ vi mariadb/sql/init-db.sql

    Add following SQL statements in this file.

    USE testdb; CREATE TABLE test ( name varchar(30), email varchar(50) ); INSERT INTO test (name, email) VALUES ('Ahmer', 'ahmer@centlinux.com'), ('Mansoor','mansoor@centlinux.com'), ('Salman','salman@centlinux.com');

    Create a Dockerfile to customize our mariadb container.

    ahmer@docker-01:~/lamp-server$ vi mariadb/Dockerfile

    Add the following build context there in.

    FROM mariadb:10.5 ENV MYSQL_ROOT_PASSWORD 123 ENV MYSQL_USER ahmer ENV MYSQL_PASSWORD 123 ENV MYSQL_DATABASE testdb COPY ./sql /docker-entrypoint-initdb.d/

    Create a directory on Docker Host to store MariaDB Server data files. This directory will be mounted within the mariadb container on container startup.

    ahmer@docker-01:~/lamp-server$ mkdir mariadb_data

    Now, configure a MariaDB docker container in the same docker-compose.yml file.

    ahmer@docker-01:~/lamp-server$ vi docker-compose.yml

    Append following lines of code under the “services” section in this file.

    mariadb: build: context: ./mariadb volumes: - ./mariadb_data:/var/lib/mysql

     

    Running LAMP Stack in Docker Container:

    Start our LAMP stack environment by using docker-compose command.

    ahmer@docker-01:~/lamp-server$ sudo docker-compose up ... Starting lamp-server_mariadb_1 ... done Starting lamp-server_php-apache_1 ... done Attaching to lamp-server_mariadb_1, lamp-server_php-apache_1 mariadb_1 | 2020-02-28 17:46:22+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 1:10.5.1+maria~bionic started. mariadb_1 | 2020-02-28 17:46:22+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql' mariadb_1 | 2020-02-28 17:46:22+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 1:10.5.1+maria~bionic started. mariadb_1 | 2020-02-28 17:46:23 0 [Note] mysqld (mysqld 10.5.1-MariaDB-1:10.5.1+maria~bionic) starting as process 1 ... mariadb_1 | 2020-02-28 17:46:23 0 [Note] InnoDB: Using Linux native AIO mariadb_1 | 2020-02-28 17:46:23 0 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins mariadb_1 | 2020-02-28 17:46:23 0 [Note] InnoDB: Uses event mutexes mariadb_1 | 2020-02-28 17:46:23 0 [Note] InnoDB: Compressed tables use zlib 1.2.11 mariadb_1 | 2020-02-28 17:46:23 0 [Note] InnoDB: Number of pools: 1 mariadb_1 | 2020-02-28 17:46:23 0 [Note] InnoDB: Using SSE2 crc32 instructions mariadb_1 | 2020-02-28 17:46:23 0 [Note] mysqld: O_TMPFILE is not supported on /tmp (disabling future attempts) mariadb_1 | 2020-02-28 17:46:23 0 [Note] InnoDB: Initializing buffer pool, total size = 268435456, chunk size = 134217728 mariadb_1 | 2020-02-28 17:46:23 0 [Note] InnoDB: Completed initialization of buffer pool mariadb_1 | 2020-02-28 17:46:23 0 [Note] InnoDB: If the mysqld execution user is authorized, page cleaner thread priority can be changed. See the man page of setpriority(). php-apache_1 | AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.19.0.3. Set the 'ServerName' directive globally to suppress this message php-apache_1 | AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.19.0.3. Set the 'ServerName' directive globally to suppress this message php-apache_1 | [Fri Feb 28 17:46:23.929980 2020] [mpm_prefork:notice] [pid 1] AH00163: Apache/2.4.38 (Debian) PHP/7.4.3 configured -- resuming normal operations php-apache_1 | [Fri Feb 28 17:46:23.930060 2020] [core:notice] [pid 1] AH00094: Command line: 'apache2 -D FOREGROUND' mariadb_1 | 2020-02-28 17:46:24 0 [Note] InnoDB: 128 rollback segments are active. mariadb_1 | 2020-02-28 17:46:24 0 [Note] InnoDB: Creating shared tablespace for temporary tables mariadb_1 | 2020-02-28 17:46:24 0 [Note] InnoDB: Setting file './ibtmp1' size to 12 MB. Physically writing the file full; Please wait ... mariadb_1 | 2020-02-28 17:46:24 0 [Note] InnoDB: File './ibtmp1' size is now 12 MB. mariadb_1 | 2020-02-28 17:46:24 0 [Note] InnoDB: 10.5.1 started; log sequence number 60160; transaction id 21 mariadb_1 | 2020-02-28 17:46:24 0 [Note] Plugin 'FEEDBACK' is disabled. mariadb_1 | 2020-02-28 17:46:24 0 [Note] InnoDB: Loading buffer pool(s) from /var/lib/mysql/ib_buffer_pool mariadb_1 | 2020-02-28 17:46:24 0 [Note] Server socket created on IP: '::'. mariadb_1 | 2020-02-28 17:46:24 0 [Warning] 'proxies_priv' entry '@% root@b4fcd5a0a00b' ignored in --skip-name-resolve mode. mariadb_1 | 2020-02-28 17:46:24 0 [Note] Reading of all Master_info entries succeeded mariadb_1 | 2020-02-28 17:46:24 0 [Note] Added new Master_info '' to hash table mariadb_1 | 2020-02-28 17:46:24 0 [Note] mysqld: ready for connections. mariadb_1 | Version: '10.5.1-MariaDB-1:10.5.1+maria~bionic' socket: '/var/run/mysqld/mysqld.sock' port: 3306 mariadb.org binary distribution mariadb_1 | 2020-02-28 17:46:24 0 [Note] InnoDB: Buffer pool(s) load completed at 200228 17:46:24

    Open URL http://docker-01.centlinux.com in a browser.

    01-lamp-stack-docker-phpinfo

    Our LAMP Stack has been serving the default PHP page that we have created above.

     

    Connect PHP Application with MariaDB Database:

    To demonstrate the connectivity between php-apache and mariadb containers, we are creating a PHP webpage that creates connection to MariaDB database to fetch and display the data from a database table.

    For this purpose, we are replacing the existing index.php with a new webpage that contains the PHP code to accomplish the required task..

    ahmer@docker-01:~/lamp-server$ mv html/index.php html/phpinfo.php ahmer@docker-01:~/lamp-server$ vi html/index.php

    Add following HTML code in this file.

    <html> <head> <title>Fetching Data from MariaDB Server</title> </head> <body> <style> td,th { border: solid black 1px; font-size: 30px; width: 200px; } </style> <table> <tr> <th>Name</th> <th>Email</th> </tr> <?php $dbhost = "mariadb"; $dbuser = "ahmer"; $dbpass = "123"; $db = "testdb"; $dbconn = mysqli_connect($dbhost, $dbuser, $dbpass, $db); if(! $dbconn ) { die('Could not connect: ' . mysql_error()); } $query = mysqli_query($dbconn, "SELECT * FROM test") or die (mysqli_error($dbconn)); while ($row = mysqli_fetch_array($query)) { echo "<tr> <td>{$row['name']}</td> <td>{$row['email']}</td> </tr>"; } mysqli_close($dbconn); ?> </body> </html>

    Open URL http://docker-01.centlinux.com in a browser.

    02-lamp-stack-docker-mariadb-connection

    You can see that, our web page is now displaying data from the test table.

    We have successfully configured LAMP Stack in Docker containers.

    2 comments:

    1. doesnt work..
      when i use sudo docker-compose up command i get following error, what should i do now?
      Thank


      [murat@centos8 docker-lampserver]$ sudo docker-compose up
      ERROR: yaml.parser.ParserError: while parsing a block mapping
      in "./docker-compose.yml", line 1, column 1
      expected , but found ''
      in "./docker-compose.yml", line 14, column 2
      [murat@centos8 docker-lampserver]$

      ReplyDelete
      Replies
      1. It looks like you have a typo error in docker-compose.yml file at line 14 column 2. Please rectify it and continue.

        Delete