Containers and continuous integration for PHP developers. Part 1. Introduction to containers.

Containers and continuous integration for PHP developers. Part 1. Introduction to containers.

Quick introduction to containers using Gitlab CI runner.
Vladimir Roudakov

Table of content.

 

Definitions.

Modern project lean towards automation leaving old manual processes behind. The ability to release software codebase as often as possible called Continuous Delivery (CD) ;whereas the ability to run all the processes (including tests, quality checks, builds) automatically prior to the release called Continuous Integration (CI).

As PHP developer I want to run specific code in specific environment (or on specific operation system). In order to run specific code on specific environment developer requires a container: specific packaged environment (i.e. Ubuntu or Alpine) with required executables (i.e. PHP, composer). For more information read "What Is Container?" from Docker website.

Large number of providers created created official images containing their products, operating systems and environments are are distributing them via Docker Hub: online repository of containers. Here's few containers I will be using in this tutorial series: 

Specific tags can be used in order to target specific environment. For example, PHP container tags lists 7.2.19-stretch or 7.2.19-alpine for specific operating system and version 7.2 of PHP. Tags 7.2.19 or 7.2 can be used if developer does not really care which operating system php is running on. Use latest tag to specify the latest version of the container.

Container runners.

Runner is required to execute a container.  Docker is a popular choice to execute on local containers machine, although Gitlab and Gitlab CI (runner) will be used for this tutorial. Registration is free and unless you are using code management solution such as GitHub or Bitbucket, you would need one to store your code.

Gitlab CI is our runner of choice and includes number of free minutes (2000 minutes per month at the moment of writing) to run anything you want using custom containers or containers from Docker Hub.

After registering and creating the first repository, you can commit .gitlab-ci.yml file (or you can fork repository here) to tell Gitlab runner what to execute.

Below, there are 3 example of simple commands executed inside containers downloaded from Docker Hub.

Example 1.

Display php version and information.

  • Run php container
  • Run php -i
chapter01-php-info: image: php:7.3 script: - php -i

And the result as follows (truncated for better readability):

Using Docker executor with image php:7.3 ... Pulling docker image php:7.3 ... ... $ php -i phpinfo() PHP Version => 7.3.6 ...

Complete output is available here.

Example 2.

Run list of composer commands preliminary checking version of php and composer.

  • Run php container
  • Run php -i
chapter01-composer: image: composer:latest before_script: - php -v - composer --version script: - composer list
Using Docker executor with image composer:latest ... Pulling docker image composer:latest ... ... $ php -v PHP 7.3.6 (cli) (built: May 31 2019 23:48:53) ( NTS ) ... $ composer --version Composer version 1.8.5 2019-04-09 17:46:47 $ composer list Composer version 1.8.5 2019-04-09 17:46:47 ... Available commands: about Shows the short information about Composer. archive Creates an archive of this composer package. ...

Complete output is available here.

Example 3.

Install and execute curl command on specific linux version.

  • Run ubuntu
  • Install curl
  • Curl google.com
chapter01-ubuntu: image: ubuntu:latest before_script: - apt-get update - apt-get install curl -y script: - curl google.com

The output of the commands above (truncated for better readability):

Using Docker executor with image ubuntu:latest ... Pulling docker image ubuntu:latest ... ... $ apt-get update ... $ apt-get install curl -y ... 0 upgraded, 30 newly installed, 0 to remove and 4 not upgraded. Need to get 4581 kB of archives. After this operation, 14.1 MB of additional disk space will be used. ... done. $ curl google.com ... <HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8"> <TITLE>301 Moved</TITLE></HEAD><BODY> <H1>301 Moved</H1> The document has moved <A HREF="http://www.google.com/">here</A>. </BODY></HTML> ...

Complete output is available here.

Links