From 06c5581b08b1b666db4a138b6d235ca06ed25ed9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Luke=C5=A1?= Date: Mon, 6 Apr 2020 00:14:30 +0200 Subject: [PATCH 1/6] added new version of Dockerfile and convenience script --- Dockerfile | 62 +++++++++++++++++++++++++++++++----------------------- README.md | 28 ++++++++++++++++++++---- dev-env | 37 ++++++++++++++++++++++++++++++++ 3 files changed, 97 insertions(+), 30 deletions(-) create mode 100755 dev-env diff --git a/Dockerfile b/Dockerfile index d205450..1a0511e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,26 +1,36 @@ -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:1.10.4 + +# composer:1.10.4 +# 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 +RUN adduser -D -G users -h /home/app app \ + && composer global require hirak/prestissimo \ + && apk add --no-cache \ + docker=19.03.5-r0 \ + yarn=1.19.2-r0 \ + groff=1.22.4-r0 \ + && yarn global add serverless@1.67.0 \ + && pip3 install \ + --progress-bar off \ + --no-cache-dir \ + --disable-pip-version-check \ + awscli==1.18.39 + +USER app +ENV PATH=$PATH:/tmp/vendor/bin \ + COMPOSER_ALLOW_SUPERUSER=0 \ + COMPOSER_HOME=/app/.composer +VOLUME /app +VOLUME /home/app/.aws +VOLUME /var/run/docker.sock +WORKDIR /app + +ENTRYPOINT ["/bin/sh", "/docker-entrypoint.sh"] diff --git a/README.md b/README.md index 4a662f1..d6b74bd 100644 --- a/README.md +++ b/README.md @@ -1,14 +1,34 @@ -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.sh`: ```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 ``` + +or via `docker run` directly: + +```bash +docker run --rm --interactive --tty \ + --user "$(id -u)":"$(id -g)" \ + --group-add "${DOCKER_GROUP_ID}" \ + --volume "${DOCKER_SOCK_PATH}:/var/run/docker.sock" \ + --volume "${AWS_CONF_DIR}:/home/app/.aws" \ + --volume "${PWD}/:/app" \ + "bref/dev-env" +``` + +Path to your `docker.sock` file is required for `serverless invoke local --docker -f myFunction` to work. + +You can also clone this repo and make a symlink of `dev-env` script to somewhere in your `$PATH`. + +Bref is NOT preinstalled in `bref/dev-env` image. For it to work you have to first install it via `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..22fb2b1 --- /dev/null +++ b/dev-env @@ -0,0 +1,37 @@ +#!/usr/bin/env bash + +set -eu + +declare -a POSSIBLE_DOCKER_SOCK_PATHS=( + /run/docker.sock + /var/run/docker.sock +) +DOCKER_SOCK_PATH= +readonly DOCKER_GROUP_ID=$(getent group docker | cut -d: -f3) +readonly AWS_CONF_DIR="${HOME}/.aws" + +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 + +docker run --rm --interactive --tty \ + --user "$(id -u)":"$(id -g)" \ + --group-add "${DOCKER_GROUP_ID}" \ + --volume "${DOCKER_SOCK_PATH}:/var/run/docker.sock" \ + --volume "${AWS_CONF_DIR}:/home/app/.aws" \ + --volume "${PWD}/:/app" \ + "bref/dev-env" "$@" From a6a85313fb5e93f264607468c855f17e583b139b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Luke=C5=A1?= Date: Sun, 10 May 2020 14:26:06 +0200 Subject: [PATCH 2/6] added support for bref/dashboard via HOST_AWS_CONF_DIR --- README.md | 9 +++++++-- dev-env | 3 ++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index d6b74bd..8302d46 100644 --- a/README.md +++ b/README.md @@ -19,16 +19,21 @@ or via `docker run` directly: 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" ``` +You can also clone this repo and make a symlink of `dev-env` script to somewhere in your `$PATH`. + +## Explanations + Path to your `docker.sock` file is required for `serverless invoke local --docker -f myFunction` to work. -You can also clone this repo and make a symlink of `dev-env` script to somewhere in your `$PATH`. +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. -Bref is NOT preinstalled in `bref/dev-env` image. For it to work you have to first install it via `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. +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 index 22fb2b1..f54d2a4 100755 --- a/dev-env +++ b/dev-env @@ -31,7 +31,8 @@ fi 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" \ + --volume "${PWD}:/app" \ "bref/dev-env" "$@" From 65a12545de5a73cc448e5fe6efc22dd5870430dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Luke=C5=A1?= Date: Fri, 22 May 2020 00:29:38 +0200 Subject: [PATCH 3/6] Apply suggestions from code review Co-authored-by: Matthieu Napoli --- Dockerfile | 3 +++ dev-env | 2 ++ 2 files changed, 5 insertions(+) diff --git a/Dockerfile b/Dockerfile index 1a0511e..5e5d8ec 100644 --- a/Dockerfile +++ b/Dockerfile @@ -11,6 +11,9 @@ FROM composer:1.10.4 # docker # awscli # groff +# hirak/prestissimo: accelerates installing Composer dependencies +# yarn: npm alternative +# groff: needed by the AWS cli RUN adduser -D -G users -h /home/app app \ && composer global require hirak/prestissimo \ && apk add --no-cache \ diff --git a/dev-env b/dev-env index f54d2a4..4fc0bb7 100755 --- a/dev-env +++ b/dev-env @@ -2,6 +2,7 @@ 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 @@ -10,6 +11,7 @@ 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}" From 668f3d3d2101401295ccf2c068d46f71225cc9fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Luke=C5=A1?= Date: Fri, 22 May 2020 00:39:24 +0200 Subject: [PATCH 4/6] Apply suggestions from code review - removed unnecessary code - changed project dir from `/app` to `/var/task` --- Dockerfile | 9 +++------ README.md | 13 ------------- 2 files changed, 3 insertions(+), 19 deletions(-) diff --git a/Dockerfile b/Dockerfile index 5e5d8ec..9ecedee 100644 --- a/Dockerfile +++ b/Dockerfile @@ -29,11 +29,8 @@ RUN adduser -D -G users -h /home/app app \ USER app ENV PATH=$PATH:/tmp/vendor/bin \ - COMPOSER_ALLOW_SUPERUSER=0 \ - COMPOSER_HOME=/app/.composer -VOLUME /app + COMPOSER_HOME=/var/task/.composer +VOLUME /var/task VOLUME /home/app/.aws VOLUME /var/run/docker.sock -WORKDIR /app - -ENTRYPOINT ["/bin/sh", "/docker-entrypoint.sh"] +WORKDIR /var/task diff --git a/README.md b/README.md index 8302d46..b9c90c3 100644 --- a/README.md +++ b/README.md @@ -13,19 +13,6 @@ Run any command in the container using `./dev-env.sh`: ./dev-env php -v ``` -or via `docker run` directly: - -```bash -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" -``` - You can also clone this repo and make a symlink of `dev-env` script to somewhere in your `$PATH`. ## Explanations From 8051f6b3a467d97974e80222156ca0807d420805 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Luke=C5=A1?= Date: Sun, 24 May 2020 21:32:24 +0200 Subject: [PATCH 5/6] removed specific package and system versions since we don't really need them --- Dockerfile | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/Dockerfile b/Dockerfile index 9ecedee..290954c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,6 +1,6 @@ -FROM composer:1.10.4 +FROM composer:latest -# composer:1.10.4 +# 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 @@ -10,22 +10,21 @@ FROM composer:1.10.4 # serverless # docker # awscli -# groff -# hirak/prestissimo: accelerates installing Composer dependencies -# yarn: npm alternative -# groff: needed by the AWS cli +# 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=19.03.5-r0 \ - yarn=1.19.2-r0 \ - groff=1.22.4-r0 \ - && yarn global add serverless@1.67.0 \ + docker \ + yarn \ + groff \ + && yarn global add serverless \ && pip3 install \ --progress-bar off \ --no-cache-dir \ --disable-pip-version-check \ - awscli==1.18.39 + awscli USER app ENV PATH=$PATH:/tmp/vendor/bin \ From e1813c2b056c9d32dc7e6f7d66d10e29665881f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Luke=C5=A1?= Date: Sun, 24 May 2020 21:53:50 +0200 Subject: [PATCH 6/6] updated documentation --- README.md | 6 +----- dev-env | 8 ++++++++ 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index b9c90c3..d23e464 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ Thanks to that image you can easily get started with Bref without having to inst ## Usage -Run any command in the container using `./dev-env.sh`: +Run any command in the container using `./dev-env`: ```bash ./dev-env @@ -17,10 +17,6 @@ You can also clone this repo and make a symlink of `dev-env` script to somewhere ## Explanations -Path to your `docker.sock` file is required for `serverless invoke local --docker -f myFunction` to work. - -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. - 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 index 4fc0bb7..f423ec5 100755 --- a/dev-env +++ b/dev-env @@ -7,6 +7,8 @@ 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" @@ -30,6 +32,12 @@ if [[ ! -d "${AWS_CONF_DIR}" ]]; then 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}" \