Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better docker support #92

Open
wants to merge 4 commits into
base: 2.5.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# All is ignored by default
*

# Whitelist files and directories
!composer.json
!COPYRIGHT.md
!LICENSE.md
!README.md
!bin/
!config/
!data/
!module/
!public/
54 changes: 48 additions & 6 deletions .github/workflows/docker-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,56 @@ on:
types: [opened, reopened]

jobs:
build:
lint:
name: Lint Dockerfile
runs-on: ubuntu-latest

steps:
- name: "Checkout"
uses: "actions/checkout@v4"
- name: Checkout
uses: actions/checkout@v4

- name: Build and run docker compose file
- name: Lint Dockerfile
uses: hadolint/[email protected]
with:
recursive: true

build_and_test:
name: Build and Test Docker Images
runs-on: ubuntu-latest

strategy:
matrix:
target:
- { name: dev, port: 8080 }
- { name: prod, port: 8081 }

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Build image for ${{ matrix.target.name }}
run: docker buildx build --target ${{ matrix.target.name }} -t laminas:${{ matrix.target.name }} .

- name: PHPUnit for dev only
if: matrix.target.name == 'dev'
run: docker run --rm laminas:${{ matrix.target.name }} vendor/bin/phpunit module/*/test

- name: Start container for ${{ matrix.target.name }} and check HTTP status
run: |
docker run --rm -d -p ${{ matrix.target.port }}:80 --name laminas-test-${{ matrix.target.name }} laminas:${{ matrix.target.name }}
http_status_code=$(curl -s -o /dev/null -w "%{http_code}" http://localhost:${{ matrix.target.port }})
response_body=$(curl -s http://localhost:${{ matrix.target.port }})
if [ "$http_status_code" -eq 200 ] && [[ "$response_body" =~ "Welcome" ]]; then
echo "HTTP status code: $http_status_code"
echo "Response body: $response_body"
else
echo "Service did not start successfully."
echo "HTTP status code: $http_status_code"
echo "Response body: $response_body"
exit 1
fi

- name: Stop and remove container for ${{ matrix.target.name }}
run: |
docker compose up -d --build
docker compose run laminas echo "ok"
docker ps
docker stop laminas-test-${{ matrix.target.name }}
119 changes: 62 additions & 57 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,78 +1,83 @@
FROM php:8.3-apache
ARG PHP_VERSION=8.3
ARG COMPOSER_VERSION="latest"

LABEL maintainer="getlaminas.org" \
org.label-schema.docker.dockerfile="/Dockerfile" \
org.label-schema.name="Laminas MVC Skeleton" \
org.label-schema.url="https://docs.getlaminas.org/mvc/" \
org.label-schema.vcs-url="https://github.com/laminas/laminas-mvc-skeleton"
# Use a Composer image to install dependencies
# Composer should not be installed in the final image
FROM composer:${COMPOSER_VERSION} AS composer

# ---------- Base image ----------
FROM php:${PHP_VERSION}-apache AS base

WORKDIR /var/www

COPY --link --from=composer /usr/bin/composer /usr/bin/composer

## Utility to install PHP extensions
ADD --chmod=0755 https://github.com/mlocati/docker-php-extension-installer/releases/latest/download/install-php-extensions /usr/local/bin/

## Update package information
RUN apt-get update
RUN apt-get update && apt-get upgrade -y --no-install-recommends \
&& rm -rf /var/lib/apt/lists/*

RUN set -eux; \
install-php-extensions \
apcu \
intl \
opcache \
zip \
## Add here all extensions you need
# memcached \
# mongodb \
# redis \
# mbstring \
# pdo_mysql \
# pdo_pgsql \
;

## Configure Apache
RUN a2enmod rewrite \
&& sed -i 's!/var/www/html!/var/www/public!g' /etc/apache2/sites-available/000-default.conf \
&& mv /var/www/html /var/www/public

## Install Composer
RUN curl -sS https://getcomposer.org/installer \
| php -- --install-dir=/usr/local/bin --filename=composer
COPY --chown=www-data:www-data . .

###
## PHP Extensisons
###
## --- Development image ---
FROM base AS dev

## Install zip libraries and extension
RUN apt-get install --yes git zlib1g-dev libzip-dev \
&& docker-php-ext-install zip
VOLUME /var/www

## Install intl library and extension
RUN apt-get install --yes libicu-dev \
&& docker-php-ext-configure intl \
&& docker-php-ext-install intl

###
## Optional PHP extensions
###

## mbstring for i18n string support
# RUN docker-php-ext-install mbstring
WORKDIR /var/www

###
## Some laminas/laminas-db supported PDO extensions
###
ENV APP_ENV=development

## MySQL PDO support
# RUN docker-php-ext-install pdo_mysql
RUN mv "$PHP_INI_DIR/php.ini-development" "$PHP_INI_DIR/php.ini"

## PostgreSQL PDO support
# RUN apt-get install --yes libpq-dev \
# && docker-php-ext-install pdo_pgsql
RUN install-php-extensions xdebug \
&& composer install --no-cache --prefer-dist --no-scripts --no-progress --no-plugins --no-interaction \
&& composer dump-autoload --optimize --classmap-authoritative

###
## laminas/laminas-cache supported extensions
###
## ---------- Production image ----------
FROM base AS prod

## APCU
# RUN pecl install apcu \
# && docker-php-ext-enable apcu
LABEL maintainer="getlaminas.org" \
org.label-schema.docker.dockerfile="/Dockerfile" \
org.label-schema.name="Laminas MVC Skeleton" \
org.label-schema.url="https://docs.getlaminas.org/mvc/" \
org.label-schema.vcs-url="https://github.com/laminas/laminas-mvc-skeleton"

## Memcached
# RUN apt-get install --yes libmemcached-dev \
# && pecl install memcached \
# && docker-php-ext-enable memcached
WORKDIR /var/www

## MongoDB
# RUN pecl install mongodb \
# && docker-php-ext-enable mongodb
ENV APP_ENV=production

## Redis support. igbinary and libzstd-dev are only needed based on
## redis pecl options
# RUN pecl install igbinary \
# && docker-php-ext-enable igbinary \
# && apt-get install --yes libzstd-dev \
# && pecl install redis \
# && docker-php-ext-enable redis
RUN composer install --no-cache --prefer-dist --no-dev --no-scripts --no-progress --no-plugins --no-interaction \
&& composer dump-autoload --optimize --apcu

#Clean up
RUN apt-get clean \
&& rm -rf /root/.composer \
&& rm -rf /usr/local/bin/install-php-extensions \
&& rm -rf /usr/local/bin/docker-php-ext-* \
&& rm -rf /usr/src/php.tar.xz \
&& rm -rf /usr/bin/phpize \
&& rm -rf /usr/bin/php-config

WORKDIR /var/www
USER www-data
24 changes: 19 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ $ composer static-analysis

## Using docker-compose

This skeleton provides a `docker-compose.yml` for use with [docker-compose](https://docs.docker.com/compose/);
This skeleton provides a `docker-compose.yml` and a `docker-compose.override.yml` for use with [docker-compose](https://docs.docker.com/compose/);
it uses the provided `Dockerfile` to build a docker image for the `laminas` container created with `docker-compose`.

Build and start the image and container using:
Expand All @@ -131,18 +131,32 @@ $ docker-compose run laminas composer install

Some composer packages optionally use additional PHP extensions.
The Dockerfile contains several commented-out commands which enable some of the more popular php extensions.
For example, to install `pdo-pgsql` support for `laminas/laminas-db` uncomment the lines:
For example, to install `pdo-pgsql` support for `laminas/laminas-db` uncomment the lines `pdo-pgsql` :

```sh
# RUN apt-get install --yes libpq-dev \
# && docker-php-ext-install pdo_pgsql
RUN set -eux; \
install-php-extensions \
apcu \
intl \
opcache \
zip \
## Add here all extensions you need
# memcached \
# mongodb \
# redis \
# mbstring \
pdo_mysql \
# pdo_pgsql \
;
```

then re-run the `docker-compose up -d --build` line as above.

> You may also want to combine the various `apt-get` and `docker-php-ext-*`
> You may also want to combine the various `apt-get` and `install-php-extensions`
> statements later to reduce the number of layers created by your image.

> [install-php-extensions](https://github.com/mlocati/docker-php-extension-installer) is a script which easily installs PHP extensions in Docker containers.

## Web server setup

### Apache setup
Expand Down
12 changes: 12 additions & 0 deletions docker-compose.override.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
services:
laminas:
build:
target: dev
environment:
- APP_ENV=development
volumes:
- ./bin:/var/www/bin
- ./config:/var/www/config
- ./data:/var/www/data
- ./module:/var/www/module
- ./public:/var/www/public
9 changes: 6 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
services:
laminas:
build: .
build:
context: .
dockerfile: Dockerfile
target: prod
environment:
- APP_ENV=production
ports:
- "8080:80"
volumes:
- .:/var/www
Loading