2017 © Pedro Peláez
 

library docker-links

A helper for parsing Docker link environment variables.

image

texthtml/docker-links

A helper for parsing Docker link environment variables.

  • Wednesday, April 5, 2017
  • by mathroc
  • Repository
  • 2 Watchers
  • 1 Stars
  • 4,476 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 3 Forks
  • 0 Open issues
  • 9 Versions
  • 1 % Grown

The README.md

Docker Links Environment Parser

Build Status Code Status Latest Version License, (*1)

Docker has a feature where you can link containers together by name. For example, you start a redis-server in a docker container and expose the default redis port 6379:, (*2)

$ docker run -p 6379 -d -name redis vagrant/redis-server

You then start another containiner running a php-fpm web service that needs to access this redis server:, (*3)

$ docker run --link redis:db -d php:fpm

Docker will internally hook up these the two containers and pass host and port information to the php-fpm web service via environment variables:, (*4)

DB_NAME=/romantic_lumiere/db
DB_PORT=tcp://172.17.0.5:6379
DB_PORT_6379_TCP=tcp://172.17.0.5:6379
DB_PORT_6379_TCP_ADDR=172.17.0.5
DB_PORT_6379_TCP_PORT=6379
DB_PORT_6379_TCP_PROTO=tcp

This library provides a helper parseLinks that will parse these environment variables into easily navigable PHP objects., (*5)

Install

Install docker-links via composer, (*6)

$ composer require texthtml/docker-links

Example Usage

Consider a container that accesses three external services on two other containers. The first container exposes redis on port 6379 and postgres on 6500. The second container exposes redis on port 6379., (*7)

DB_NAME=/romantic_lumiere/db
DB_PORT=tcp://172.17.0.5:6379
DB_PORT_6379_TCP=tcp://172.17.0.5:6379
DB_PORT_6379_TCP_ADDR=172.17.0.5
DB_PORT_6379_TCP_PORT=6379
DB_PORT_6379_TCP_PROTO=tcp
DB_PORT_6500_TCP=tcp://172.17.0.5:6500
DB_PORT_6500_TCP_ADDR=172.17.0.5
DB_PORT_6500_TCP_PORT=6500
DB_PORT_6500_TCP_PROTO=tcp
DB_REDIS_NAME=/romantic_lumiere/db_redis
DB_REDIS_PORT=tcp://172.17.0.2:6379
DB_REDIS_PORT_6379_TCP=tcp://172.17.0.2:6379
DB_REDIS_PORT_6379_TCP_ADDR=172.17.0.2
DB_REDIS_PORT_6379_TCP_PORT=6379
DB_REDIS_PORT_6379_TCP_PROTO=tcp

Parse with docker-links:, (*8)

$links = \TH\Docker\Links::buildFrom($_ENV);

// $links can be used as an array
echo count($links), PHP_EOL; // 2
foreach ($links as $name => $link) {
    echo $name, PHP_EOL; // /romantic_lumiere/db, /romantic_lumiere/db
}

// each link is an instanceof [Link](test)
$link = $links['/romantic_lumiere/db'];

echo $link->mainPort()->address(), PHP_EOL; // 172.17.0.5
echo $link->mainPort()->protocol(), PHP_EOL; // TCP
echo $link->mainPort()->number(), PHP_EOL; // 6379

echo $link->env()['USERNAME'], PHP_EOL; // username
echo $link->env('PASSWORD'), PHP_EOL; // password
echo $link->env('SOMETHING_ELSE'), PHP_EOL; // NULL
echo $link->env('SOMETHING', 'default value'), PHP_EOL; // default value

The Versions

05/04 2017

dev-master

9999999-dev

A helper for parsing Docker link environment variables.

  Sources   Download

AGPL-3.0

The Development Requires

18/10 2016

v1.5.0

1.5.0.0

A helper for parsing Docker link environment variables.

  Sources   Download

AGPL-3.0

The Development Requires

17/10 2016

dev-feature/perf

dev-feature/perf

A helper for parsing Docker link environment variables.

  Sources   Download

AGPL-3.0

The Development Requires

03/11 2015

v1.4.0

1.4.0.0

A helper for parsing Docker link environment variables.

  Sources   Download

AGPL-3.0

The Development Requires

15/07 2015

v1.3.0

1.3.0.0

A helper for parsing Docker link environment variables.

  Sources   Download

AGPL-3.0

The Development Requires

13/07 2015

v1.2.0

1.2.0.0

A helper for parsing Docker link environment variables.

  Sources   Download

AGPL-3.0

The Development Requires

18/06 2015

v1.1.0

1.1.0.0

A helper for parsing Docker link environment variables.

  Sources   Download

AGPL-3.0

The Development Requires

02/06 2015

v1.0.1

1.0.1.0

A helper for parsing Docker link environment variables.

  Sources   Download

AGPL-3.0

The Development Requires

02/06 2015

v1.0.0

1.0.0.0

A helper for parsing Docker link environment variables.

  Sources   Download

AGPL-3.0

The Development Requires