LAMP Server (Docker)

References


Files


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

$ tree . -a
.
├── .docker
│   ├── mysql
│   │   └── my.cnf
│   ├── nginx
│   │   └── conf.d
│   │       └── default.conf
│   └── php
│       └── local.ini
├── .env
├── Dockerfile
├── README.md
├── docker-compose.yml
└── src
    └── index.php

6 directories, 8 files

.env:

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

docker-compose.yml:

version: "3"

services:
  nginx:
    container_name: nginx
    image: nginx:stable-alpine
    ports:
      - "8080:80"
    volumes:
      - ./src:/var/www
      - .docker/nginx/conf.d/:/etc/nginx/conf.d/
    depends_on:
      - php
      - mysql
    networks:
      - app-network

  mysql:
    container_name: mysql
    image: mysql:5.6
    command: --default-authentication-plugin=mysql_native_password
    restart: unless-stopped
    ports:
      - 3306:3306
    environment:
      MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
      MYSQL_DATABASE: ${MYSQL_DATABASE}
      MYSQL_USER: ${MYSQL_USER}
      MYSQL_PASSWORD: ${MYSQL_PASSWORD}
    # volumes:
    #   - dbdata:/var/lib/mysql
    #   - .docker/mysql/my.cnf:/etc/mysql/my.cnf
    networks:
      - app-network

  php:
    container_name: php
    build: .
    image: php-pdo:7.3-fpm-alpine
    volumes:
      - ./src:/var/www
      # - .docker/php/local.ini:/usr/local/etc/php/conf.d/local.ini
    ports:
      - "9000:9000"
    networks:
      - app-network

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

networks:
  app-network:
    driver: bridge
# volumes:
#   dbdata:
#     driver: local

Dockerfile:

FROM php:7.3-fpm-alpine

COPY ./src/ /var/www

WORKDIR /var/www

RUN apk update && apk add mysql-client zip libzip-dev

RUN docker-php-ext-install pdo pdo_mysql zip

RUN php -r "copy('https://getcomposer.org/composer.phar', 'composer.phar');" \
  && mv composer.phar /usr/local/bin/composer \
  && chmod +x /usr/local/bin/composer

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