DEV Community

loading...

[Series] Drupal Env using docker & nginx - 2!

hamzahasbi profile image Hasbi Hamza Updated on ・3 min read

So previously we discussed how we can set up a quick web server based on nginx & php-fpm for our drupal stack.

Alt Text
As for now, we'll dockerize the environment.

So basic docker knowledge is a must to go further.
Alt Text
First thing we'll need a docker-compose file where we will be grouping or our needed services for our case :

  1. nginx
  2. php-fpm
  3. mariadb
  4. memcached (optional)
  5. redis (optional)

In each one of our containers we'll provide the config files needed (same files from the first tutorial), those files will be copied directly into our containers.

Next thing we will expose & map each service with the port of our choice on the host machine and we'll mount the intended codebase project as a volume in the nginx & php-fpm containers so that we can access it within the container, moreover we need persistent database schemas so we will have to add an external volume where we will store data from Mariadb container.

Furthermore, we can add dependencies for the mandatory services (webserver - database and php-fpm) by linking those we make sure that the webserver would never start before having an accessible php container & mariadb .

Now that i covered the configuration files, let's move to the used docker images .
For mariadb i've just used the official image from docker-hub because i don't need any prior on build config ....
As for the php image i've created a custom image with some drupal tweaks that i find usefull such as adding some packages (gd imagck apcu memcached redis composer) and drush (any drupal dev would find it usefull ).
In addition to that i'm using a slitely custom nginx image mainly for 2 reasons :

  • Brotli compression support .
  • User setting (for both php & nginx images we need to have a user to own the project root).

Note that changing the config files of nginx can be tricky so to play it safe here i've managed to do it on build time in the Dockerfile.
So finaly we got the following docker-compose file

version: "3"
services:
  web:
    build: ./web
    restart: always
    ports:
      - "80:80"
      - "443:443"
    links:
     - php
     - database
    networks:
      vpcbr:
        ipv4_address: 172.28.0.101
    environment:
      - NGINX_PORT=80
    volumes:
      - ../:/var/www/html
  php:
    build: ./php
    restart: always
    container_name: d8_php
    links:
      - database
    ports:
      - "9000:9000"
    volumes:
      - ../:/var/www/html
    networks:
      vpcbr:
        ipv4_address: 172.28.0.102

  database:
    image: mariadb:latest
    restart: always
    hostname: mysql
    environment:
      - MYSQL_ROOT_PASSWORD=root
    networks:
      vpcbr:
        ipv4_address: 172.28.0.103

    volumes:
      - "./db:/var/lib/mysql"
      - "./database:/etc/mysql/conf.d"
    ports:
      - "3307:3306"
  redis:
    image: redis
    ports:
      - "6379:6379"
    networks:
      vpcbr:
        ipv4_address: 172.28.0.104
  memcached:
    image: memcached
    ports:
      - "11211:11211"
    networks:
      vpcbr:
        ipv4_address: 172.28.0.105
networks:
  vpcbr:
    driver: bridge
    ipam:
      config:
        - subnet: 172.28.0.0/16
Enter fullscreen mode Exit fullscreen mode

Next thing we'll discuss the Drupal config and how we can link all the containers together.

Alt Text

(SPOILER : Notice the use of the network key in the docker compose file :D)

Discussion (0)

pic
Editor guide