Getting Started with Docker: A Basic Bare Bones Setup for a LAMP Stack

Docker is a powerful tool that can simplify the process of creating and deploying applications. In this tutorial, we’ll walk through a very basic bare bones Docker setup for a LAMP (Linux, Apache, MySQL, PHP) stack.

Prerequisites

Before we get started, you’ll need to have Docker installed on your system. You can download and install Docker from the official website: https://www.docker.com/get-started

Building the Dockerfile

The first step in setting up a basic Docker setup for a LAMP stack is to create a Dockerfile. The Dockerfile is a configuration file that contains instructions for building a Docker image.

Create a new file called Dockerfile in your project directory and add the following lines:

FROM ubuntu:latest

RUN apt-get update && apt-get install -y apache2 php mysql-server

EXPOSE 80

CMD ["/usr/sbin/apache2ctl", "-D", "FOREGROUND"]

This Dockerfile specifies that we want to use the latest version of Ubuntu as the base image. We then install Apache, PHP, and MySQL server. Finally, we expose port 80 (which is used by Apache) and start the Apache service.

Building the Docker Image

Next, we’ll build the Docker image using the Dockerfile. Run the following command in your project directory:

docker build -t my-lamp .

This command tells Docker to build an image based on the Dockerfile in the current directory, and name the image my-lamp.

Running the Docker Container with Persistent Storage

By default, any data that is created or modified in a Docker container is stored in the container’s writable layer. This means that if the container is deleted or recreated, any changes made to the data will be lost. To persist data in a Docker container, we need to use a volume.

A volume is a Docker-managed directory that can be used to store data that needs to persist between container instances. We can use a volume to store our MySQL data.

Run the following command to create a new volume:

docker volume create my-mysql-data

Now we can run the Docker container and mount the volume as follows:

docker run -p 8080:80 -v my-mysql-data:/var/lib/mysql my-lamp

This command tells Docker to run a container based on the my-lamp image, map port 8080 on the host machine to port 80 on the container, and mount the my-mysql-data volume at /var/lib/mysql in the container.

Testing the LAMP Stack with Persistent Storage

Now that the container is running with persistent storage, we can test our LAMP stack by visiting http://localhost:8080/ in a web browser. You should see the default Apache page indicating that Apache is running.

You can also test MySQL by logging in to the container and creating a new database. Run the following command to log in to the container:

docker exec -it <container-id> bash

Replace <container-id> with the ID of the running container, which can be found using the docker ps command.

Once you’re logged in to the container, run the following commands to log in to the MySQL server as the root user and create a new database:

mysql -u root
CREATE DATABASE mydatabase;

Exit the container by running the exit command.

Running the Docker Container

Now that we have our Docker image, we can run a Docker container based on that image. Run the following command:

docker run -p 8080:80 my-lamp

This command tells Docker to run a container based on the my-lamp image, and map port 8080 on the host machine to port 80 on the container.

Testing the LAMP Stack

Now that the container is running, we can test our LAMP stack by visiting http://localhost:8080/ in a web browser. You should see the default Apache page indicating that Apache is running.

You can also test PHP by creating a new file called index.php in your project directory with the following code:

<?php
phpinfo();
?>

Save the file and refresh the page in your web browser. You should now see the PHP information page.

Finally, you can test MySQL by logging in to the container and running the following command:

mysql -u root

This will log you in to the MySQL server as the root user.

Conclusion

In this tutorial, we’ve walked through a very basic bare bones Docker setup for a LAMP stack. By creating a Dockerfile that installs Apache, PHP, and MySQL, building a Docker image from that Dockerfile, and running a Docker container based on that image, we were able to create a functioning LAMP stack that can be accessed from a web browser.

Docker is a powerful tool that can simplify the process of creating and deploying applications. By using Docker, you can create isolated environments for your applications and easily move those environments between development, testing, and production.