Docker composer – postgres / pgadmin / nginx / php-fpm with different php versions

This post is using the default docker files that have already been pre-defined, the second post will use an alpine mini root file system as a base and then add on the additional packages for each part of this setup.

Folder listings

My folder listing for the below.

conf.d  Dockerfile  nginx.conf  sites



Dockerfile  xdebug.ini

(extras are for the logs / postgres volumne mounted data)


Enviornment file

To start with, I like to create a env(ironment) file that will denote internal settings used within the docker file builds. This file below will denote the php version that I wish to use and also since we are using php-fpm in this development environment, then might as well enable xdebug as well.

Below is the local.env — please save as that as well, shall include a git repo below.




This starts of with the PHP version, follows onto the postgres default details with also the PGADMIN default login details with the tail of the environment details having the xdebug_port defined.

Docker-composer file

The next part is the docker-composer file, this describes how the containers will either rely on each other (depends_on within the docker composer file) networks to use and also the ports to expose. As a side thing, I always found the ports to be funny way around so it is <outside of the container>:<internal to the container> so port 8080:80 will expose the interal port 80 (websites that aren’t using SSL run on this port for example) to the outside world on port 8080 e.g. to host you can do http://localhost:8080 to view the containers port 80 service.

Also, if you are not altering the base docker files that are being pulled down from the docker repo (shall do another post about altering this to a local repo or AWS ECS) then there is no build descriptions akin to the db container description below, but for php-fpm / nginx there are some extra build steps required for this demo hence the Dockerfile(s) in those areas.

# Use postgres/example user/password credentials
version: '3.1'


    image: postgres
    restart: always
    hostname: postgresDB
    container_name: postgresDB
      - ./postgres-data:/var/lib/postgresql/data
      - 5432:5432
      - iKnowNW

    image: dpage/pgadmin4
    hostname: pgadmin4
    container_name: pgadmin4
      - db 
    restart: always
      - 8080:80
      - iKnowNW

      context: ./php-fpm
      - db
      - XDEBUG_CONFIG=client_port=${XDEBUG_PORT}
      - ../src:/var/www
      - ./logs:/var/logs
      - ./php-fpm/xdebug.ini:/usr/local/etc/php/conf.d/xdebug.ini
      - iKnowNW      

      context: ./nginx
      - ../src:/var/www
      - ./nginx/nginx.conf:/etc/nginx/nginx.conf
      - ./nginx/conf.d/:/etc/nginx/conf.d
      - ./nginx/sites/:/etc/nginx/sites-available
      - ./logs:/var/log
      - php-fpm
      - 8081:80
      - iKnowNW        

      driver: bridge


So the main things are within the php-fpm confirguration above



Both of these are import, because they will pass details (arguments / enviornment variables) to the php-fpm DockerFile build process, so lets start with that — below is the php-fpm Dockerfile


FROM php:${PHP_VERSION}-fpm-alpine

RUN apk --update --no-cache add git postgresql-dev
RUN apk add --no-cache $PHPIZE_DEPS
RUN pecl install xdebug 
RUN docker-php-ext-install pdo pdo_pgsql 
RUN docker-php-ext-enable xdebug

WORKDIR /var/www

So the ARG variable is what was passed in from the docker-composer file within this instance it would be 8.0 denoting the php version to use. Also with exposing the xdebug port to the outside world of the container.

Additional xdebug settings are included in the xdebug.ini file below, this will be “copied” into the container during the build process.



This one is the biggest folder, as the following steps will take place

  • Insert the nginx.conf file — this is the service configuration — into the container
  • Insert the default site configuration that uses the php-fpm container using the fast cgi protocol.
  • Insert the configuration of the php-upstream of the php-fpm container

Lets start with the nginx.conf file, it just describes the nginx service with connections / logs and where the http configurations are placed etc.

user  nginx;
worker_processes  4;
daemon off;

error_log  /var/log/nginx/error.log debug;
pid        /var/run/;

events {
    worker_connections  1024;

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;
    access_log  /var/log/nginx/access.log;
    sendfile        on;
    keepalive_timeout  65;

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-available/*.conf;

The default.conf is the default site, as from above it is placed into the /etc/nginx/sites-available folder.

server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    server_name localhost;
    root /var/www/public;
    index index.php index.html index.htm;

    location / {
         try_files $uri $uri/ /index.php$is_args$args;

    location ~ \.php$ {
        try_files $uri /index.php =404;
        fastcgi_pass php-upstream;
        fastcgi_index index.php;
        fastcgi_buffers 16 16k;
        fastcgi_buffer_size 32k;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_read_timeout 600;
        include fastcgi_params;

    location ~ /\.ht {
        deny all;

The most important part here is the “root” key value pair above, this is where the local php folder needs to be mounted for the site to work.

The last part is php-fpm upstream service (conf.d/default.conf)

upstream php-upstream {
    server php-fpm:9000;

Literally, the php-fpm value above matches the container name from the docker-compose file above.

And then the actually Dockerfile is very small!!

FROM nginx:alpine
WORKDIR /var/www
CMD ["nginx"]

Literally describing where to get the nginx base container from e.g. nginx repository with the tag of alpine. Then define the WORKDIR (working directory) for where a container is “cd” into for the command (CMD) to be executed.

Final part!

Create the subdirectories

  • logs
  • postgres-data

To store the containers data that isn’t lost after the container has been stopped / killed (containers are in theory ephemeral — short existence)

Last step is to run the build process and then view your code that you have within your ../src directory (this is where the PHP hosting code will be — I am using the a sub folder within there called public e.g. ../src/public/ will be where the website viewable code controllers etc after a symfony creation script)

To view the PGADMIN page — just goto http://localhost:8080/

To view the PHP hosting code — just goto http://localhost:8081/

Have fun — if there are any issues, please contact me!! but here is my script to either build /run the container setup above.

## place your local enviornment file name here

case "$1" in 
   echo "STARTING"
   docker-compose --env-file=$ENV -f docker-compose.yml up
   echo "STOPPING"
   docker-compose --env-file=$ENV -f docker-compose.yml down
   $0 stop
   $0 start
   echo "REBUILD"
   docker-compose --env-file=$ENV -f docker-compose.yml up --build
   docker-compose pull

   echo "Usage: $0 {start|stop|rebulid|upgrade}"

exit 0 

Linux – Steam – Half life 2

I have started to use Arch linux as my main linux distro and to be honest it is very close to gentoo linux with regards to custom building what type of setup you are wanting, but without the pain of compiling.

You do lose some of the configuration options compared to gentoo, but to be honest not enough to be spending that time compiling!.

So I am using kde, with also having steam to play some games :), but the half life 2 game kept on crashing and upon ssh’ing onto the box there was the error within dmesg of

[ 2334.498295] INFO: task hl2_linux:4442 blocked for more than 120 seconds.
[ 2334.498298]       Not tainted 4.8.13-1-ARCH #1
[ 2334.498300] "echo 0 &gt; /proc/sys/kernel/hung_task_timeout_secs" disables this message.
[ 2334.498303] hl2_linux       D ffff8807cdeeba88     0  4442      1 0x20020006
[ 2334.498308]  ffff8807cdeeba88 00ff8807cdeebb50 ffff88081af15580 ffff8807c8169c80
[ 2334.498312]  0000000000000000 ffff8807cdeec000 ffff880817c1c580 ffff880817c1c548
[ 2334.498317]  ffff880817c18000 ffff8807641ef828 ffff8807cdeebaa0 ffffffff815f40ec
[ 2334.498321] Call Trace:
[ 2334.498324]  [<ffffffff815f40ec>] schedule+0x3c/0x90
[ 2334.498359]  [<ffffffffa0144ab8>] amd_sched_entity_fini+0x68/0x100 [amdgpu]
[ 2334.498364]  [<ffffffff810c0450>] ? wake_atomic_t_function+0x60/0x60
[ 2334.498396]  [<ffffffffa010c2dd>] amdgpu_ctx_fini+0xcd/0x110 [amdgpu]
[ 2334.498427]  [<ffffffffa010cb65>] amdgpu_ctx_mgr_fini+0x65/0xa0 [amdgpu]
[ 2334.498454]  [<ffffffffa00e520e>] amdgpu_driver_postclose_kms+0x3e/0xd0 [amdgpu]
[ 2334.498465]  [<ffffffffa0004703>] drm_release+0x203/0x380 [drm]
[ 2334.498469]  [<ffffffff8120b42f>] __fput+0x9f/0x1e0
[ 2334.498472]  [<ffffffff8120b5ae>] ____fput+0xe/0x10
[ 2334.498475]  [<ffffffff8109a0d0>] task_work_run+0x80/0xa0
[ 2334.498479]  [<ffffffff810806e2>] do_exit+0x2c2/0xb50
[ 2334.498483]  [<ffffffff810b4155>] ? put_prev_entity+0x35/0x8c0
[ 2334.498487]  [<ffffffff81080feb>] do_group_exit+0x3b/0xb0
[ 2334.498490]  [<ffffffff8108be08>] get_signal+0x268/0x640
[ 2334.498494]  [<ffffffff8102d0f7>] do_signal+0x37/0x6b0
[ 2334.498498]  [<ffffffff815f7241>] ? do_nanosleep+0x91/0xf0
[ 2334.498501]  [<ffffffff810ecdb0>] ? hrtimer_init+0x120/0x120
[ 2334.498504]  [<ffffffff815f720a>] ? do_nanosleep+0x5a/0xf0
[ 2334.498508]  [<ffffffff81003651>] exit_to_usermode_loop+0xa1/0xc0
[ 2334.498511]  [<ffffffff81003df7>] do_fast_syscall_32+0x157/0x170
[ 2334.498515]  [<ffffffff815f987b>] entry_SYSCALL_compat+0x3b/0x40

of which was to do with the AMDGPU-PRO graphics driver.

So with some digging around you are able to alter the video settings within half life 2, so if you goto “options->Video” the below will be shown

Then click on “advanced”, which will show

If you turn “Multicore Rendering” to disabled and also “Wait for vertical sync” to Enabled, this should stop half life 2 from crashing.

Happy gaming.!!!.

php 7 – generator – yield’s

php 7 has now implement generators where you are able to return (yield) results from a method and only the ones that you will need.

// define the output as type Generator -- e.g. yield results.. the compiler will still work without this Generator keyword
function yieldSomeNumbers() : Generator {  
    yield 10;
    yield 13;
foreach (yieldSomeNumbers() as $v) {

Will output


The ‘Generator’ at the end of the method ” : Generator” is not actual needed as the compiler will append it as such on the fly since the method is yield results.

For example, lets say that you are doing a search of numbers from 1-100 and are searching for the value of 10, so before generators the code would have been something like

function generateNumbers() {
    return range(1,100);   // load up a array of values 1-100 e.g. 1,2,3,4,5...
foreach (generateNumbers() as $v){
    if ($v == 10) {

The ‘foreach (generateNumbers()’ will be using the full array where as

function generateSomeNumbers() : Generator {
    foreach (range(1,100) as $v) {
        yield $v;
foreach (generateSomeNumbers() as $v){
    if ($v == 10) {

will only return each yield upon request.

php 7 – strict typing and return types

This is going to be the first of my PHP 7 posts where PHP 7 has grown up and has PHP 7 taken a massive leap forwards!!!

So the first thing is return types, before PHP 7 there was no return typing e.g.

function add($a,$b) {
    return $a + $b;
echo add("hi there","good");

was a “valid” PHP code!!

But now with not only parameter type hinting, you are also able to use return types with strict typing as well!. So to start with, this is defining the return type of int (just before the function body there is the :int)

function add(int $a,int $b): int {
    return (int)($a + $b);
echo add(2,3);

The answer of course will be 5, but you could still be silly and allow PHP to convert string characters (that was alphanumeric) to integers, for example calling the above function like

echo add("2","3");

Would still work and give the answer of 5!, which is kinder wrong!.

So there is the strict typing which enforces the correct parameter being used.

So if you change the above code to

function add(int $a,int $b): int {
    return (int)($a + $b);

The output would be

PHP Fatal error:  Uncaught TypeError: Argument 1 passed to add() must be of the type integer, string given, called in /home/genux/Programming/php7/scalar.php on line 12 and def
ined in /home/genux/Programming/php7/scalar.php:5
Stack trace:
#0 /home/genux/Programming/php7/scalar.php(12): add('2', '3')
#1 {main}
  thrown in /home/genux/Programming/php7/scalar.php on line 5

Which means that you have pass in the integer values, as in strict typing and not just assume PHP will be “cool” and just sort it out for you.

Await and async

I was working on an project that was using the async and await keywords and an college asked what/why use the await/ async keywords when you will still be waiting on the task / thread to finish. Which was an very good question, and the best answer that I could give (of which I am writing it here) is that if you want to start an thread and let it process it’s work whilst still carrying on with the main thread then you would need to write some thread controls etc. But .Net gives you that thread control with the async / await keywords.

The async is short for asynchronous which basically means do something whilst doing something else, e.g. you are able to talk and listen at the same time!.

So what happens is that you can create an method to do some long processing, I am just using an Task delay here to simulate that with also using the await syntax just before it so that it will actually wait for the task delay to finish, as below

 public async Task<Boolean> getCheckAsync(Boolean c)
            System.Console.WriteLine("getCheckAsync about to wait");
            await Task.Delay(1000);
            System.Console.WriteLine("getCheckAsync finished waiting");
            // do some processing.. pulling data from a database / website etc.
            if (c)
                return false;
                return true;

You have to decorate the method call with async Task, since then you will be able to do some await on this method.

The next part is to create an call to this method, and this will start the call as well — start processing the method of getCheckAsync

 Task<Boolean> getCheckAsyncTask = this.getCheckAsync(t);            // calls the process to start processing -- e.g. will output "getCheckAsync about to wait"

And to await for the result, which means that you are able to do some other actions in between the calling the method (getCheckAsync) and waiting on the result.

Boolean res = await getCheckAsyncTask;

So from the full code example below, the first output would be

Calling the async
Waiting for result from getCheckAsync
getCheckAsync about to wait
Bit inbetween the getCheckAsync call

which as you can see has already called the getCheckAsync method and then carried on processing the bit inbetween that is not reliant on the result of the method call (getCheckAsync), but once we are ready for the result of the getCheckAsync method call, the output would carry on with

getCheckAsync finished waiting
Obtainted the result from getCheckAsync
Passed the 'Calling the async'
B = False

Here is the code in full

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace asynctest
    class Program
        static void Main(string[] args)
            Program p = new Program();
            System.Console.WriteLine("Calling the async ");
            Boolean b = p.getCheck(true).Result; 
            System.Console.WriteLine("Passed the 'Calling the async'");
            System.Console.WriteLine("B = "+b);
        public async Task<Boolean> getCheck(Boolean t)
            System.Console.WriteLine("Waiting for result from getCheckAsync ");
            Task<Boolean> getCheckAsyncTask = this.getCheckAsync(t);            // calls the process to start processing -- e.g. will output "getCheckAsync about to wait"
            System.Console.WriteLine("Bit inbetween the getCheckAsync call");   // do some extra processing.
            Boolean res = await getCheckAsyncTask;                              // but now we have to "await" for the result.
            System.Console.WriteLine("Obtainted the result from getCheckAsync ");
            return res;
        public async Task<Boolean> getCheckAsync(Boolean c)
            System.Console.WriteLine("getCheckAsync about to wait");
            await Task.Delay(1000);
            System.Console.WriteLine("getCheckAsync finished waiting");
            // do some processing.. pulling data from a database / website etc.
            if (c)
                return false;
                return true;

vectors and for_each

A friend of mine asked how to use the newer for_each and is it similar to the foreach in php, so here is the example that I gave and thus doing it online as well.

To start with the foreach (php version here) does loop over data and so does the for_each in c++, but the main difference is that the for_each in c++ is basically a for loop

for (; first!=last;++first)

where the functionCall is the parameter passed in to do something with that part of the data.

The great thing with c++ is that you are able to use either a method, structure etc as long as it is able to output a variable then we are good.

So here is a example, please note that I am loading up a vector e.g. only functions that have a single int make sense.

void printNumber (int i) {
	cout << "Print the number from a function : " << i << "\n";
for_each(v.begin(), v.end(), printNumber);

here we are using the for_each (where the v is a vector) passing in the start and end of the vector with a function as the last parameter.

and here

// create a object that can use the operator() to output value passed in.
struct structToOutput {
	void operator() (int i) {
		cout << "Struct to output : " << i << "\n";
} structToOutputObject;
for_each(v.begin(), v.end(), structToOutputObject);

will output the values differently but in essence still able to access the values.

Here is the code in full

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void printNumber (int i) {
	cout << "Print the number from a function : " << i << "\n";
// create a object that can use the operator() to output value passed in.
struct structToOutput {
	void operator() (int i) {
		cout << "Struct to output : " << i << "\n";
} structToOutputObject;
int main(int argc, char* argv[])
	// lets load up some dummy data.
	vector<int> v;
	for (int i =0; i< 10; i++)
	// run through the vector using a standard function with parameter
	for_each(v.begin(), v.end(), printNumber);
	// output using a operator() method
	for_each(v.begin(), v.end(), structToOutputObject);
	return 0;

with the output being

Print the number from a function : 0
Print the number from a function : 1
Print the number from a function : 2
Print the number from a function : 3
Print the number from a function : 4
Print the number from a function : 5
Print the number from a function : 6
Print the number from a function : 7
Print the number from a function : 8
Print the number from a function : 9
Struct to output : 0
Struct to output : 1
Struct to output : 2
Struct to output : 3
Struct to output : 4
Struct to output : 5
Struct to output : 6
Struct to output : 7
Struct to output : 8
Struct to output : 9

How to compile with g++

g++ <filename> -o <outfilename> -std=c++11

auto and lambda

The C++ 11 standards are starting to take more and more cue’s from other languages where you are able to create functions on the fly (lambda). With also adding the “auto” keyword which will allow the program to automatically figure out what type of variable will be returned from lambda function.

So here the break up of the lambda function is

<return type> <[optional local variables to be used]>(optional parameters);

The optional local variables are abit like the “use” within PHP (use)

So the full code is

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(int argc, char* argv[])
	int num = 30;
	int anothernum = 0;
	auto func = [&anothernum](int i) {
			cout << "Parameter passed in :" << i << "\n";
			anothernum = anothernum + 10; // alter the value passed in.
			return 50;	// return a value and use the auto keyword.
	int ret = func(num);
	cout << "Return from func :" << ret << "\n";
	cout << "Another num :" << anothernum << "\n";
	return 0;

and the output would be

Parameter passed in : 30
Return from func : 50
Another num : 10;

To compile with g++ I used these parameters

g++ <filename> -o <outfilename> -std=c++11