diff --git a/Dockerfile b/Dockerfile index d205450..290954c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,26 +1,35 @@ -FROM php:7.4 - -# Install npm -# libzip-dev: for the PHP zip extension (used by Composer) -RUN curl -sL https://deb.nodesource.com/setup_12.x | bash - \ - && apt update && apt install -y git ssh apache2-utils nodejs python3-pip libzip-dev zip && rm -rf /var/lib/apt/lists/* - -# Install Composer -RUN curl -sS https://getcomposer.org/installer | php \ - && mv composer.phar /usr/local/bin/composer \ - && chmod +x /usr/local/bin/composer - -# Install the `aws` CLI tool -RUN pip3 install --upgrade --user awscli && echo 'export PATH=/root/.local/bin:$PATH'>/root/.bashrc - -# Install serverless -RUN npm install -g serverless - -RUN docker-php-ext-install zip pdo_mysql - -RUN mkdir -p /var/task - -# Register the Serverless and AWS bin directories -ENV PATH="/root/.serverless/bin:/root/.local/bin:${PATH}" - -WORKDIR '/var/task' +FROM composer:latest + +# composer +# https://github.com/composer/docker/blob/master/1.10/Dockerfile +# based on php:7-alpine +# https://github.com/docker-library/php/blob/master/7.4/alpine3.11/cli/Dockerfile +# based on alpine:3.11 + +# Package Dependencies: +# serverless +# docker +# awscli +# groff: needed by the AWS cli +# hirak/prestissimo: accelerates installing Composer dependencies +# yarn: npm alternative +RUN adduser -D -G users -h /home/app app \ + && composer global require hirak/prestissimo \ + && apk add --no-cache \ + docker \ + yarn \ + groff \ + && yarn global add serverless \ + && pip3 install \ + --progress-bar off \ + --no-cache-dir \ + --disable-pip-version-check \ + awscli + +USER app +ENV PATH=$PATH:/tmp/vendor/bin \ + COMPOSER_HOME=/var/task/.composer +VOLUME /var/task +VOLUME /home/app/.aws +VOLUME /var/run/docker.sock +WORKDIR /var/task diff --git a/README.md b/README.md index 4a662f1..d23e464 100644 --- a/README.md +++ b/README.md @@ -1,14 +1,22 @@ -This repository provides a development environment for Bref via a Docker image. +This repository provides a development environment for Bref via single Docker image and convenience script. Thanks to that image you can easily get started with Bref without having to install its tools: `serverless`, `aws`, `php`, `composer`… ## Usage -Run any command in the container: +Run any command in the container using `./dev-env`: ```bash -docker run --rm -it bref/dev-env +./dev-env # For example: -docker run --rm -it bref/dev-env php -v +./dev-env php -v ``` + +You can also clone this repo and make a symlink of `dev-env` script to somewhere in your `$PATH`. + +## Explanations + +Bref is NOT preinstalled in `bref/dev-env` image. For it to work you have to first install it via `./dev-env composer require bref/bref`. This will create not only typical `vendor` directory in your project but also `.composer` directory for Composer cache, which you can add to your `.gitignore` file. + +The Serverless util uses `.serverless` directory also located in your project. You can ignore that as well. diff --git a/dev-env b/dev-env new file mode 100755 index 0000000..f423ec5 --- /dev/null +++ b/dev-env @@ -0,0 +1,48 @@ +#!/usr/bin/env bash + +set -eu + +# This is an array of where the Docker socket usually is on most distributions +declare -a POSSIBLE_DOCKER_SOCK_PATHS=( + /run/docker.sock + /var/run/docker.sock +) + +# Path to your `docker.sock` file is required for `serverless invoke local --docker -f myFunction` to work. +DOCKER_SOCK_PATH= +readonly DOCKER_GROUP_ID=$(getent group docker | cut -d: -f3) +readonly AWS_CONF_DIR="${HOME}/.aws" + +# Find out which of the possible paths for Docker socket is valid +for path in "${POSSIBLE_DOCKER_SOCK_PATHS[@]}" ; do + if [[ -S "${path}" ]]; then + DOCKER_SOCK_PATH="${path}" + break + fi +done + +if [[ -z "${DOCKER_SOCK_PATH}" ]]; then + echo "Unable to locate Docker socket." + echo "Please make sure Docker service is installed." + exit 1 +fi + +if [[ ! -d "${AWS_CONF_DIR}" ]]; then + echo "AWS settings directory is not found in ${AWS_CONF_DIR}" + exit 1 +fi + +# --user So that all files created in container are actually owned by the user running this container +# --group-add Sets GID of a user able to run Docker since docker commands run in container +# will still be run via Docker socket so user running this container needs to be able to work with it +# The `HOST_AWS_CONF_DIR` ENV variable is needed for running `bref/dashboard`. +# It's because we can't simply use volume mappings from inside of `bref/dev-env` container +# since in case of Docker in Docker mappings are from host instead. +docker run --rm --interactive --tty \ + --user "$(id -u)":"$(id -g)" \ + --group-add "${DOCKER_GROUP_ID}" \ + --env HOST_AWS_CONF_DIR="${AWS_CONF_DIR}" \ + --volume "${DOCKER_SOCK_PATH}:/var/run/docker.sock" \ + --volume "${AWS_CONF_DIR}:/home/app/.aws" \ + --volume "${PWD}:/app" \ + "bref/dev-env" "$@"