Skip to content

Instantly share code, notes, and snippets.

@thommyhh
Last active March 24, 2022 16:00
Show Gist options
  • Save thommyhh/98d82e4b0d357fa38b498308b46ce662 to your computer and use it in GitHub Desktop.
Save thommyhh/98d82e4b0d357fa38b498308b46ce662 to your computer and use it in GitHub Desktop.
A script to easily run composer through docker to provide a defined environment
#!/bin/sh
# This script is meant to be user with docker images from https://hub.docker.com/r/webcoastdk/composer/tags
#
# It mounts the current working directory into /build in the container and calls the given command.
# Additionally the current users `.ssh` and `.composer` directory are also mounted to give access to composer cache
# and SSH known hosts
#
# On hosts with MacOS the SSH agent is started inside the container, as it is currently not possible to mount the SSH
# authentication agent socket into the container.
# On other hosts (mainly Linux) the current `$SSH_AUTH_SOCK` is mounted to `/ssh-agent` and the environment variable
# `SSH_AUTH_SOCK` is set to `/ssh-agent`, so the host agent is used.
#
# This scripts expects a docker file under `./docker/composer/Dockerfile`, relative to the current working directory.
# The image name prefix is determined by the environment variable `$COMPOSE_PROJECT_NAME`, as with docker-compose. If
# this variable is not set, the current working directory is used as prefix, also as with docker-compose. The variable
# can be set using an `.env` file in the current working directory or providing it otherwise.
#
# The project structure could look like this:
# Project root/
# ├── docker/
# │ └── composer/
# │ └── Dockerfile (this should install the necessary dependencies defined in your `composer.json`)
# ├── scripts/
# │ └── composer.sh (this script)
# ├── .env (optional)
# ├── composer.json
# ├── composer.lock
# └── docker-compose.yml (optional)
# Include the `.env` file if it exists (exclude lines starting with #)
if [ -f "`pwd`/.env" ]; then
export $(cat "`pwd`/.env"|grep -Ev '^#'|xargs)
fi
# Determine the docker image name
if [ -z "$COMPOSE_PROJECT_NAME" ]; then
# If no project name is set, use the current working directory
dockerImageName=$(basename $(pwd)|sed -e 's/ /_/g'|awk '{print tolower($0)}')_composer
else
dockerImageName="$COMPOSE_PROJECT_NAME"_composer
fi
# Build the docker image based on the docker file in `docker/composer/` directory and tag it as `$dockerImageName`
if [ "$1" = "build" ]; then
docker build --pull docker/composer -t "$dockerImageName"
else
# Determine, if the script is run from a terminal and add the "-t" parameter, if true
TERMINAL_PARAMETER=""
tty -s
if [ $? -eq 0 ]; then
TERMINAL_PARAMETER="t"
fi
if [ `uname -s` = "Darwin" ]; then
docker run --rm -${TERMINAL_PARAMETER}i -v "`pwd`:/build" -v "$HOME/.ssh:/root/.ssh" -v "$HOME/.composer:/root/.composer" "$dockerImageName" "$@"
else
docker run --rm -${TERMINAL_PARAMETER}i -v "`pwd`:/build" -v "$HOME/.ssh:/root/.ssh" -v "$HOME/.composer:/root/.composer" -v "$SSH_AUTH_SOCK:/ssh-agent" -e "SSH_AUTH_SOCK=/ssh-agent" "$dockerImageName" "$@"
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment