Create a Docker image for Php

Using Codefresh pipelines

Codefresh can work with Php projects using any of the popular frameworks (Laravel, Symphony, CakePHp etc.)

The example php project

You can see the example project at https://github.com/codefresh-contrib/php-composer-sample-app. The repository contains a simple Php project that uses composer as a package manager.

The dockerfile uses multi-stage builds to minimize the size of the docker image.

Dockerfile

FROM composer/composer:php7 as vendor

WORKDIR /tmp/

COPY composer.json composer.json
COPY composer.lock composer.lock

RUN composer install \
    --ignore-platform-reqs \
    --no-interaction \
    --no-plugins \
    --no-scripts \
    --prefer-dist

FROM php:7.2-apache-stretch

COPY . /var/www/html
COPY --from=vendor /tmp/vendor/ /var/www/html/vendor/

Create a Docker image for Php project

An example pipeline is also offered in the git repository. It contains just two steps. A clone step to fetch the code and a build step to create a Docker image.

codefresh.yml

version: '1.0'
steps:
  main_clone:
    title: Cloning main repository...
    type: git-clone
    repo: 'codefresh-contrib/php-composer-sample-app'
    revision: master
    git: github
  MyAppDockerImage:
    title: Building Docker Image
    type: build
    image_name: my-php-image
    working_directory: ./
    tag: master
    dockerfile: Dockerfile

Once you run this pipeline Codefresh will create a Docker image for the Php application:

Creating a docker image for php

Creating a docker image for php

Notice that all dependencies are downloaded when the dockerfile is created.

Launch Docker images

Codefresh can also launch Docker images (using Docker swarm behind the scenes). With each Codefresh account you get access to a limited number of Docker environments that can host any Docker image or Docker compose file.

First find your images in the internal Codefresh registry.

Launching a Docker image

Launching a Docker image

Click on the launch button and a new pipeline will run for deployment:

Getting the environment url

Getting the environment url

Notice that the pipeline logs show the dynamic URL of the application. Simply visit it with your browser and you will see the result.

Application preview

Application preview

Notice that these environments are only for testing and previewing your application as it is developed. They are NOT for production purposes.