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:1.9.3 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:

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 Docker image dashboard.

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.

Codefresh YAML for pipeline definitions
Steps in pipelines
Creating pipelines
How Codefresh pipelines work