Skip to content

Setting Up LAMP Stack with Docker Compose

Advertisement

Hey there! If you’re looking to set up a LAMP stack (that’s Linux, Apache, MySQL, and PHP) for your web projects, using Docker Compose is a super easy and efficient way to do it. In this guide, we’ll walk you through the steps to get your LAMP stack up and running smoothly with Docker Compose. Whether you’re a beginner or just want a hassle-free setup, this article is for you! Let’s dive in and make it happen.

Prerequisites

Before we dive into the setup process, ensure you have the following installed on your system:

  • Docker: A platform for developing, shipping, and running applications in containers.
  • Docker-Compose: A tool for defining and running multi-container Docker applications.

You can check the installation of these tools by running docker -v and docker-compose -v in your terminal. If you haven’t installed them yet, visit the official Docker website for installation instructions.

Step-by-Step LAMP Stack Setup

Step 1: Create a Project Directory

First, create a folder for your project. This will hold your Docker Compose file and other necessary files.


mkdir lamp-docker
cd lamp-docker

Step 2: Write the Docker Compose File

Start by creating a docker-compose.yml file in your project directory. This file is crucial as it orchestrates the setup of your LAMP stack, specifying how each service should be built and interact.


version: '3'
services:
  web:
    build: .
    container_name: apache-php
    ports:
      - "80:80"
    volumes:
      - ./www:/var/www/html
  db:
    image: mysql:8
    container_name: mysql-server
    command: --default-authentication-plugin=mysql_native_password
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: secure_password
      MYSQL_DATABASE: mydb
      MYSQL_USER: myuser
      MYSQL_PASSWORD: password
    ports:
      - "3306:3306"
    volumes:
      - db_data:/var/lib/mysql
volumes:
  db_data:

This configuration details two main services: web for Apache and PHP, and db for MySQL. Notice the build: . directive under web, indicating that Docker should use a Dockerfile in the current directory to build this service.

Step 3: Customize PHP with a Dockerfile

To incorporate additional PHP modules and Composer, create a Dockerfile in the same directory as your docker-compose.yml. This Dockerfile will instruct Docker on how to prepare your custom PHP environment.


nano Dockerfile

Add the following content.


FROM php:8.3-apache

# Install additional PHP modules
RUN docker-php-ext-install pdo_mysql mysqli

# Install Composer
RUN php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" && \
    php composer-setup.php --install-dir=/usr/local/bin --filename=composer && \
    php -r "unlink('composer-setup.php');"

# Enable Apache mod_rewrite
RUN a2enmod rewrite

This Dockerfile extends the php:8.3-apache image by adding the pdo_mysql and mysqli extensions for database interactions and installing Composer globally.

Step 4: Building and Running Your Containers

With your docker-compose.yml file ready, you can now launch your LAMP stack. Open a terminal, navigate to your project directory where the docker-compose.yml file is located, and run the following command:

docker-compose build 
docker-compose up -d 

The -d flag runs your containers in detached mode, freeing up your terminal. Docker-Compose will pull the necessary images from Docker Hub, create the defined services, and set up the network and volumes as specified.

Step 5: Accessing Your LAMP Stack

Once the containers are up and running, you can access your Apache server by visiting http://localhost in your web browser. You should see the default Apache page or the contents of your ./www directory if you’ve added any HTML or PHP files.

To interact with your MySQL database, you can use the following command to access the MySQL CLI:

docker-compose exec db mysql -uroot -proot

Replace root with the password you defined in your docker-compose.yml file if necessary.

Step 6: Managing Your LAMP Stack

Docker-Compose makes it easy to stop, start, and rebuild your services. Use the following commands as needed:

  • To stop your containers: docker-compose down
  • To start your containers again: docker-compose up -d
  • To rebuild your services after making changes: docker-compose up -d --build

Conclusion

Using Docker Compose, you’ve set up a fully functional LAMP stack in just a few steps. You can now build and test your PHP applications without worrying about complex server configurations. Docker keeps everything tidy and portable, so you can focus on coding. Try adding more PHP files or connecting to the database to create something awesome. Happy coding!

If you have questions or want to dive deeper into Docker or LAMP, let me know!

Rahul

I, Rahul Kumar am the founder and chief editor of TecAdmin.net. I am a Red Hat Certified Engineer (RHCE) and working as an IT professional since 2009..

1 Comment

  1. Thanks for your tutorial.
    Only one question:
    at point 2, create a Dockerfile in the same directory to incorporate additional PHP modules and Composer, you offer the important line to implement your docker, but don’t tell us how you have to name this file.

    Thanks for your help.

Leave a Reply

Your email address will not be published. Required fields are marked *


Back to top