Scripts and tools to run a PHP web-application - meaning Laravel, right now - in Docker.
Note: This repository is supposed to be a Git submodule of a web application.
Ingredients
Assuming an initial repository where the (Laravel) application lives in a sub-directory.
Add the webapp-docker
repository as a Git submodule.
Then copy the example Makefile
and .dockerignore
file.
# Add submodule
git submodule add [email protected]:leggedrobotics/webapp-docker.git docker
# Copy example files
cp docker/dockerignore.example .dockerignore
cp docker/Makefile.example Makefile
In the Makefile, adjust the variables to reflect the name and directory of your app.
APP_DIR = my-app
APP_NAME = my-app
...
# build the development container
make build-development
# install composer dependencies locally
make composer-install
# install npm dependencies locally
make npm-install
# generate development certificates, assumes ${APP_NAME}.test
# override by setting APP_DOMAIN (env)var.
make dev-certs
# start the development server
make development
# watch assets
make npm-run-watch
# start a shell session in the development container
make sh
# start a Tinker (PHP REPL) session in the development container
make tinker
Add features to the images or change behavior by adding Make targets or extending the images with Dockerfiles.
Via a Dockerfile in the root of the project.
# syntax = docker/dockerfile:1.0-experimental
ARG BASE_IMG=my-app-development-base
FROM $BASE_IMG AS base
# Extend the development base image with some extra packages
RUN --mount=type=cache,target=/etc/apk/cache apk --update-cache add \
php-mysqli \
php-pdo_mysql
Then, adding to the Makefile
:
# ... rest of the Makefile
include docker/makefile.mk
build-development: build-development-base
${BUILD} -t ${APP_NAME}-development \
--target development \
.
Now, run make build-development
and make development
is usual
TODO Adding
supervisord
services
Getting ready for production by building a container that includes sources and compiled assets.
Extend the Dockerfile
and Makefile
to accomodate the necessary steps.
Example Makefile
# ... rest of the Makefile
# Build production by extending the base image
# (target `build-base`, image name `${APP_NAME}-base`)
build-production: build-base
${BUILD} --target production \
--build-arg BASE_IMG=${APP_NAME}-base \
-t ${APP_NAME}-production \
.
production: build-production
${DOCKER} run -ti --rm \
--name ${APP_NAME}-production \
-p80:80 -p443:443 \
-v${APP_NAME}-certs:/certs \
-v${APP_NAME}-production-storage:/opt/storage \
${APP_NAME}-production
Then to run:
# build production container
make build-production
# optionally, tag and push the container
# docker tag my-app-production my.registry.io/my-app
# docker push my.registry.io/my-app
# test the production container locally
make production
The UTF-8 emoji for a Seal is not widely supported...
Using Podman is simple due to the partial command-line compatibility.
DOCKER=podman BUILD=podman\ build make development
Or set the environment variables in your Makefile
.