Skip to content

Instantly share code, notes, and snippets.

@shahin-you
Created February 23, 2025 23:35
Show Gist options
  • Save shahin-you/6144230c954c5d14cb6217be39139c0d to your computer and use it in GitHub Desktop.
Save shahin-you/6144230c954c5d14cb6217be39139c0d to your computer and use it in GitHub Desktop.
dmake script for running commands in docker container
#!/bin/bash
# Get the directory containing this script
SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd)
# Go one level up to find the project directory
PROJECT_DIR="$(dirname "$SCRIPT_DIR")"
# Container version
CONTAINER_VERSION=1.0.0
# Set the Docker image name and docker file name
DOCKER_IMAGE_NAME="build_env:${CONTAINER_VERSION}"
DOCKERFILE_NAME="$SCRIPT_DIR/BuildEnvDockerFile"
case "$1" in
-b|--build)
shift
# Check if the Docker image exists, build it if not
if ! docker image inspect "$DOCKER_IMAGE_NAME" > /dev/null 2>&1; then
echo "Building Docker image '$DOCKER_IMAGE_NAME'..."
docker build -t "$DOCKER_IMAGE_NAME" -f "$DOCKERFILE_NAME" "$SCRIPT_DIR"
else
echo "Docker image '$DOCKER_IMAGE_NAME' already exists."
fi
;;
-r|--run)
shift
if [ -z "$1" ]; then
echo "Error: No command specified to run in the container."
exit 1
fi
docker run --rm -v "$PROJECT_DIR":/workspace -w /workspace -e IN_DOCKER=true "$DOCKER_IMAGE_NAME" /bin/bash -c "$@"
;;
-m|--make)
shift
docker run --rm -v "$PROJECT_DIR":/workspace -w /workspace -e IN_DOCKER=true "$DOCKER_IMAGE_NAME" make "$@"
;;
-B|--bash)
shift
docker run --rm -v "$PROJECT_DIR":/workspace -w /workspace -e IN_DOCKER=true -it "$DOCKER_IMAGE_NAME" /bin/bash
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment