PhpZone Docker

Getting Started

Requirements

PhpZone requires PHP 5.3 or higher.

Installation

Installation is provided via Composer, if you don’t have it, do install:

$ curl -s https://getcomposer.org/installer | php

then PhpZone Docker Compose can be added into your dependencies by:

$ composer require --dev phpzone/docker 0.2.*

or add it manually into your composer.json:

{
    "required-dev": {
        "phpzone/docker": "0.2.*"
    }
}

Configuration file

If the configuration file doesn’t exist yet, you can find more information in PhpZone - Getting Started

Registration of the extension

Registration is provided by a simple definition of full name of the class (namespace included):

extensions:
    PhpZone\Docker\DockerCompose: ~

Note

This extension is a command builder with definitions within its values. This means that only the registration without any values would have no effect.

Creating of commands

As mentioned in the PhpZone documentation, each extension gets its configuration via values which are defined during its registration. PhpZone Docker Compose expects to get an array of required commands. Each command is defined with it’s name as the key and the Docker command to be run as the value:

extensions:
    PhpZone\Docker\DockerCompose:
        command_name:
            command: up

Now, when we would run:

$ vendor/bin/phpzone command_name

the command docker-compose up would be run.

Definitions For Command

Commands can contain following definitions:

Name Type Default Required
build array   No
command string up No
description string null No
enable boolean true No
file string docker-compose.yml No
help string null No
name string current directory name No
parent string null No
rm array   No
scale array   No
up array   No
verbose boolean false No

Example with default values:

extensions:
    PhpZone\Docker\DockerCompose:
        command_name:
            build:
                no_cache: false
            command: up
            description: ~
            enable: true
            file: ~
            help: ~
            name: ~  # current directory name
            parent: ~
            rm:
                force: false
            scale: ~
            up:
                daemon:      false
                no_recreate: false
                no_build:    false
            verbose: false

Note

The order of the definitions is not important.

Note

Not required definitions don’t need to be set.

build

Type Default Required
array   No

Extended options in case of the definition command: build.

Example with default values:

extensions:
    PhpZone\Docker\DockerCompose:
        command_name:
            build:
                no_cache: false

no_cache

Type Default Required
boolean false No

Do not use cache when building the image.

command

Type Default Required
string up No

Specifies which command will be run against services. Available commands are: build, kill, logs, ps, pull, rm, scale, start, stop and up. For more information read official documentation of Docker Compose.

description

Type Default Required
string null No

The description of a command will be displayed when a developer would run the command list or without any command.

enable

Type Default Required
boolean true No

All defined commands are enabled by default. Sometimes can be useful to disable a command without its removal.

file

Type Default Required
string docker-compose.yml No

Specifies an alternate Compose yaml file. Official documentation of Docker Compose

help

Type Default Required
string null No

The help of a command will be displayed when a developer would run the command help.

name

Type Default Required  
string current directory name No

Specifies an alternate project name. Official documentation of Docker Compose

parent

Type Default Required
string null No

It can help you to define more commands related to the same definitions, so it can help to avoid duplications. The value is defined as parent: command_name.

Example:

extensions:
    PhpZone\Docker\DockerCompose:
        command_name_1:
            command: up
            name:    myproject
        command_name_2:
            command: stop
            parent:  command_name_1

If you run:

$ vendor/bin/phpzone comand_name_2

This will compose docker-compose -p myproject stop and execute it.

rm

Type Default Required
array   No

Extended options in case of the definition command: rm.

Example with default values:

extensions:
    PhpZone\Docker\DockerCompose:
        command_name:
            rm:
                force: false

force

Type Default Required
boolean false No

Don’t ask to confirm removal.

scale

Type Default Required
array   No

Extended options in case of the definition command: scale. Numbers are specified in the form service_name: integer.

Example:

extensions:
    PhpZone\Docker\DockerCompose:
        command_name:
            scale:
                service_name_1: 3
                service_name_2: 2

up

Type Default Required
array   No

Extended options in case of the definition command: up.

Example with default values:

extensions:
    PhpZone\Docker\DockerCompose:
        command_name:
            up:
                daemon:      false
                no_recreate: false
                no_build:    false

daemon

Type Default Required
boolean false No

Detached mode: Run containers in the background, print new container names.

no_recreate

Type Default Required
boolean false No

If containers already exist, don’t recreate them.

no_build

Type Default Required
boolean false No

Don’t build an image, even if it’s missing.

verbose

Type Default Required
boolean false No

Show more output. Official documentation of Docker Compose

Options For Command

All built commands have their definitions of how they are executed, but sometimes it’s useful to have an option to rewrite a defined parameter or extend a functionality. This is provided by options below.

Options are extended attributes which can be set either before command name or after command name, so both following examples are valid:

$ vendor/bin/phpzone <OPTION> <COMMAND>
$ vendor/bin/phpzone <COMMAND> <OPTION>

Tip

All available options can be displayed by:

$ vendor/bin/phpzone <COMMAND> --help

–build

Overwrites defined command by build. Command definition

–kill

Overwrites defined command by kill. Command definition

–logs

Overwrites defined command by logs. Command definition

–ps

Overwrites defined command by ps. Command definition

–pull

Overwrites defined command by pull. Command definition

–rm

Overwrites defined command by rm. Command definition

–scale

Overwrites defined command by scale. Command definition

–start

Overwrites defined command by start. Command definition

–stop

Overwrites defined command by stop. Command definition

–up

Overwrites defined command by up. Command definition

A Docker command builder configured by YAML, based on PhpZone. Its primary purpose is to provide a simple way to define commands for running Docker containers/instances which could be used in daily workflow of every developer. With this plugin, you no longer need to have advanced knowledge about Docker, as you can add store simple commands to your configuration. This is also useful for advanced users who want to store ready-made commands.

Attention

This tool is only a configurator and executor of Docker commands, applications Docker and `Docker Compose`_ are not included.

Basic Usage

An example speaks a hundred words so let’s go through one.

The configuration file below is used for the development of this extension:

Create a phpzone.yml file in the root of a project:

extensions:
    PhpZone\Docker\DockerCompose: # register an extension with a configuration
        db:
            description: Run DB which can be used for running tests
            name: myproject
            file: docker-compose.yml
            command: up

and run:

$ vendor/bin/phpzone db

This will compose a proper Docker Compose command docker-compose -f docker-compose.yml -p myproject up and execute it.