Skip to main content

Shenzhen, China

Phalcon Framework with Docker

Based on Working with Phalcon Framework and Docker by Rogério Silva.

Github Repository

Scaffold your Application

Link your SQL database to your Phalcon App container using Docker-compose:

version: '3'

services:
  app:
    container_name: phalcon_app
    build: .
    working_dir: /var/www/html
    volumes:
      - ./html:/var/www/html
    ports:
      - '8080:80'
    expose:
      - '8080'
    depends_on:
      - mysql
    links:
      - mysql

  mysql:
    container_name: phalcon_db
    image: mariadb:latest
    environment:
      MYSQL_DATABASE: phalcon_app
      MYSQL_ROOT_PASSWORD: root
      SERVICE_TAGS: dev
      SERVICE_NAME: mysql
    ports:
      - '3306:3306'

Build the Phalcon Container

Using the PHP/NGINX container by webdevops as base image:

FROM webdevops/php-nginx:7.4

ARG PSR_VERSION=1.2.0
ARG PHALCON_VERSION=4.1.3
ARG PHALCON_EXT_PATH=php7/64bits

RUN set -xe && \
    # Download PSR, see https://github.com/jbboehr/php-psr
    curl -LO https://github.com/jbboehr/php-psr/archive/v${PSR_VERSION}.tar.gz && \
    tar xzf ${PWD}/v${PSR_VERSION}.tar.gz && \
    # Download Phalcon
    curl -LO https://github.com/phalcon/cphalcon/archive/v${PHALCON_VERSION}.tar.gz && \
    tar xzf ${PWD}/v${PHALCON_VERSION}.tar.gz && \
    docker-php-ext-install -j $(getconf _NPROCESSORS_ONLN) \
        ${PWD}/php-psr-${PSR_VERSION} \
        ${PWD}/cphalcon-${PHALCON_VERSION}/build/${PHALCON_EXT_PATH} \
    && \
    # Remove all temp files
    rm -r \
        ${PWD}/v${PSR_VERSION}.tar.gz \
        ${PWD}/php-psr-${PSR_VERSION} \
        ${PWD}/v${PHALCON_VERSION}.tar.gz \
        ${PWD}/cphalcon-${PHALCON_VERSION} \
    && \
    php -m

ENV WEB_DOCUMENT_ROOT=/var/www/html/application/public

I am using the currently latest version of php-psr in combination with the last cphalcon v4 release. I was not yet able to get version 5 to work.

Build

docker pull mariadb:latest
docker pull webdevops/php-nginx:7.4
mkdir html
docker-compose build app
docker images

docker-phalcon_app                        latest          08a2db66d313   6 seconds ago   1.03GB
webdevops/php-nginx                       7.4             bf283e850d32   3 days ago      1.02GB
mariadb                                   latest          8dafde72c039   4 days ago      383MB

Run the Service

docker-compose up -d

Creating network "docker-phalcon_default" with the default driver
Creating phalcon_db ... done
Creating phalcon_app ... done

Configure the Phalcon App

Initialize the application by generating a composer.json:

docker exec -ti phalcon_app composer init

This command will guide you through creating your composer.json config.

Package name (<vendor>/<name>) [root/html]: phalcon/app
Description []: Phalcon App
Author [n to skip]: Mike Polinowski
Minimum Stability []: 
Package Type (e.g. library, project, metapackage, composer-plugin) []: 
License []: MIT

Define your dependencies.

Would you like to define your dependencies (require) interactively [yes]? no
Would you like to define your dev dependencies (require-dev) interactively [yes]? no
Add PSR-4 autoload mapping? Maps namespace "Phalcon\App" to the entered relative path. [src/, n to skip]: n

{
    "name": "phalcon/app",
    "description": "Phalcon App",
    "license": "MIT",
    "authors": [
        {
            "name": "Mike Polinowski"
        }
    ],
    "require": {}
}

Do you confirm generation [yes]? yes

Create a default Phalcon project using the Phalcon Devtools:

docker exec -ti phalcon_app composer require --dev phalcon/devtools

Using version ^4.2 for phalcon/devtools
./composer.json has been updated
Running composer update phalcon/devtools
Loading composer repositories with package information
Updating dependencies
Lock file operations: 22 installs, 0 updates, 0 removals
docker exec -ti phalcon_app ./vendor/bin/phalcon project application simple

Phalcon DevTools (4.2.0)
  Success: Controller "index" was successfully created.
  Info: /var/www/html/application/app/controllers/IndexController.php
  Success: Project 'application' was successfully created. 
  Info: Please choose a password and username to use Database connection.
  Info: Used default: 'root' without password.

Phalcon Framework with Docker