Symfony, elasticsearch and docker environment

S

Hi, and welcome to the second article devoted to the theme:  “How to work with ElasticSearch using Symfony PHP framework”. Here we will prepare our local environment for further development. As you remember from the Part 1 we have to create a web server, microservice application and elasticsearch as standalone docker containers.

ElasticSearch tutorial environment
ElasticSearch tutorial environment

Below is docker compose file that will help us to realize environment represented at scheme above. I am also adding the screen of physical project file structure in order it would be clear how according files are organized.

Project file structure
# docker-compose.yml

version: '3'

services:
  nginx:
    build: ./ci/nginx
    container_name: "web_server"
    links:
      - udemy_phpes_php
    volumes:
      - .:/var/www/udemy_phpes_php
      - ./ci/data/nginx/logs:/var/log/nginx
    ports:
      - "80:80"
    expose:
      - 80

  udemy_phpes_php:
    build: ./ci/php
    container_name: "udemy_phpes_php"
    volumes:
      - .:/var/www/udemy_phpes_php

  udemy_phpes_elasticsearch:
    build: ./ci/elasticsearch
    environment:
      -  discovery.type=single-node
      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
    container_name: "udemy_phpes_elasticsearch"
    volumes:
      - ./ci/data/es/db:/usr/share/elasticsearch/data
      - ./ci/data/es/logs:/usr/share/elasticsearch/logs
      - ./ci/data/es/repo:/usr/share/elasticsearch/repo
    ports:
      - "9202:9200"
    expose:
      - "9202"

Let’s have a look inside the configuration. So first of all, we are having here nginx web server. According docker configuration is located at ci folder. Here is a docker file that installs the server by itself and mounts our server configuration.

# ci/nginx/Dockerfile

FROM ubuntu:18.04

RUN apt-get update && apt-get install -y software-properties-common curl wget

RUN apt-get --no-install-recommends install -y  \
    nginx \
    nano \
    vim

ADD nginx.conf /etc/nginx/
ADD ./sites-available/ /etc/nginx/sites-available/

RUN ln -s /etc/nginx/sites-available/udemy_phpes.conf /etc/nginx/sites-enabled/udemy_phpes

RUN usermod -u 1000 www-data

CMD ["nginx"]

ENV TERM xterm

Our server would be listening at udemy_phpes.com.test (you have to add according record to your hosts file) and forward requests to our application container that is called udemy_phpes_php.

# ci/nginx/sites-available/udemy_phpes.conf

server {
    server_name udemy_phpes.com.test;
    root /var/www/udemy_phpes_php/public;

    location / {
        # try to serve file directly, fallback to index.php
        try_files $uri /index.php$is_args$args;
    }

    location ~ ^/index\.php(/|$) {
        fastcgi_pass udemy_phpes_php:9000;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        include fastcgi_params;
     
        internal;
    }

    location ~ \.php$ {
        return 404;
    }

    access_log /var/log/nginx/udemy_phpes_access.log;
    error_log /dev/stdout warn;
}

Let’s return to the docker compose file and our php application container. Docker conf file look likes the next way. It is built from the official php 7 version + additional packages and tools are installed.

#ci/php/Dockerfile

FROM php:7.3-fpm

RUN apt-get update \
    && apt-get install -y \
      libmcrypt-dev \
      libxml2-dev \
      libpcre3-dev \
      curl \
      git \
      vim \
      nano \
      bzip2 \
      mc \
      sudo \
      silversearcher-ag \
      apt-utils \
      net-tools \
      iproute2 \
      libzip-dev \
      curl \
      git \
      vim \
      nano \
      apt-utils \
      net-tools && \
      apt-get clean

RUN pecl install \
    igbinary \
    oauth \
    xdebug \
    docker-php-ext-enable \

RUN docker-php-ext-enable \
    igbinary \
    oauth \
    xdebug

RUN docker-php-ext-install \
    mysqli \
    pdo \
    pdo_mysql \
    xml \
    mbstring \
    soap \
    xmlrpc \
    opcache \
    zip \
    sockets

RUN docker-php-ext-install bcmath


RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer --version=2.0.13

EXPOSE 9090

RUN groupadd -r --gid 1000 php_user \
    && useradd -r -u 1000 -g 1000 php_user \
    && usermod -aG sudo php_user \
    && echo "php_user ALL=(ALL) NOPASSWD:ALL" | sudo tee -a /etc/sudoers

RUN mkdir -p /home/php_user \
    && chown -R php_user:1000 /home/php_user

RUN chown -R php_user:1000 /home/php_user

COPY ./php.ini /usr/local/etc/php/php.ini
COPY ./xdebug.ini /usr/local/etc/php/conf.d/xdebug.ini

COPY ./entrypoint.sh /var/www/entrypoint.sh

RUN mkdir -p /var/www/udemy_phpes_php \
    && chown -R php_user:1000 /var/www/ \
    && chown -R php_user:1000 /home/php_user \
    && chmod a+rx  /var/www/entrypoint.sh

RUN apt-get update && apt-get install -y zlib1g-dev \
   && docker-php-ext-install zip

RUN ln -s /usr/local/bin/php /usr/bin/php

USER php_user

VOLUME /var/www/udemy_phpes_php/

WORKDIR /var/www/udemy_phpes_php/

ENTRYPOINT ["/var/www/entrypoint.sh"]
#ci/php/entrypoint.sh

#!/bin/bash
set -e

echo -e "`/sbin/ip route|awk '/default/ { print $3 }'`\tdocker.host.internal" | sudo tee -a /etc/hosts > /dev/null

php-fpm

And the last one is our elasticsearch docker container.

# ci/elasticsearch/Dockerfile

FROM elasticsearch:7.13.3

COPY elasticsearch.yml /usr/share/elasticsearch/config/

RUN bin/elasticsearch-plugin install analysis-icu
# ci/elasticsearch/elasticsearch.yml

network:
  host: 0.0.0.0
path:
  repo: /usr/share/elasticsearch/repo
http.cors.enabled: true
http.cors.allow-origin: "*"
http.cors.allow-headers: "Authorization"

To bring up the symfony elasticsearch environment, go to the project root folder and run docker-compose up -d

In the next article (Part 3. “Symfony, elasticsearch – front controller and api documentation”) we will speak about package dependencies we are going to use, some project structure aspects, controller, REST API and response/request models. If you would like to pass all material more fast, then I propose you to view my on-line course at udemy where you will also find full project skeleton. Below is the link to the course. As the reader of that blog you are also getting possibility to use coupon for the best possible low price. Thank you for you attention.


architecture cluster docker elasticsearch flask geo high availability java php programming languages python recommendation systems search systems spring boot symfony