[Series] Drupal Env using docker & nginx - 2!
Tags: php, docker, nginx, drupal
Published: 2020-11-28 13:14:03

---
title: [Series] Drupal Env using docker & nginx - 2!
published: true
description: Setting up a drupal env using docker.
tags: php,docker,nginx,drupal
---



So [previously](https://dev.to/hamzahasbi/series-drupal-env-using-docker-nginx-3oen) we discussed how we can set up a quick web server based on nginx & php-fpm for our drupal stack.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/zbmmqj6jim4kahfvbb37.png)
As for now, we'll **dockerize** the environment.

***So basic docker knowledge is a must to go further.***
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/gpiy7gmb2u8pm9ek4zd7.jpg)
First thing we'll need a [docker-compose](https://docs.docker.com/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](https://dev.to/hamzahasbi/series-drupal-env-using-docker-nginx-3oen)), 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](https://hub.docker.com/_/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


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

 - Code can be found [here](https://github.com/hamzahasbi/docker-nginx).
 - Part 1 : [link](https://dev.to/hamzahasbi/series-drupal-env-using-docker-nginx-3oen).
 - Part 2 : [link](https://dev.to/hamzahasbi/series-drupal-env-using-docker-nginx-3-3o24).

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/3iausbk6a2ewuzkrpf2i.jpg)	
> (SPOILER : Notice the use of the network key in the docker compose file :D)