How to setup Lamp Stack by Docker Compose on Debian 11.3

To Setup Lamp Stack By Docker Compose on Debian 11.3

Introduction:

The Docker Compose software was created to help define and share multi-container applications. Furthermore, we can use YAML files to define services, and with just a single command, we can start or stop everything.

Installation Procedure:

Step 1: Check the OS version by using the below Command

root@linuxhelp: ~# lsb_release -a
No LSB modules are available.
Distributor ID:	Debian
Description:	Debian GNU/Linux 11 (bullseye)
Release:	11
Codename:	bullseye

Step 2: Check the Docker Version by using the below command

root@linuxhelp: ~# docker -v 
Docker version 20.10.17, build 100c701

Step 3: Install the Docker Compose Packages by using the below command

root@linuxhelp: ~# apt-get install docker docker-compose

Step 4: Create a directory as docker by using the below command

root@linuxhelp: ~# mkdir docker 

Step 5: Change to the docker directory by using the below command

root@linuxhelp: ~# cd docker/

Step 6: Create a Docker Compose file by using the below command

root@linuxhelp: ~/docker# nano docker-compose.yml 

version: "3"
services:
  web:
    image: "httpd"
    restart: 'always'
    depends_on:
      - mariadb
    restart: 'always'
    ports:
      - '8080:80'
    links:
      - mariadb
  mariadb:
    image: "mariadb"
    restart: 'always'
    environment:
      MYSQL_ROOT_PASSWORD: linuxc
MYSQL_DATABASE: lamp
      MYSQL_USER: luser
      MYSQL_PASSWORD: linuxc

  php:
    image: "php"
    restart: 'always'

Step 7: Execute the Docker Compose file by using the below command

root@linuxhelp: ~/docker# docker-compose up 
Creating network "docker_default" with the default driver
Pulling mariadb (mariadb:)...
latest: Pulling from library/mariadb
08c01a0ec47e: Pull complete
a2bcb14c13a1: Pull complete
29c56760f879: Pull complete
a95000a218fc: Pull complete
a765d76e68d9: Pull complete
c6945738f085: Pull complete
62787b7c58c5: Pull complete
d2987a30cfe4: Pull complete
cbc65983d8b5: Pull complete
db216f91595e: Pull complete
Digest: sha256:ca31f38b6e325ece985d857db7eba1fe59928b4fd83ff8a55cb912c9684b9e43
Status: Downloaded newer image for mariadb: latest
Pulling web (httpd:)...
latest: Pulling from library/httpd
5eb5b503b376: Pull complete
a43a76ccc967: Pull complete
942bd346e7f7: Pull complete
cdb155854ae6: Pull complete
10c4d45228bf: Pull complete
Digest: sha256:5cc947a200524a822883dc6ce6456d852d7c5629ab177dfbf7e38c1b4a647705
Status: Downloaded newer image for httpd: latest
Pulling php (php:)...
latest: Pulling from library/php
5eb5b503b376: Already exists
8b1ad84cf101: Pull complete
38c937dadeb7: Pull complete
6a2f1dc96e59: Pull complete
09f78174268e: Pull complete
789f1e9593c6: Pull complete
cefa19191812: Pull complete
6773fb0526e7: Pull complete
e0dc0a9c3fe9: Pull complete
Digest: sha256:f1d66b530e99d2e3c2ea302523c5a10ae9e666ddb5ceaacb7dda0af20c7976d7
Status: Downloaded newer image for php:latest
Creating docker_php_1     ... done
Creating docker_mariadb_1 ... done
Creating docker_web_1     ... done
Attaching to docker_mariadb_1, docker_php_1, docker_web_1
php_1      | Interactive shell
php_1      | 
php_1      | php > mariadb_1  | 2022-08-08 20:49:21+00:00 [Note] [Entrypoint]: Entrypoint script for MariaDB Server 1:10.6.5+maria~focal started.
mariadb_1  | 2022-08-08 20:49:22+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
mariadb_1  | 2022-08-08 20:49:22+00:00 [Note] [Entrypoint]: Entrypoint script for MariaDB Server 1:10.6.5+maria~focal started.
mariadb_1  | 2022-08-08 20:49:22+00:00 [Note] [Entrypoint]: Initializing database files
mariadb_1  | 2022-08-08 20:49:23 0 [Warning] You need to use --log-bin to make --expire-logs-days or --binlog-expire-logs-seconds work.
web_1      | AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.18.0.4. Set the 'ServerName' directive globally to suppress this message
web_1      | AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.18.0.4. Set the 'ServerName' directive globally to suppress this message
web_1      | [Wed Aug 08 20:49:23.865790 2022] [mpm_event:notice] [pid 1:tid 140529800916288] AH00489: Apache/2.4.52 (Unix) configured -- resuming normal operations
web_1      | [Wed Aug 08 20:49:23.874958 2022] [core:notice] [pid 1:tid 140529800916288] AH00094: Command line: 'httpd -D FOREGROUND'
docker_php_1 exited with code 0
mariadb_1  | 
mariadb_1  | 
mariadb_1  | PLEASE REMEMBER TO SET A PASSWORD FOR THE MariaDB root USER !
mariadb_1  | To do so, start the server, then issue the following command:
mariadb_1  | 
mariadb_1  | '/usr/bin/mysql_secure_installation'
mariadb_1  | 
mariadb_1  | which will also give you the option of removing the test
mariadb_1  | databases and anonymous user created by default.  This is
mariadb_1  | strongly recommended for production servers.

Step 8: Go to the browser Ping http://localhost:8080 as shown in the below image.

Conclusion:

We have reached the end of this article. In this guide, we have walked you through the steps required to setup Lamp Stack by Docker Compose on Debian 11.3. Your feedback is much welcome.

FAQ
Q
Why we should use Docker?
A
As Docker reduces the need for more infrastructure resources for development and the container created for individual processes can be shared with other apps with instances of these containerized apps using less memory compared to virtual machines – it makes the development and deployment process more cost-effective.
Q
Which command flag used to run Docker compose in background?
A
If you want to force Compose to stop and recreate all containers, use the --force-recreate flag.
Q
How to run a container in the background?
A
To run a Docker container in the background or the detached mode from the terminal, you can use the Docker run command followed by the -d flag (or detached flag) and followed by the name of the Docker image you need to use in the terminal.
Q
What does Docker compose start do?
A
The Docker compose start command is useful only to restart containers that were previously created, but were stopped.
Q
What is Docker-compose vs Docker file?
A
Docker file is a simple text file that contains the commands a user could call to assemble an image whereas Docker Compose is a tool for defining and running multi-container Docker applications