Deploy LAMP and/or LEMP stacks easily using Docker

After reading the title of this guide, you might wonder why should we deploy LAMP and LEMP stacks using Docker? Why can’t we just install it manually? Setting up LAMP and LEMP stack is very easy, then Why should we complicate with Docker? Let me answer your questions. Trust me, deploying LAMP or LEMP or any other stacks using Docker is much easier than manually installing them.

Why do we need to deploy LAMP/LEMP stacks using Docker?

As you probably know, Docker is light-weight, and os-level virtualization solution. You do not need any kind of virtualization softwares such as VirtualBox, Xen, or VMWare etc., to test and deploy multiple operating systems. For more details about Docker installation and usage, please refer the following link.

And, why do we need to deploy LAMP or LEMP stacks with Docker? Because, we can easily pull ready-made LAMP or LEMP images and run them instantly in couple of minutes. There is no need to complicate yourself by manually installing Apache, MySQL/MariaDB, and PHP softwares one by one. Let us say you are a developer or a tester or any random software enthusiast, and you want to test a web-based application. To install a web-based application, you must setup install Apache/Nginx, MySQL/MariaDB, and PHP (That is what we call LAMP or LEMP stacks). This will be a time-consuming process. This is where, Docker comes in help.

Using Docker, you can easily download and deploy pre-configured applications instantly without much hassle, and start using them in minutes. Sounds, good? Well it should be.

Now, let us get into the topic. This tutorial will show you how to deploy LAMP or LEMP stacks with Docker.

Deploying LAMP/LEMP stacks using Docker

After installing Docker as described in the above link, let use search for the ready-made LAMP/LEMP stacks.

To do so, run:

$ sudo docker search lamp

Sample output:

 reinblau/lamp Dockerfile for PHP-Projects with MySql client 17 [OK]
 dockie/lamp 6 [OK]
 nickistre/ubuntu-lamp LAMP server on Ubuntu 4 [OK]
 nickistre/ubuntu-lamp-wordpress LAMP on Ubuntu with wp-cli installed 4 [OK]
 nickistre/centos-lamp LAMP on centos setup 3 [OK]
 damienlagae/lamp Docker LAMP with supervisord 3 [OK]
 boolean93/lamp LAMP based on linode/lamp 2 [OK]
 drunomics/lamp 1 [OK]
 avatao/lamp LAMP base image 1 [OK]
 nickistre/ubuntu-lamp-xdebug LAMP on Ubuntu with xdebug installed 1 [OK]
 nickistre/centos-lamp-wordpress LAMP on CentOS setups with wp-cli installed 1 [OK]
 linuxconfig/lamp Automated build LAMP stack environment for... 1 [OK]
 greyltc/lamp a super secure, up-to-date and lightweight... 0 [OK]
 kazaoki/lamp ローカルフォルダをマウントす... 0 [OK]
 greyltc/lamp-gateone LAMP stack with gateone server & webdav 0 [OK]
 fauria/lamp Modern, developer friendly LAMP stack. Inc... 0 [OK]
 drunomics/lamp-memcached LAMP + Memcached base image. 0 [OK]
 rpawel/lamp Apache 2.4 + php5-fpm container 0 [OK]
 lioshi/lamp Docker image for LAMP + MySql under debian 0 [OK]
 nickistre/centos-lamp-xdebug LAMP on centos with xDebug 0 [OK]
 greyltc/lamp-aur LAMP stack (in Arch with php7) with AUR ac... 0 [OK]
 alledia/lamp General LAMP for our tests, based on phusi... 0 [OK]
 greatfox/lamp 0 [OK]
 cnrk/lamp LAMP stack Docker image. 0 [OK]
 grmanit/lamp Based on tutum/lamp with additional settin... 0 [OK]

sk@server: ~_009

As you see in the above result, there are many ready-made LAMP stack images available for Arch Linux, CentOS, and Ubuntu. The images are sorted by rating.

Similarly, you can search LEMP stack Docker images with command:

$ sudo docker search lemp

Then, pull a Docker image of your Choice. Here, I am going to download nickistre/ubuntu-lamp image.

$ docker pull nickistre/ubuntu-lamp

Sample output:

Using default tag: latest
latest: Pulling from nickistre/ubuntu-lamp

faecf96fd5ab: Pull complete 
995977506e98: Pull complete 
efb63fb8dcb6: Pull complete 
a3ed95caeb02: Pull complete 
61626f5cc06d: Pull complete 
d42e54d21590: Pull complete 
4a32d1f581a1: Pull complete 
52f44a8dd6d0: Pull complete 
ce6c1074ae9e: Pull complete 
2fa559435609: Pull complete 
93a433221384: Pull complete 
521d09b9a2d1: Pull complete 
6222edddc59d: Pull complete 
8fa401b50efc: Pull complete 
683063a5d5e0: Pull complete 
1f87fa5088b3: Pull complete 
c5ee1c14048f: Pull complete 
Digest: sha256:e913d43c204b3cdb931156c1a680c712acfe8db531225ec7b9e4708fc7ebe93c
Status: Downloaded newer image for nickistre/ubuntu-lamp:latest

The above command will download the LAMP stack for Ubuntu. You can download the LAMP stacks for other platforms such as CentOS, Arch Linux.

Alternatively, you can search, download, and deploy Docker images from Docker hub, where thousands of the most popular images hosted.

Search for the LAMP stack images and download them. - Google Chrome_014

Click the Docker images to view further details such as how to install and how to use them. - Google Chrome_015 - Google Chrome_001

The downloaded images will be saved locally in the /var/lib/docker/ directory.

To list the downloaded Docker images, run the following command:

$ docker images

Sample output:

nickistre/ubuntu-lamp latest 5e750e4f49e4 2 days ago 633 MB
reinblau/lamp latest 2813b461f843 9 days ago 697.9 MB
hello-world latest 690ed74de00f 5 months ago 960 B

Well, After downloading the Docker image for LAMP/LEMP stack, run it using command:

$ docker run -it nickistre/ubuntu-lamp /bin/bash

After running the above command, you’ll be automatically logged-in to the Container’s web root directory as ‘root’ user.

[email protected]:/#

Start Apache service using command:

# service apache2 start

Then, MySQL service using command:

# service mysql start

Check if Apache server is working. To do so, open your web browser and navigate to http://container_IP/.

To find the IP-address, just type ‘ifconfig’ or ‘ip addr’ in the Container’s Terminal.

You should see the Apache server test page.

You can check the version of Apache, MySQL, and PHP using commands:

# apache2 -v
# mysql -v
# php -v

root@184851ac9ebd: -_002

That’s it. LAMP stack in Ubuntu has been deployed. Now, you can start to deploy and test your web-based applications.

Note for the beginners :

Use Docker for just testing and development purposes only. Also, do not confuse Docker with other Virtualization tools. Docker will not deploy LAMP stack or any other apps directly on your host system. What exactly Docker will do is setup a guest os (We call it as ‘container’), and deploy the LAMP stack inside the guest os. Technically, it is same as other Virtualization technologies, but the main difference is unlike other virtualization methods such as VirtualBox, KVM, and Vmware, the guest systems (containers) uses the kernel of the host system. All guests will efficiently share the resources such as CPU, RAM, Hard disk, and network etc of your original host computer. Hope you clear now.

That’s all for now. I will be here soon with an another interesting guide. Until then, stay tuned with Unixmen.