#TIL : Changing channel from alpha to stable will remove ALL DATA

On MacOS, changing Docker channel will remove all data (includes volumes, images, networks and … everything).

Because Docker on Mac using a minimal Linux machine to host docker engine, so changing machine means discarding all old data. So BECAREFUL !

#TIL : Reducing docker image the right way

When building an image, Docker engine commit file system layer on every command (RUN, ADD, COPY). So next time you installing packages from package manager likes apt, yum, pacman, …remember clean their cache in same line.

BAD WAY

1
2
3
4
5
6
7
RUN apt-get update
RUN apt-get install git

# Something here

# End of file
RUN apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

RIGHT WAY

1
RUN apt-get update && apt-get install -y git zip unzip && apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

Kick start Docker environment on ubuntu

What does script do :

  • Install latest Docker Engine
  • Install latest Docker Compose tool
  • AND last but not least, a congratuation message !

Here is the script :

My docker containers are building

Hm.. it’s exactly docker images are building instead


Ref:

Backup and Restore ElasticSearch databases using Docker

Introduction

One day, your ElasticSearch data is large and you have to move all data to another nodes or you just want backup the data file to somewhere.

ElasticSearch-Dump is a tool can help you on this situation, it’s written on NodeJS so can run multi-platform. You can view the documents here.

One way more convinient if you already have Docker is running its Docker image.

Instruction

Prepare a Docker image

You can build manually its Docker image by

1
2
3
$ git clone https://github.com/taskrabbit/elasticsearch-dump
$ cd elasticsearch-dump
$ docker build -t elasticdump .

or pull image that was built on [Docker Hub] (it’s only 14 MB because of building on top Alpine) (https://hub.docker.com/r/khanhicetea/elasticsearch-dump/)

1
$ docker pull khanhicetea/elasticsearch-dump

In next chapters, you should replace khanhicetea/elasticsearch-dump instead of elasticdump if you pulled image from Docker Hub.

Backup and restore the data using dump files

Saving dump files (must map a docker volume)

1
2
3
4
5
6
7
8
$ docker run --rm -ti -v `pwd`:/data elasticdump \
--input=http://production.es.com:9200/my_index \
--output=/data/my_index.mapping.json \
--type=mapping
$ docker run --rm -ti -v `pwd`:/data elasticdump \
--input=http://production.es.com:9200/my_index \
--output=/data/my_index.data.json \
--type=data

Restore from dump files (must map a docker volume)

1
2
3
4
5
6
7
8
$ docker run --rm -ti -v `pwd`:/data elasticdump \
--input=/data/my_index.mapping.json \
--output=http://newnodes.es.com:9200/my_index \
--type=mapping
$ docker run --rm -ti -v `pwd`:/data elasticdump \
--input=/data/my_index.data.json \
--output=http://newnodes.es.com:9200/my_index \
--type=data

Transfer data directly to another instance

1
2
3
4
5
6
7
8
$ docker run --rm -ti elasticdump \
--input=http://production.es.com:9200/my_index \
--output=http://newnodes.es.com:9200/my_index \
--type=mapping
$ docker run --rm -ti elasticdump \
--input=http://production.es.com:9200/my_index \
--output=http://newnodes.es.com:9200/my_index \
--type=data

Note : you must replace localhost or 127.0.0.1 to 172.17.0.1 to access to ES Docker host. 172.17.0.1 is default IP of docker host in docker0 network interface

Minimize downtime while transfering data

If you transfering data from Index A to Index B in same instance, you can try Index Alias to minimize the downtime.

  • Step 1 : Make a index alias to old index.
  • Step 2 : Point your application to use the created index alias.
  • Step 3 : Transfering mapping, data (you can serialize the data before restore)
    • You should use option --bulk=true for using Bulk API when writing documents
  • Step 4 : Move index alias from old index to new index (delete old link and make a new link)

PHP 5.6 vs PHP 7RC8 - Benchmarking using Docker

New version of PHP, PHP7 has been released yesterday after 11 years of PHP5. I am very excited with it, so I made a benchmark to compare the performance of 2 versions.

The fastest way to test out PHP multi-versions is using Docker image. You can get it from : https://hub.docker.com/_/php/

Let’s start pull PHP images

1
2
docker pull php:5.6
docker pull php:7

Change directory to the web root folder of PHP project (I used my micro-framework, Sifoni to test). And add this file server.php to web folder with content

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?php

error_reporting(0);

$filename = __DIR__.preg_replace('#(\?.*)$#', '', $_SERVER['REQUEST_URI']);
if (php_sapi_name() === 'cli-server' && is_file($filename)) {
return false;
}

define('ROOT_PATH', dirname(dirname(__FILE__)));

$autoloader = require_once (ROOT_PATH . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php');

\Sifoni\Engine::getInstance()->init()->bootstrap(array(
'path.root' => ROOT_PATH,
'autoloader' => $autoloader,
))->start()->run();

Next, run this command to run with PHP 5.6

1
docker run -it -p 8080:8080 -v `pwd`:/code php:5.6 php -S 0.0.0.0:8080 -t /code/web /code/web/server.php

Open http://localhost:8080/ to see the memory and processing time of PHP5.6.

Continue with PHP7, exit the PHP 5.6 session and run this command

1
docker run -it -p 8080:8080 -v `pwd`:/code php:7 php -S 0.0.0.0:8080 -t /code/web /code/web/server.php

Open http://localhost:8080/ to see the memory and processing time PHP7.

This is my result :

PHP 5.6 benchmarking result

PHP PHP 7 RC8 benchmarking result

Amazing… PHP 7 is winner in Saving Memory and Processesing Time (about 1.4x times than PHP 5.6).

At the moment I tested, official docker image of PHP is PHP7RC8. Hope image maintainer updates it soon :)

#TIL : Run built-in server via Docker

Docker is the fast and clean way to run Linux programs.

We can run a PHP project via PHP built-in server and Docker.

1
docker run -it -p 8080:8080 -v `pwd`:/code php:7 php -S 0.0.0.0:8080 -t /code/web /code/web/server.php

With server.php content is

1
2
3
4
5
6
7
8
9
10
11
<?php

$filename = __DIR__.preg_replace('#(\?.*)$#', '', $_SERVER['REQUEST_URI']);
if (php_sapi_name() === 'cli-server' && is_file($filename)) {
return false;

}

// Run application below
$app = new Application();
$app->run();