|
1 | 1 | #!/bin/sh |
2 | 2 | # |
3 | | -# Forked from https://github.com/docker/compose/blob/master/script/run/run.sh |
4 | | -# |
5 | 3 | # Run docker-compose in a container |
6 | 4 | # |
7 | 5 | # This script will attempt to mirror the host paths by using volumes for the |
8 | 6 | # following paths: |
9 | | -# * $(pwd) |
10 | | -# * $(dirname $COMPOSE_FILE) if it's set |
11 | | -# * $HOME if it's set |
| 7 | +# * ${PWD} |
| 8 | +# * $(dirname ${COMPOSE_FILE}) if it's set |
| 9 | +# * ${HOME} if it's set |
12 | 10 | # |
13 | 11 | # You can add additional volumes (or any docker run options) using |
14 | | -# the $COMPOSE_OPTIONS environment variable. |
| 12 | +# the ${COMPOSE_OPTIONS} environment variable. |
15 | 13 | # |
16 | | -# You can set a specific image tag from Docker Hub, such as "1.26.2-ls9", or "alpine" |
17 | | -# using the $DOCKER_COMPOSE_IMAGE_TAG environment variable (defaults to "latest") |
| 14 | +# You can set a specific image and tag, such as "docker/compose:1.27.4", or "docker/compose:alpine-1.27.4" |
| 15 | +# using the $DOCKER_COMPOSE_IMAGE_TAG environment variable (defaults to "docker/compose:1.27.4") |
18 | 16 | # |
19 | 17 |
|
20 | | - |
21 | 18 | set -e |
22 | 19 |
|
23 | | -# set image tag to latest if not globally set |
24 | | -DOCKER_COMPOSE_IMAGE_TAG="${DOCKER_COMPOSE_IMAGE_TAG:-latest}" |
25 | | -IMAGE="ghcr.io/linuxserver/docker-compose:$DOCKER_COMPOSE_IMAGE_TAG" |
26 | | - |
27 | 20 | # Setup options for connecting to docker host |
28 | | -if [ -z "$DOCKER_HOST" ]; then |
| 21 | +if [ -z "${DOCKER_HOST}" ]; then |
29 | 22 | DOCKER_HOST='unix:///var/run/docker.sock' |
30 | 23 | fi |
31 | 24 | if [ -S "${DOCKER_HOST#unix://}" ]; then |
|
34 | 27 | DOCKER_ADDR="-e DOCKER_HOST -e DOCKER_TLS_VERIFY -e DOCKER_CERT_PATH" |
35 | 28 | fi |
36 | 29 |
|
37 | | - |
38 | 30 | # Setup volume mounts for compose config and context |
39 | | -if [ "$(pwd)" != '/' ]; then |
40 | | - VOLUMES="-v $(pwd):$(pwd)" |
| 31 | +if [ "${PWD}" != '/' ]; then |
| 32 | + VOLUMES="-v ${PWD}:${PWD}" |
41 | 33 | fi |
42 | | -if [ -n "$COMPOSE_FILE" ]; then |
43 | | - COMPOSE_OPTIONS="$COMPOSE_OPTIONS -e COMPOSE_FILE=$COMPOSE_FILE" |
44 | | - compose_dir="$(dirname "$COMPOSE_FILE")" |
| 34 | +if [ -n "${COMPOSE_FILE}" ]; then |
| 35 | + COMPOSE_OPTIONS="${COMPOSE_OPTIONS} -e COMPOSE_FILE=${COMPOSE_FILE}" |
| 36 | + COMPOSE_DIR="$(dirname "${COMPOSE_FILE}")" |
45 | 37 | # canonicalize dir, do not use realpath or readlink -f |
46 | 38 | # since they are not available in some systems (e.g. macOS). |
47 | | - compose_dir="$(cd "$compose_dir" && pwd)" |
| 39 | + COMPOSE_DIR="$(cd "${COMPOSE_DIR}" && pwd)" |
48 | 40 | fi |
49 | | -if [ -n "$COMPOSE_PROJECT_NAME" ]; then |
50 | | - COMPOSE_OPTIONS="-e COMPOSE_PROJECT_NAME $COMPOSE_OPTIONS" |
| 41 | +if [ -n "${COMPOSE_PROJECT_NAME}" ]; then |
| 42 | + COMPOSE_OPTIONS="-e COMPOSE_PROJECT_NAME ${COMPOSE_OPTIONS}" |
51 | 43 | fi |
52 | 44 | # TODO: also check --file argument |
53 | | -if [ -n "$compose_dir" ]; then |
54 | | - VOLUMES="$VOLUMES -v $compose_dir:$compose_dir" |
| 45 | +if [ -n "${COMPOSE_DIR}" ]; then |
| 46 | + VOLUMES="${VOLUMES} -v ${COMPOSE_DIR}:${COMPOSE_DIR}" |
55 | 47 | fi |
56 | | -if [ -n "$HOME" ]; then |
57 | | - VOLUMES="$VOLUMES -v $HOME:$HOME -e HOME" # Pass in HOME to share docker.config and allow ~/-relative paths to work. |
| 48 | +if [ -n "${HOME}" ]; then |
| 49 | + VOLUMES="${VOLUMES} -v ${HOME}:${HOME} -e HOME" # Pass in HOME to share docker.config and allow ~/-relative paths to work. |
58 | 50 | fi |
59 | 51 |
|
60 | 52 | # Only allocate tty if we detect one |
61 | 53 | if [ -t 0 ] && [ -t 1 ]; then |
62 | | - DOCKER_RUN_OPTIONS="$DOCKER_RUN_OPTIONS -t" |
| 54 | + DOCKER_RUN_OPTIONS="${DOCKER_RUN_OPTIONS} -t" |
63 | 55 | fi |
64 | 56 |
|
65 | 57 | # Always set -i to support piped and terminal input in run/exec |
66 | | -DOCKER_RUN_OPTIONS="$DOCKER_RUN_OPTIONS -i" |
67 | | - |
| 58 | +DOCKER_RUN_OPTIONS="${DOCKER_RUN_OPTIONS} -i" |
68 | 59 |
|
69 | 60 | # Handle userns security |
70 | | -if docker info --format '{{json .SecurityOptions}}' 2>/dev/null | grep -q 'name=userns'; then |
71 | | - DOCKER_RUN_OPTIONS="$DOCKER_RUN_OPTIONS --userns=host" |
| 61 | +if docker info --format '{{json .SecurityOptions}}' 2> /dev/null | grep -q 'name=userns'; then |
| 62 | + DOCKER_RUN_OPTIONS="${DOCKER_RUN_OPTIONS} --userns=host" |
72 | 63 | fi |
73 | 64 |
|
74 | 65 | # Detect SELinux and add --privileged if necessary |
75 | | -if docker info --format '{{json .SecurityOptions}}' 2>/dev/null | grep -q 'name=selinux'; then |
76 | | - DOCKER_RUN_OPTIONS="$DOCKER_RUN_OPTIONS --privileged" |
| 66 | +if docker info --format '{{json .SecurityOptions}}' 2> /dev/null | grep -q 'name=selinux'; then |
| 67 | + DOCKER_RUN_OPTIONS="${DOCKER_RUN_OPTIONS} --privileged" |
77 | 68 | fi |
78 | 69 |
|
79 | 70 | # shellcheck disable=SC2086 |
80 | | -exec docker run --rm $DOCKER_RUN_OPTIONS $DOCKER_ADDR $COMPOSE_OPTIONS $VOLUMES -w "$(pwd)" $IMAGE "$@" |
| 71 | +exec docker run --rm ${DOCKER_RUN_OPTIONS} ${DOCKER_ADDR} ${COMPOSE_OPTIONS} ${VOLUMES} -w "${PWD}" "${DOCKER_COMPOSE_IMAGE_TAG:-docker/compose:1.27.4}" "$@" |
0 commit comments