Setting up Multiple PHP Versions in same Host using Docker Container

May 2, 2023 | 4 min read
Shares
facebook sharing button
twitter sharing button
linkedin sharing button
telegram sharing button
pinterest sharing button
wechat sharing button
whatsapp sharing button

This blog is a continuation of my previous blog series on Various ways to run multiple php versions on your host server or development machine.

In , earlier version, we implemented a virtual host to support multiple php versions in apache server.

In this version we will be using Docker containers to implement multiple php servers i.e. php 7.2 and 8.0 in our code.

Let’s get started:

Step 1: Installation of Docker Desktop

The first step is to install Docker Desktop on the OS of your choice. You can go to this link to get the docker installer https://docs.docker.com/get-docker/.

Step 2 : Create Folders and Files as required

First create a main folder where you will be placing two or more projects that will run on separate php versions through docker , (although it is not mandatory to keep all projects inside the same root but it gives easier access for management of projects),

Let , projectphp7 and projectphp8 folders are created as your two projects inside each project create a file name index.php file, inside this each project folder (as per your choice) and add the following code to index.php in each projects.

<?php echo phpinfo() ?>

Now, in the root folder where your multiple php projects will reside, you will need to create a file named docker-compose.yml.

Create two additional files named Dockerfile7 and Dockerfile8 (file without extensions) in the same level as your project resides.

What is docker-compose.yml?

A docker-compose.yml file is a YAML configuration file used to define and manage multi-container Docker applications. It allows you to define the services, networks, volumes, environment variables, and other configuration options for running multiple containers as a single application.

Note : One of the most important considerations when working with YAML files is maintaining proper indentation and structure. YAML relies heavily on indentation to define the hierarchy and relationships between elements, such as lists, dictionaries, and nested data structures. If you distort the indentation , your configuration may not work.

What is a Docker file?

A Dockerfile is a text file that contains instructions to build a Docker image. It has a lot of roles like it specifies the base image to use, commands to install additional dependencies, copy application code into the image, configure environment variables, expose ports, and define the default command to run when the container starts. The standard file convention for the docker file is it does not have any extensions.

If you didn't get what files does and detail it's absolutely fine, we will dig into it deeper in our other blogs.

Now, once you are done , your folder should look like :

Root Folder 	   >	project1 > index.php
                                  project2 > index.php
                                  … 
                                  docker-compose.yml
                                  Dockerfile7
                                  Dockerfile8
                                  …

Your Folder must look like this :

Step 3: Update docker-compose.yml file

version: "3.8"
services:
  php-8.0:
    container_name: php8.0-apache
    build:
      context: .
      dockerfile: ./Dockerfile8
    volumes:
      - ./php8/:/var/www/html/
    ports:
      - 8000:80

  php-7.3:
    container_name: php-apache
    build:
      context: .
      dockerfile: ./Dockerfile7
    volumes:
      - ./php7/:/var/www/html/
    ports:
      - 8001:80

* Volumes: If you check configuration key volumes : ./php7 is the path of your project that run on php7 .so, give the path of your actual project

* Port: The port key is the port that your application will open to, in above code php8.0 app runs in port 8000 (left side of (:) colon) and php7.3 runs on port 8001, you are free to change the port as per your choice.

Step 4: Create and open Dockerfile7 and Dockerfile8 and update the following code(optional)

Dockerfile7

FROM php:7.3-apache

WORKDIR /var/www/html

Dockerfile8

FROM php:8.0-apache

WORKDIR /var/www/html

Step 5: Now run the docker image:

To run the server, open up the Docker desktop that was installed in first step,

Then in the command line point to your project root i.e. in same level of docker-compose.yml file , Then, run following command :

$ /var/www/root docker-compose up

Now if you open up your browser and point:

localhost:8000 → You will get a page that shows php version 8.0 with details

localhost:8001 → Php version 7.3 will open

Congratulations, you have successfully set up multiple php versions for different projects using Docker,

Step 5 (Optional Step ) Setting up mysql and phpmyadmin in docker

If you want to setup phpmyadmin and mysql additionally using docker, you can continue beyond this point:

Copy the below given docker-compose.yml and dockerfile and replace your code and rerun docker compose up

version: "3.8"
services:
  php-8.0:
    container_name: php8.0-apache
    build:
      context: .
      dockerfile: ./Dockerfile8
    volumes:
      - ./php8/:/var/www/html/
    ports:
      - 8000:80
    depends_on:
      - mysql
    networks:
      - multiphp

  php-7.3:
    container_name: php-apache
    build:
      context: .
      dockerfile: ./Dockerfile7
    volumes:
      - ./php7/:/var/www/html/
    ports:
      - 8001:80
    depends_on:
      - mysql
    networks:
      - multiphp

  mysql:
    image: mysql/mysql-server:8.0
    container_name: mysql
    ports:
      - "${FORWARD_DB_PORT:-3307}:3306"
    environment:
      MYSQL_ROOT_PASSWORD: "123456"
      MYSQL_ROOT_HOST: "%"
      MYSQL_DATABASE: "database"
      MYSQL_USER: "root"
      MYSQL_PASSWORD: "123456"
      MYSQL_ALLOW_EMPTY_PASSWORD: 1
      TIMEZONE: "Asia/Kathmandu"
    expose:
      - '3306'
    volumes:
      - "./mysql:/var/lib/mysql"
    networks:
      - multiphp

  phpmyadmin:
    depends_on:
      - mysql
    image: "phpmyadmin"
    environment:
      - PMA_HOST=mysql
      - PMA_PORT = 3307
      - PMA_ARBITRARY=1
    ports:
      - 8081:80
    restart: always
    networks:
      - multiphp

networks:
  multiphp:
    driver: bridge

File : docker-compose.yml

FROM php:7.3-apache

RUN apt-get update -y && apt-get install -y libmariadb-dev && docker-php-ext-install mysqli   && docker-php-ext-install pdo_mysql

RUN a2enmod proxy proxy_http rewrite
RUN  a2ensite 000-default.conf
RUN service apache2 restart

WORKDIR /var/www/html

File: Dockerfile7

FROM php:8.0-apache

RUN apt-get update -y && apt-get install -y libmariadb-dev && docker-php-ext-install mysqli   && docker-php-ext-install pdo_mysql

RUN a2enmod proxy proxy_http rewrite
RUN  a2ensite 000-default.conf
RUN service apache2 restart


WORKDIR /var/www/html

File: Dockerfile8

Replace the corresponding files and rerun docker compose up in command line,

You can access:

Php 8 application : localhost:8000

Php 7 application : localhost:8001

PHPmyadmin : localhost:8081

Mysql: localhost:3006

You can also change your database Databasename,username , password from Environment in as given above in the service named mysql.

You can view other series from :

  1. Various ways to run multiple php versions on your server .
  2. Setting up Multiple PHP Versions using Apache Configurations
Author Profile Picture

sagar chapagain

I am a Software Engineer, a Solution Architect,a Mentor, a Trainor, a Technologist, Speaker, from land of Himalays, Enthusiasts in Tech, Investment and Economy, with a total years of experience in field of software and application development, Deployment .


Shares
facebook sharing button
twitter sharing button
linkedin sharing button
telegram sharing button
pinterest sharing button
wechat sharing button
whatsapp sharing button
Shares
pinterest sharing button
facebook sharing button
twitter sharing button
blogger sharing button
arrow_left sharing button
arrow_right sharing button

WE CARE ABOUT YOUR PRIVACY

sagarchapagin.com
Vendors
Manage Settings
Vendors

sagarchapagin.com Settings

Purposes

We and our partners can:

Vendor Settings

Purposes

Vendors can:

Special Purposes

Vendors can:

  • Ensure security, prevent and detect fraud, and fix errors
  • Deliver and present advertising and content
  • Save and communicate privacy choices

Features

Vendors can:

  • Match and combine data from other data sources
  • Link different devices
  • Identify devices based on information transmitted automatically

Special Features

Vendors can:

Some partners do not ask for your consent to process your data, instead, they rely on their legitimate business interest. Personal data processed includes but is not limited to cookies, IP addresses, and URLs visited. View our list of partners to see the purposes they believe they have a legitimate interest for and object to legitimate interests on a per vendor basis. Manage your settings and object to purposes as a legitimate interest in general.

You can change your settings at any time, including by withdrawing your consent, by clicking on the cog icon in the bottom right hand corner.