Skip to content

Commit

Permalink
run-docker.sh: functionality improvements
Browse files Browse the repository at this point in the history
- adding automatic use of sudo when working with docker
- improving image docker user setup to access repository from image docker
- adding a special flag `--stop` which stops the running of image docker
  • Loading branch information
Maxython committed Sep 9, 2024
1 parent c37c65a commit c50cd8e
Showing 1 changed file with 31 additions and 11 deletions.
42 changes: 31 additions & 11 deletions scripts/run-docker.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,20 @@ fi

USER=builder

if [ -n "${TERMUX_DOCKER_USE_SUDO-}" ]; then
if [ $(id -u) -ne 0 ]; then
SUDO="sudo"
else
SUDO=""
fi

echo "Running container '$CONTAINER_NAME' from image '$TERMUX_BUILDER_IMAGE_NAME'..."
STOP_CONTAINER=false
if [ "$1" = "--stop" ]; then
STOP_CONTAINER=true
fi

if [ "$STOP_CONTAINER" = "false" ]; then
echo "Running container '$CONTAINER_NAME' from image '$TERMUX_BUILDER_IMAGE_NAME'..."
fi

# Check whether attached to tty and adjust docker flags accordingly.
if [ -t 1 ]; then
Expand All @@ -44,6 +51,10 @@ else
fi

$SUDO docker start $CONTAINER_NAME >/dev/null 2>&1 || {
if [ "$STOP_CONTAINER" ]; then
echo "Container is not running."
exit 1
fi
echo "Creating new container..."
$SUDO docker run \
--detach \
Expand All @@ -54,21 +65,30 @@ $SUDO docker start $CONTAINER_NAME >/dev/null 2>&1 || {
--tty \
$TERMUX_BUILDER_IMAGE_NAME
if [ "$UNAME" != Darwin ]; then
if [ $(id -u) -ne 1001 -a $(id -u) -ne 0 ]; then
REPO_UID="$(stat -c %u $REPOROOT)"
REPO_GID="$(stat -c %g $REPOROOT)"
if [ "$REPO_UID" -eq "0" ]; then
echo "Warning, the repository is cloned by root. Because of this, some script functions will not work in container."
elif [ $REPO_UID -ne 1001 ]; then
echo "Changed builder uid/gid... (this may take a while)"
$SUDO docker exec $DOCKER_TTY $CONTAINER_NAME sudo chown -R $(id -u) $CONTAINER_HOME_DIR
$SUDO docker exec $DOCKER_TTY $CONTAINER_NAME sudo chown -R $(id -u) /data
$SUDO docker exec $DOCKER_TTY $CONTAINER_NAME sudo usermod -u $(id -u) builder
$SUDO docker exec $DOCKER_TTY $CONTAINER_NAME sudo groupmod -g $(id -g) builder
$SUDO docker exec $DOCKER_TTY $CONTAINER_NAME sudo chown -R $REPO_UID $CONTAINER_HOME_DIR
$SUDO docker exec $DOCKER_TTY $CONTAINER_NAME sudo chown -R $REPO_UID /data
$SUDO docker exec $DOCKER_TTY $CONTAINER_NAME sudo usermod -u $REPO_UID builder
$SUDO docker exec $DOCKER_TTY $CONTAINER_NAME sudo groupmod -g $REPO_GID builder
fi
fi
}

# Set traps to ensure that the process started with docker exec and all its children are killed.
. "$TERMUX_SCRIPTDIR/scripts/utils/docker/docker.sh"; docker__setup_docker_exec_traps

if [ "$#" -eq "0" ]; then
set -- bash
fi

$SUDO docker exec --env "DOCKER_EXEC_PID_FILE_PATH=$DOCKER_EXEC_PID_FILE_PATH" --interactive $DOCKER_TTY $CONTAINER_NAME "$@"
if [ "$STOP_CONTAINER" = "true" ]; then
echo "Stopping container '$CONTAINER_NAME'..."
$SUDO docker stop $CONTAINER_NAME >/dev/null 2>&1
$SUDO docker rm $CONTAINER_NAME >/dev/null 2>&1
else
# Set traps to ensure that the process started with docker exec and all its children are killed.
. "$TERMUX_SCRIPTDIR/scripts/utils/docker/docker.sh"; docker__setup_docker_exec_traps
$SUDO docker exec --env "DOCKER_EXEC_PID_FILE_PATH=$DOCKER_EXEC_PID_FILE_PATH" --interactive $DOCKER_TTY $CONTAINER_NAME "$@"
fi

0 comments on commit c50cd8e

Please sign in to comment.