Multi Containers Using Docker Compose for Laravel Project

Multi Containers Docker Using Docker Compose for Laravel Project

Here we are going to create three containers with three different Dockerfiles. There will be three different containers are: mysql as mysql_lara and this container for the mysql database and it will be using 5009 port, Laravel project as app_laravel and the project is in the same root directory called test-app which will be using 5011 port, http or php server as lara_servphp7 which will be using 5013 port. Lastly, we will create network which is using bridge network. However, creating the docker environment for Laravel as following – 

  1. Create docker-compose file in the root and copy the following code and past in it,
version: '3'
services:
  mysql_lara:
    build: ./mysql
    container_name: lara_mysql
    ports:
      - '5009:3306'
    environment:
      MYSQL_ROOT_PASSWORD: pass
    volumes:
      - ./initdb.d:/docker-entrypoint-initdb.d
    networks:
      lara:
        ipv4_address: 192.168.100.4
  app_laravel:
    tty: true
    build: ./app_laravel
    container_name: test_laravel
    volumes:
      - ./test-app:/var/www/html:cached
    ports:
      - '5011:80'
    networks:
      lara:
        ipv4_address: 192.168.100.7
    depends_on:
      - mysql_lara
 
  lara_servphp7:
    tty: true
    build: ./lara_servphp7
    container_name: lara_servphp7
    volumes:
      - /Users/hashimoto/git/soe_serv:/var/www/html
      - /Users/hashimoto/git:/var/git
    ports:
      - '5013:80'
    networks:
      lara:
        ipv4_address: 192.168.100.6
    depends_on:
      - mysql_lara
 
 
networks:
  lara:
    driver: bridge
    ipam:
      config:
      - subnet: 192.168.100.0/24Code language: PHP (php)

2.Create a folder called initdb.d and in the folder create a file called dbinit.sql.

3.Create a folder called lara_servphp7 and create a file called Dockerfile and copy the following and past into the Dockerfile you created,

FROM php:7.2-apache 
 
RUN docker-php-ext-install mysqli
RUN docker-php-ext-install pdo_mysql
 
RUN apt-get update && apt-get install -y \
  libssl-dev \
  openssl \
  ssl-cert \
  && apt-get clean \
  && rm -rf /var/lib/apt/lists/* \
  && a2enmod ssl \
  && a2ensite default-ssl
 
RUN php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
RUN php -r "if (hash_file('sha384', 'composer-setup.php') === 'a5c698ffe4b8e849a443b120cd5ba38043260d5c4023dbf93e1558871f1f07f58274fc6f4c93bcfd858c6bd0775cd8d1') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
RUN php composer-setup.php
RUN php -r "unlink('composer-setup.php');"
RUN cp ./composer.phar /usr/bin/composer
 
COPY ./php.ini /usr/local/etc/php/php.iniCode language: PHP (php)

4. Create a file php.ini in the same directory(lara_servphp7) and copy all text from here past it in the file you just created (php.ini).

5. Create a directory called mysql and create a Dockerfile and copy the following code and past in it,

FROM mysql:5.7
COPY ./my.cnf /etc/mysql/conf.d/

6. Create another file called my.cnf in the same directory and copy the following code and past it in the file,

[mysqld]
character-set-server=utf8
sql_mode=Code language: JavaScript (javascript)

7. Create a directory called app_laravel and in the directory create a Docker file and copy and past the following code in it,

FROM php:7.2-apache
#FROM php:7.2-fpm-alpine3.7
 
RUN docker-php-ext-install mysqli
RUN docker-php-ext-install pdo_mysql
RUN pecl install xdebug
RUN docker-php-ext-enable xdebug
 
 
RUN apt-get update && apt-get install -y \
  wget \
  vim \
  libssl-dev \
  openssl \
  ssl-cert \
  && apt-get clean \
  && rm -rf /var/lib/apt/lists/* \
  && a2enmod ssl \
  && a2ensite default-ssl
 
 
 
 
RUN php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
RUN php -r "if (hash_file('sha384', 'composer-setup.php') === 'a5c698ffe4b8e849a443b120cd5ba38043260d5c4023dbf93e1558871f1f07f58274fc6f4c93bcfd858c6bd0775cd8d1') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
RUN php composer-setup.php
RUN php -r "unlink('composer-setup.php');"
RUN cp ./composer.phar /usr/bin/composer
RUN a2enmod rewrite
 
 
COPY ./php.ini /usr/local/etc/php/php.ini
COPY ./000-default.conf /etc/apache2/sites-available/000-default.conf
COPY ./apache2.conf /etc/apache2/apache2.conf
Code language: PHP (php)

8. Create a file called 000-default.conf in the same directory and download all the contains from here and past it in the file.

9. Create a file apache2.conf in the same directory and again download from here and past in it

10. Create php.ini in the same directory and copy and paste from here.

11. Create a Laravel project called test-app or any name you want but if you change the name then it is needed to change the name in the docker-compose.yml file you created at the very beginning.

12. Finally, go to the root or where the docker-composer.yml file located and type:

docker-compose up or docker-compose up -d

and open your browser and  browse

https://localhost:5011Code language: JavaScript (javascript)

Finally, it can be said that installing docker and create docker images is simple and easier and it is totally independent platform to use multiple development tools thereby, it helps to reduce development and deployment Complexities.

Note: if there is some help needed for install to create docker image then feel free to contact ixora solution. 

Add a Comment

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