LAMP Server (Docker)

References


Files


https://ifpb.github.io/php-guide/lamp/docker-php-apache.zip

$ tree . -a
.
├── .env
├── Dockerfile
├── README.md
├── docker-compose.yml
└── src
    └── index.php

1 directory, 5 file

.env:

MYSQL_ROOT_PASSWORD=secret
MYSQL_DATABASE=example
MYSQL_USER=devuser
MYSQL_PASSWORD=devpass

docker-compose.yml:

version: "3"

services:
  web:
    container_name: web
    image: ifpb/php:7.3-apache-pdo
    build: .
    networks:
      - app-tier
    ports:
      - 8080:80
    volumes:
      - ./src/:/var/www/html/
    depends_on:
      - mysql

  mysql:
    container_name: mysql
    image: mysql:8.0
    command:
      - --default-authentication-plugin=mysql_native_password
    environment:
      MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
      MYSQL_DATABASE: ${MYSQL_DATABASE}
      MYSQL_USER: ${MYSQL_USER}
      MYSQL_PASSWORD: ${MYSQL_PASSWORD}
    networks:
      - app-tier
    ports:
      - 3306:3306

  adminer:
    container_name: adminer
    image: adminer:4.7.5
    restart: always
    networks:
      - app-tier
    ports:
      - 8081:8080
    depends_on:
      - mysql

networks:
  app-tier:
    driver: bridge

Dockerfile:

FROM php:7.3-apache

RUN apt -y update \
  && apt install -y \
  mariadb-client

# pdo_mysql package
RUN docker-php-ext-install pdo_mysql

Creating LAMP


$ docker-compose up -d
$ docker-compose ps
$ curl -i http://localhost:8080/
$ docker-compose down

Checking PHP


$ docker-compose exec web bash
$ php -i
$ php -m

Running PHP code (Interactive shell)


$ docker run -it --rm --name php -v "$PWD":/usr/src/app -w /usr/src/app php:alpine php
# php --version
$ alias php= 'docker run -it --rm --name php -v "$PWD":/usr/src/app -w /usr/src/app php:alpine php'
$ php --version
$ php -a
Interactive shell

php > $x = 10;
php > echo $x;
10
php > exit

Connecting to Mysql


$ docker-compose exec mysql bash
# mysql -u root -p
mysql> SHOW DATABASES;

Docker commands