Set Up PHP development enviorment on Mac, Windows and Linux with Docker

When I start web development in Mac, There was a couple of option to start like WAMP, but docker is the most favorite to a virtual environment for development in these days. But it was not an easy task to like docker pull and start. If you Ubuntu without Docker, I discussed setup LAMP stack environment on Ubuntu in another article.

I was developing the application that required Couchbase Elasticsearch So I start looking for one complete package of docker to do it. I did not find any e readymade solution for PHP 7.3 with Couchbase and Elasticsearch. Because recently Couchbase introduce is PHP SDK 3.1 It requires C SDK

Requirements:

You only need to install docker on your current operating system.

On Linux, you can easily install these libraries via the command line. But when you try to install this SDK with docker for PHP you may face several problems.

I created the complete solution and pushed it on Github to just take a clone and start work. You can see how it works by taking clone the complete repository from GitHub.

Note:

This solution is not only for Mac but it also works on Windows Linux and other operating systems.

In the article, I will go through each file and configuration with you, so become easy to understand and save too much time. Before starting anything just take the clone of the following repository and checkout branch dixeam-blog

https://github.com/link2qaiser/docker-lamp-stack/

docker-compose.ytml

version: "3.2"
services:
  php:
    build:
      context: "./php/"
      args:
        PHP_VERSION: ${PHP_VERSION}
    networks:
      - backend
    volumes:
      - /Users/your-username/Documents/D/practice/php/:/var/www/html/
    container_name: php
  apache:
    build:
      context: "./apache/"
      args:
        APACHE_VERSION: ${APACHE_VERSION}
    depends_on:
      - php
      - mysql
    networks:
      - frontend
      - backend
    ports:
      - "8080:80"
    volumes:
      - /Users/your-username/Documents/D/practice/php/:/var/www/html/
    container_name: apache
  mysql:
    image: mysql:${MYSQL_VERSION:-latest}
    restart: always
    ports:
      - "3306:3306"
    volumes:
      - /Users/your-username/Documents/D/databases/mysql:/var/lib/mysql
    networks:
      - backend
    environment:
      MYSQL_ROOT_PASSWORD: "${DB_ROOT_PASSWORD}"
      MYSQL_DATABASE: "${DB_NAME}"
      MYSQL_USER: "${DB_USERNAME}"
      MYSQL_PASSWORD: "${DB_PASSWORD}"
    container_name: mysql
networks:
  frontend:
  backend:
volumes:
  data:

The docker-compose.yml main docker configuration file to download Apache, PHP, MySQL, and Couchbase SDK. You can replace your-username with your Mac username. You can read more about the docker-compose.yml file in docker documentation.

PHP Docker File:

./php/Dockerfile

contains script to install the PHP extension, Here is the key part to install the Couchbase extension. It will install the following PHP extensions

  1. mysqli
  2. Zip
  3. Gd
  4. Imagick
  5. Imap
  6. Mailparse
  7. Opcache
  8. Xdebug

You can add and remove your required extensions and it also copies the php.ini file into the container.

What is:

A Dockerfile is a simple plain text file consisting of a list of commands to make an image. It contains UNIX-like commands to build an image.

Apache Docker File:

./apache/DockerFile

same as PHP DockerFile, In this file every extension you installed will for Apache PHP. In PHP DockerFile extensions only will reflect in the command line but when you install the same extension in Apache DockerFile it will reflect in php.info and all apache requests.

Note:

When you install an extension in PHP DockerFile you can list via PHP -m but when you run PHP code in Apache webserver these extensions do not reflect, so you need to install the same extensions in Apache DockerFile

After the update, the docker-compose.yml file with your local path and just run.

$ docker-compose up -d --build

It will take time and install all dependencies. After complete, the installation just browses

 http://0.0.0.0

in the browser. Now your web server is running.

Happy Setup!