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

Run applications locally with Bref Docker images #367

Closed
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
71 changes: 69 additions & 2 deletions docs/local-development.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,76 @@ php -S localhost:8000 index.php
# The application is now available at http://localhost:8000/
```

In order to run the application locally in an environment closer to production, we are working on [Docker images in #237](https://github.com/brefphp/bref/issues/237).
In order to run the application locally in an environment closer to production, you can use the [Bref Docker images](https://cloud.docker.com/u/bref). For example for an HTTP application, create the following `docker-compose.yml`:

There is also a [Serverless plugin called "serverless-offline"](https://github.com/dherault/serverless-offline) that runs API Gateway locally. However it currently doesn't support layers, which means it doesn't work with Bref yet. We are working on this in [#648](https://github.com/dherault/serverless-offline/pull/648).
```yaml
web:
image: bref/web
ports:
- "8080:80"
volumes:
- .:/var/task
links:
- php
environment:
HANDLER: "test.php"
php:
image: bref/php-73-fpm-dev
volumes:
- .:/var/task:ro
# - ./var/cache:/code/var/cache
```

After running `docker-compose up`, the application will be available at [http://localhost:8000/](http://localhost:8000/).

The `HANDLER` environment variable lets you define which PHP file will be handling all HTTP requests. This should be the same handler that you have defined in `serverless.yml` for your HTTP function.

> Currently the Docker image support only one PHP handler. If you have multiple HTTP functions in `serverless.yml`, you can duplicate the service in `docker-compose.yml` to have one container per lambda function.

### Read-only filesystem

The code will be mounted as read-only in `/var/task`, just like in Lambda. However when developing locally, it is common to regenerate cache files on the fly (for example Symfony or Laravel cache). You have 2 options:

- mount the whole codebase as writable:

```yaml
volumes:
- .:/var/task
```
- mount a specific cache directory as writable (better):

```yaml
volumes:
- .:/var/task:ro
- ./cache:/var/task/cache
```

### Assets

If you want to serve assets locally, you can define a `DOCUMENT_ROOT` environment variable:

```yaml
web:
image: bref/web
ports:
- "8080:80"
volumes:
- .:/var/task
links:
- php
environment:
HANDLER: public/index.php
DOCUMENT_ROOT: public
php:
image: bref/php-73-fpm-dev
volumes:
- .:/var/task:ro
# - ./var/cache:/code/var/cache
```

In the example above, a `public/assets/style.css` file will be accessible at `http://localhost:8000/assets/style.css`.

> Be aware that serving assets in production will not work like this out of the box. You will need [to use a S3 bucket](/docs/runtimes/http.md#assets).

## Console applications

Expand Down
37 changes: 32 additions & 5 deletions runtime/Makefile
Original file line number Diff line number Diff line change
@@ -1,15 +1,42 @@
SHELL := /bin/bash

.PHONY: layers
# Publish the layers on AWS Lambda
publish: layers
php publish.php

# Build the layers
layers: export/console.zip export/php-%.zip
layers: export/console.zip export/php-72.zip export/php-73.zip export/php-72-fpm.zip export/php-73-fpm.zip

# The PHP runtimes
export/php-%.zip:
cd php && make distribution
export/php%.zip: build
PHP_VERSION=$$(echo $@| tail -c +8|head -c -5);\
rm -f $@;\
mkdir export/tmp ; cd export/tmp ;\
docker run --entrypoint "tar" bref/$$PHP_VERSION:latest -ch -C /opt . |tar -x;zip --quiet --recurse-paths ../$$PHP_VERSION.zip . ;
rm -rf export/tmp

# The console runtime
export/console.zip: console/bootstrap
export/console.zip: layers/console/bootstrap
rm -f export/console.zip
cd console && zip ../export/console.zip bootstrap
cd layers/console && zip ../../export/console.zip bootstrap

# Build the docker container that will be used to compile PHP and its extensions
compiler: compiler.Dockerfile
docker build -f ${PWD}/compiler.Dockerfile -t bref/runtime/compiler:latest .

# Compile PHP and its extensions
build: compiler
docker build -f ${PWD}/php-intermediary.Dockerfile -t bref/php-72-intermediary:latest $(shell helpers/docker_args.sh versions.ini php72) .
cd layers/fpm ; docker build -t bref/php-72-fpm:latest --build-arg LAYER_IMAGE=bref/php-72-intermediary:latest . ; cd ../..
cd layers/fpm-dev ; docker build -t bref/php-72-fpm-dev:latest --build-arg LAYER_IMAGE=bref/php-72-intermediary:latest . ; cd ../..
cd layers/function ; docker build -t bref/php-72:latest --build-arg LAYER_IMAGE=bref/php-72-intermediary:latest . ; cd ../..
docker build -f ${PWD}/php-intermediary.Dockerfile -t bref/php-73-intermediary:latest $(shell helpers/docker_args.sh versions.ini php73) .
cd layers/fpm ; docker build -t bref/php-73-fpm:latest --build-arg LAYER_IMAGE=bref/php-73-intermediary:latest . ; cd ../..
cd layers/fpm-dev ; docker build -t bref/php-73-fpm-dev:latest --build-arg LAYER_IMAGE=bref/php-73-intermediary:latest . ; cd ../..
cd layers/function ; docker build -t bref/php-73:latest --build-arg LAYER_IMAGE=bref/php-73-intermediary:latest . ; cd ../..
cd layers/web; docker build -t bref/web:latest . ; cd ../..

publish: build
docker push bref/php-72:latest
docker push bref/php-73:latest
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
# https://docs.aws.amazon.com/lambda/latest/dg/current-supported-versions.html
# AWS has kindly provided us with it as a base docker image.
# https://github.com/aws/amazon-linux-docker-images/tree/2017.03
FROM amazonlinux:2017.03
FROM amazonlinux:2
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Waiting on #332 to use 2018.03 instead.

LABEL authors="Bubba Hines <[email protected]>"
LABEL vendor1="Signature Tech Studio, Inc."
LABEL vendor2="bref"
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
6 changes: 6 additions & 0 deletions runtime/layers/fpm-dev/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
ARG LAYER_IMAGE
FROM $LAYER_IMAGE

COPY ./php-fpm.conf /opt/bref/etc/php-fpm.conf
EXPOSE 9000
CMD /opt/bin/php-fpm --nodaemonize --fpm-config /opt/bref/etc/php-fpm.conf
24 changes: 24 additions & 0 deletions runtime/layers/fpm-dev/php-fpm.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
; Logging anywhere on disk doesn't make sense on lambda since instances are ephemeral
error_log = /dev/stderr
pid = /tmp/php-fpm.pid
; Log above warning because PHP-FPM logs useless notices
; We must comment this flag else uncaught exceptions/fatal errors are not reported in the logs!
; TODO: report that to the PHP bug tracker
;log_level = 'warning'

[default]
pm = static
; We only need one child because a lambda can process only one request at a time
pm.max_children = 1
user = nobody
listen = 9000
; Allows PHP processes to access the lambda's environment variables
clear_env = no
; Forward stderr of PHP processes to stderr of PHP-FPM (so that it can be sent to cloudwatch)
catch_workers_output = yes
; New PHP 7.3 option that disables a verbose log prefix
; Disabled for now until we switch to PHP 7.3
;decorate_workers_output = no
; Limit the number of core dump logs to 1 to avoid filling up the /tmp disk
; See https://github.com/brefphp/bref/issues/275
rlimit_core = 1
13 changes: 13 additions & 0 deletions runtime/layers/fpm/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
ARG LAYER_IMAGE
FROM $LAYER_IMAGE

WORKDIR /opt

COPY bootstrap bootstrap
COPY php.ini bref/etc/php/conf.d/bref.ini
COPY php-fpm.conf bref/etc/php-fpm.conf

FROM lambci/lambda:provided

WORKDIR /
COPY --from=0 /opt /opt
File renamed without changes.
File renamed without changes.
File renamed without changes.
12 changes: 12 additions & 0 deletions runtime/layers/function/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
ARG LAYER_IMAGE
FROM $LAYER_IMAGE

WORKDIR /opt

COPY bootstrap bootstrap
COPY php.ini bref/etc/php/conf.d/bref.ini

FROM lambci/lambda:provided

WORKDIR /
COPY --from=0 /opt /opt
File renamed without changes.
4 changes: 4 additions & 0 deletions runtime/layers/web/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
FROM nginx:latest
COPY default.conf /etc/nginx/conf.d/default.conf

CMD sed -i "s|##DOCUMENT_ROOT##|$DOCUMENT_ROOT|g" /etc/nginx/conf.d/default.conf;sed -i "s|##HANDLER##|$HANDLER|g" /etc/nginx/conf.d/default.conf && nginx -g "daemon off;"
14 changes: 14 additions & 0 deletions runtime/layers/web/default.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
server {
server_name php-docker.local;
root /var/task/##DOCUMENT_ROOT##;

# Allow /src/index.php and src/test.php. This two routes should be in the .stack.yaml
location = / {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php:9000;
include fastcgi_params;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /var/task/##HANDLER##;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
3 changes: 0 additions & 3 deletions runtime/loop/README.md

This file was deleted.

3 changes: 0 additions & 3 deletions runtime/loop/bootstrap

This file was deleted.

107 changes: 0 additions & 107 deletions runtime/loop/build.sh

This file was deleted.

18 changes: 0 additions & 18 deletions runtime/loop/php.ini

This file was deleted.

29 changes: 0 additions & 29 deletions runtime/loop/publish.sh

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ RUN ln -s /opt/bref/sbin/* /opt/bin

# Now we get rid of everything that is unnecessary. All the build tools, source code, and anything else
# that might have created intermediate layers for docker. Back to base AmazonLinux we started with.
FROM amazonlinux:2017.03
FROM amazonlinux:2
ENV INSTALL_DIR="/opt/bref"
ENV PATH="/opt/bin:${PATH}" \
LD_LIBRARY_PATH="${INSTALL_DIR}/lib64:${INSTALL_DIR}/lib"
Expand Down
Loading