Decoding Docker: Embarking on a LAMP Server Journey for Beginners

Sameera Abeysekara
4 min readDec 25, 2023

--

Docker is a platform designed to make it easier to create, deploy, and run applications using containers. Containers allow developers to package up an application with all its dependencies, ensuring that it runs reliably on any environment.

Containers and Images

At its core, Docker revolves around two main concepts: containers and images.

  • Containers: These are lightweight, runnable instances of an image. They encapsulate an application and its dependencies, ensuring consistency and isolation. Containers run in an isolated environment on the host operating system, sharing the kernel but having their own file system, processes, and networking.
  • Images: An image is a read-only template containing the application and everything needed to run it — code, libraries, environment variables, and configuration files. Images are the basis for containers. They can be created, modified, and shared to facilitate the deployment of applications.

Key Components

  • Docker Engine: The core of Docker that enables container creation, management, and running instances of Docker containers.
  • Dockerfile: A text file that contains instructions for building a Docker image. It specifies the base image, necessary dependencies, environment variables, and other configurations needed to create the image.
  • Docker Hub: A repository hosting various Docker images shared by the community. Users can pull images from Docker Hub to use them in their own environments.

Advantages of Docker

  1. Portability: Docker containers run consistently across various environments, from development to production, irrespective of the underlying infrastructure.
  2. Resource Efficiency: Containers share the host OS kernel, making them lightweight compared to traditional virtual machines. They consume fewer resources and can be started and stopped quickly.
  3. Isolation: Each container runs in its own isolated environment, preventing conflicts between different applications or dependencies.

Use Cases

  • Microservices Architecture: Docker facilitates breaking down applications into smaller, independent services that can be developed, deployed, and scaled individually.
  • Continuous Integration/Continuous Deployment (CI/CD): Docker simplifies the deployment pipeline by providing a consistent environment for testing and deployment.
  • Development Environments: Docker allows developers to create consistent development environments, ensuring that the code runs uniformly across all machines.

Setting Up a LAMP Server with Docker

version: '3'
services:
db:
# MySQL service
image: mysql:latest
environment:
MYSQL_DATABASE: dockerDb
MYSQL_USER: dockerUser
MYSQL_PASSWORD: 123
MYSQL_ALLOW_EMPTY_PASSWORD: 1
networks:
- lamp-docker

www:
# PHP/Apache service
depends_on:
- db
image: php:8.1.1-apache
volumes:
- "./:/var/www/html" # Mounts the current directory as the web server's root
ports:
- 80:80 # Maps host port 80 to container port 80 (HTTP)
- 443:443 # Maps host port 443 to container port 443 (HTTPS)
networks:
- lamp-docker

phpmyadmin:
# phpMyAdmin service
depends_on:
- db
image: phpmyadmin/phpmyadmin
ports:
- 8001:80 # Maps host port 8001 to phpMyAdmin's default port 80
environment:
- PMA_HOST=db
- PMA_PORT=3306
networks:
- lamp-docker

networks:
lamp-docker:
driver: bridge

Let’s break down the Docker Compose file and explain its components:

version: '3'

  • This specifies the version of the Docker Compose file format being used.

services:

  • This section defines the various services (containers) that constitute the LAMP stack.

db:

  • Service for MySQL.
  • image: mysql:latest: Pulls the latest MySQL image from Docker Hub.
  • environment: Sets up environment variables for MySQL, including the database name, user, password, and allowing an empty password for simplicity in this example.
  • networks: Connects this service to the specified network named lamp-docker.

www:

  • Service for PHP with Apache.
  • depends_on: Specifies that this service depends on the db service.
  • image: php:8.1.1-apache: Pulls the PHP 8.1.1 with Apache image from Docker Hub.
  • volumes: Mounts the current directory (./) as the web server's root directory (/var/www/html).
  • ports: Maps host ports 80 and 443 to container ports 80 and 443, respectively, allowing HTTP and HTTPS traffic.
  • networks: Connects this service to the lamp-docker network.

phpmyadmin:

  • Service for phpMyAdmin.
  • depends_on: Specifies that this service depends on the db service.
  • image: phpmyadmin/phpmyadmin: Pulls the phpMyAdmin image from Docker Hub.
  • ports: Maps host port 8001 to phpMyAdmin's default port 80, allowing access to the phpMyAdmin interface.
  • environment: Sets up environment variables to define the MySQL host and port for phpMyAdmin to connect.
  • networks: Connects this service to the lamp-docker network.

networks:

  • Defines the custom network named lamp-docker with a bridge driver to allow communication between the services.

How to Use the Docker Compose File:

  1. Install Docker and Docker Compose: If you haven’t already, install Docker and Docker Compose on your system.
  2. Create a Directory: Create a new directory for your project and save the docker-compose.yml file in it.
  3. Run Docker Compose: Open a terminal, navigate to the directory with the docker-compose.yml file, and run:
docker-compose up -d

This command will pull the necessary images and start the services defined in the file in detached mode (-d).

Accessing Services:

  • The web server can be accessed at http://localhost or http://<your_host_ip> on port 80.
  • phpMyAdmin can be accessed at http://localhost:8001 or http://<your_host_ip>:8001.

Conclusion

Understanding Docker is fundamental for modern software development and deployment. It offers a standardized way to package, distribute, and run applications, providing consistency and reliability across different environments.

Feel free to explore Docker’s vast ecosystem, including orchestration tools like Kubernetes, to manage containerized applications at scale.

By leveraging Docker’s capabilities, developers and operations teams can streamline workflows, improve deployment processes, and foster greater collaboration across teams.

--

--