Local php development with docker

We will develop php application in our local system without installing XAMPP or WAMP. Assuming we have docker and portainer running in our system.

As we know without database (MySQL) there is no application can work properly. So we will install mysql and php myadmin with attachable network.

First create a network in docker –

docker network create --driver=overlay --attachable local_dev_backend

Now in portainer create a stack named – “devmysql”

version: '3.4'

services:

  application_db:
    image: mysql:5.7
    environment:
      MYSQL_DATABASE: ${MYSQL_DATABASE}
      MYSQL_USER: ${MYSQL_USER}
      MYSQL_PASSWORD: ${MYSQL_PASSWORD}
      MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
      MYSQL_ROOT_HOST: '%'
    volumes:
      - dbdata:/var/lib/mysql
    ports:
      - 3306:3306
    networks:
      - local_dev_backend 

  help_phpmyadmin:
    image: phpmyadmin/phpmyadmin
    environment:
      PMA_ARBITRARY: 1
      UPLOAD_LIMIT: 300M
    ports:
      - ${PUBLIC_PMA_PORT}:80
    networks:
      - local_dev_backend 
      
volumes:
  dbdata: 
networks:
  local_dev_backend:
    external: true

And of course we need to provide the environment details –

MYSQL_DATABASE=<YOUER_DB_NAME>
MYSQL_USER=<YOUER_DB_USER>
MYSQL_PASSWORD=<YOUER_DB_PASSWORD>
MYSQL_ROOT_PASSWORD=<YOUER_DB_ROOT_PASSWORD>
PUBLIC_PMA_PORT=8801

And now in you php application root folder create docker file named – “Dockerfile.local” and put the following details –

FROM php:7.2-apache
RUN docker-php-ext-install mysqli pdo pdo_mysql
RUN a2enmod rewrite
RUN service apache2 restart

Now create “docker-compose.local.yml” to provide all the details from build image to connect the mysql network, like –

version: '3.4'

services:

    app_kisholoy_web:
        build:
            context: .
            dockerfile: Dockerfile.local
        volumes:
            - .:/var/www/html/
            - app_log:/var/www/html/logs
        ports:
            - ${APP_PORT}:80
        environment:
            APP_CONFIG_1: ${APP_PORT}            
        networks:
            - local_dev_backend           
    
volumes:  
    app_log: 
    
networks:
    local_dev_backend:
        external: true

now create a “local.env” file to provide the environment details –

APP_PORT=8080

Now to start the docker server for live work run the command –

docker-compose -f docker-compose.local.yml --env-file local.env --project-name local_app_dev up -d --build

And to stop the development environment run the below command –

docker-compose -f docker-compose.local.yml --env-file local.env --project-name local_app_dev down

Now if you make any changes on your code that you can easily see on your browser while in your local machine there is no XAMPP or WAMP present.

Related posts:

Leave a Reply

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