How to Manage a LAMP Stack with Docker

How to Manage a LAMP Stack with Docker

Setting up a Linux, Apache, MySql and PHP stack usually requires some third party bundles or each individual software separately. The major issue in either of these is that it is not replicable, and if you want same environment again you will need to do it all again.

Thanks to Docker, we need to setup an environment only once and then keep replicating as many times as we want by executing a single command. In this blog we will discuss the specific details to setting up LAMP stack with Docker but if you are interested to know how Docker replicates an environment by using a single command you can find details on Docker website.

The blog will also focus on setting up LAMP on already installed Docker machine and if you need help with Docker installation refer to the Docker website.

I am using CentOs, you can choose any of your choice.

To get an image of the CentOs Linux distribution, open the command shell and run the command:

docker pull centos

This will search the CentOs image on Docker Hub and download it from there and once we get the image, we are ready to setup our environment in CentOs.

First we’ll run a container on the pulled image:

docker run -it --name lampContainer centos

This command will launch a container named lampContainer in an interactive mode. Let’s create our environment by installing some applications inside the container.

# yum update
# yum install git
# yum install httpd
# exit

We can exit the container after successfully installing some stuff on our Linux container.

Our container environment will experience changes due to our installations. And we want to replicate these changes into another environment. This can be done by making use of the following command after creating a user account on Docker Hub.

docker commit lampContainer <myuser>/myenv

Where <myuser> is the username. This command will create an image from our container’s present state, and name it myenv. All changes are carried into this image.

docker push <myuser>/myenv

This command will push the image into docker hub for the first time and on each execution update the existing image with the newly pushed image.

Remember, we need to get the environment updates into another environment. The image myenv can be pulled from another place by using the following command:

docker run -it --name lampCustomizedContainer <myuser>/myenv

This would run a container from our image having all the updates we had made. For instance, we had installed git. Check:

# git --version
	git version 2.17.0
	# exit

Now, where is MySQL in all this? We will create a separate container for MySQL and link it with our customized container.

docker run -d --name mysqlContainer -p 3306:3306 -e MYSQL_ROOT_PASSWORD=mypass mysql

It’s a simple command which runs a container from mysql image in detached mode, notice -d. Names it mysqlContainer, exposes port 3306 and sets the root password “mypass”.

docker run -it --link mysqlContainer: mysqlContainer --name lampCustomizedContainer -p 80:80 <myuser>/myenv

This command runs a container from our image myenv, links it with mysqlContainer on it’s exposed port, i-e 3306, and exposes it’s own port 80.

# service httpd start

With the help of the command below, we have successfully setup LAMP stack with Docker and it is accessible on local host http://localhost/ .


USA408 365 4638


1301 Shoreway Road, Suite 160,

Belmont, CA 94002

Contact us

Whether you are a large enterprise looking to augment your teams with experts resources or an SME looking to scale your business or a startup looking to build something.
We are your digital growth partner.

Tel: +1 408 365 4638
Support: +1 (408) 512 1812