bash.sh 1.59 KB
Newer Older
1 2 3 4 5
#!/usr/bin/env bash
#
# Start a bash, mount /workspace to be current directory.
#
# Usage: docker/bash.sh <CONTAINER_NAME>
6 7 8 9
#     Starts an interactive session
#
# Usage2: docker/bash.sh <CONTAINER_NAME> [COMMAND]
#     Execute command in the docker image, non-interactive
10 11
#
if [ "$#" -lt 1 ]; then
12
    echo "Usage: docker/bash.sh <CONTAINER_NAME> [COMMAND]"
13 14 15
    exit -1
fi

16 17 18 19 20 21 22 23 24 25
DOCKER_IMAGE_NAME=("$1")

if [ "$#" -eq 1 ]; then
    COMMAND="bash"
    CI_DOCKER_EXTRA_PARAMS=("-it --net=host")
else
    shift 1
    COMMAND=("$@")
fi

26 27 28 29 30 31 32 33 34 35 36 37
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
WORKSPACE="$(pwd)"

# Use nvidia-docker if the container is GPU.
if [[ "${DOCKER_IMAGE_NAME}" == *"gpu"* ]]; then
    DOCKER_BINARY="nvidia-docker"
else
    DOCKER_BINARY="docker"
fi

# Print arguments.
echo "WORKSPACE: ${WORKSPACE}"
38
echo "DOCKER CONTAINER NAME: ${DOCKER_IMAGE_NAME}"
39 40
echo ""

41 42
echo "Running '${COMMAND[@]}' inside ${DOCKER_IMAGE_NAME}..."

43 44 45 46
# By default we cleanup - remove the container once it finish running (--rm)
# and share the PID namespace (--pid=host) so the process inside does not have
# pid 1 and SIGKILL is propagated to the process inside (jenkins can kill it).
echo ${DOCKER_BINARY}
47
${DOCKER_BINARY} run --rm --pid=host\
48 49 50 51 52 53 54 55
    -v ${WORKSPACE}:/workspace \
    -v ${SCRIPT_DIR}:/docker \
    -w /workspace \
    -e "CI_BUILD_HOME=/workspace" \
    -e "CI_BUILD_USER=$(id -u -n)" \
    -e "CI_BUILD_UID=$(id -u)" \
    -e "CI_BUILD_GROUP=$(id -g -n)" \
    -e "CI_BUILD_GID=$(id -g)" \
56
    ${CI_DOCKER_EXTRA_PARAMS[@]} \
57
    ${DOCKER_IMAGE_NAME}\
58
    bash --login /docker/with_the_same_user \
59
    ${COMMAND[@]}