-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdockerrun
More file actions
executable file
·152 lines (139 loc) · 4.17 KB
/
Copy pathdockerrun
File metadata and controls
executable file
·152 lines (139 loc) · 4.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/bin/bash
## Description: Runs the docker container supplied in the first argument
## Dependencies: docker
## Usage: ./dockerrun.sh <docker_image> <args>
## Arguments:
## docker_image: The Docker image to run (Specified in ~/scripts/dockerfiles)
## args: Additional arguments to pass to the Docker container
## Author: Tom Steer
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# Remove /bin if it exists in the path
if [[ "$SCRIPT_DIR" == */bin ]]; then
SCRIPT_DIR="${SCRIPT_DIR%/bin}"
fi
DOCKERFILES_DIR="$SCRIPT_DIR/dockerfiles"
REBUILD=false
ORIGINAL_ARGS=("$@")
DOCKERIMAGE_PREFIX="scripts"
usage() {
echo "Usage: $0 [flags] <docker_image> [args]"
echo "Flags:"
echo " --rebuild: Rebuild the Docker image before running"
echo " --list: List available Docker images"
echo " --mount <path>: Mount a local directory to the container at /mnt"
echo " --help: Show this help message"
exit 0
}
# Check if the first argument is a flag
while [[ $# -gt 0 ]]; do
case "$1" in
--rebuild)
REBUILD=true
shift
;;
--list)
echo "Available Docker images:"
ls "$DOCKERFILES_DIR"
exit 0
;;
--mount)
if [ -z "$2" ]; then
echo "Error: --mount requires a path argument"
exit 1
fi
MOUNT_PATH=$(realpath "$2")
shift 2
;;
--help)
usage
;;
*)
break
;;
esac
done
# If the docker image is rebuild then take the second argument as the docker image name and rebuild
DOCKER_IMAGE="$1"
if [ -z "$DOCKER_IMAGE" ]; then
echo "Error: No Docker image specified"
usage
fi
DOCKERFILE_PATH=$DOCKERFILES_DIR/"$DOCKER_IMAGE"
shift
# Check if the docker image exists in the scripts directory
if [ ! -d $DOCKERFILE_PATH ]; then
echo "Docker image '$DOCKER_IMAGE' not found in $DOCKERFILES_DIR."
echo "Available Docker images:"
ls "$DOCKERFILES_DIR"
exit 1
fi
# Check if there is a .rebuild file in the docker image directory
if [ -f $DOCKERFILE_PATH/.rebuild ]; then
echo "Rebuilding Docker image '$DOCKER_IMAGE' due to .rebuild file."
REBUILD=true
fi
# Check if the sha1sum of the Dockerfile has changed
if [ -f $DOCKERFILE_PATH/Dockerfile ]; then
CHECKSUM_FILE="$DOCKERFILE_PATH/.checksum"
CURRENT_CHECKSUM=$(sha1sum "$DOCKERFILE_PATH/Dockerfile" | awk '{print $1}')
if [ -f "$CHECKSUM_FILE" ]; then
SAVED_CHECKSUM=$(cat "$CHECKSUM_FILE")
if [ "$CURRENT_CHECKSUM" != "$SAVED_CHECKSUM" ]; then
echo "Rebuilding Docker image '$DOCKER_IMAGE' due to Dockerfile change."
REBUILD=true
fi
else
# No checksum file, so assume we need to build
REBUILD=true
fi
# Save the current checksum
echo -n "$CURRENT_CHECKSUM" > "$CHECKSUM_FILE"
fi
# Check if able to run docker commands
docker info > /dev/null 2>&1
if [ $? -ne 0 ]; then
# Check if root
if [ "$(id -u)" -ne 0 ]; then
echo "Restarting as root to run docker commands..."
echo "Running: sudo $0 ${ORIGINAL_ARGS[*]}"
exec sudo "$0" "${ORIGINAL_ARGS[@]}"
else
echo "Docker is not running"
echo "Please ensure Docker is installed and running"
exit 1
fi
fi
if ! docker image inspect "$DOCKERIMAGE_PREFIX/$DOCKER_IMAGE" > /dev/null 2>&1; then
echo "Docker image '$DOCKERIMAGE_PREFIX/$DOCKER_IMAGE' not found. Building..."
REBUILD=true
fi
if [ "$REBUILD" = true ]; then
docker build -t "$DOCKERIMAGE_PREFIX/$DOCKER_IMAGE" "$DOCKERFILE_PATH"
if [ $? -ne 0 ]; then
echo "Failed to build Docker image '$DOCKERIMAGE_PREFIX/$DOCKER_IMAGE'."
exit 1
fi
fi
# Run the docker container from the docker compose file if one exists
if [ ! -f "$DOCKERFILE_PATH/docker-compose.yaml" ]; then
if [ "$MOUNT_PATH" ]; then
echo "Running Docker container '$DOCKERIMAGE_PREFIX/$DOCKER_IMAGE' with mounted path '$MOUNT_PATH'."
docker run --rm -it -v "$MOUNT_PATH":/mnt "$DOCKERIMAGE_PREFIX/$DOCKER_IMAGE" "$@"
else
docker run --rm -it "$DOCKERIMAGE_PREFIX/$DOCKER_IMAGE" "$@"
fi
if [ $? -ne 0 ]; then
echo "Failed to run Docker container '$DOCKERIMAGE_PREFIX/$DOCKER_IMAGE'."
exit 1
fi
exit 0
fi
echo "Running with docker compose"
if [ "$MOUNT_PATH" ]; then
echo "Mounting is not supported with docker compose backed containers."
fi
docker compose -f $DOCKERFILE_PATH/docker-compose.yml run --rm app "$@"
if [ $? -ne 0 ]; then
echo "Failed to run Docker container '$DOCKERIMAGE_PREFIX/$DOCKER_IMAGE'."
exit 1
fi