-
Notifications
You must be signed in to change notification settings - Fork 12
/
docker.sh
executable file
·106 lines (97 loc) · 2.79 KB
/
docker.sh
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
#!/bin/bash
# Default values
username="pufferai" # replace with your Docker Hub username
dockerfile="" # Dockerfile to use
image="puffertank"
tag="2.0"
name="puffertank"
# Function for building Docker image
build() {
# Verify a Dockerfile was provided
if [ -z "$dockerfile" ]; then
echo "You must specify a Dockerfile with -d."
exit 1
fi
# Check if a Docker container with the same name already exists
if [ "$(docker ps -aq -f name=^/${name}$)" ]; then
# Stop and remove the existing container
echo "A Docker container with the name ${name} already exists. Stopping and removing it..."
docker stop ${name}
docker rm ${name}
fi
echo "Building Docker image ${username}/${image}:${tag} with Dockerfile ${dockerfile}..."
docker build -t ${username}/${image}:${tag} -f ${dockerfile} .
}
# Function for testing Docker image
# Need this on ubuntu for x11: xhost +local:docker
test() {
# Check if a Docker container with the same name already exists
if [ "$(docker ps -aq -f name=^/${name})" ]; then
# If the container exists and is stopped, start it
echo "A Docker container with the name ${name} already exists. Starting it..."
docker start ${name}
else
# If the container does not exist, run a new one
echo "Running Docker image ${username}/${image}:${tag} and executing shell..."
docker run -it \
--name ${name} \
--gpus all \
-v /tmp/.X11-unix:/tmp/.X11-unix \
-v /var/run/docker.sock:/var/run/docker.sock \
-v /mnt/wslg:/mnt/wslg \
-v "$(pwd):/puffertank/docker" \
-e DISPLAY \
-e WAYLAND_DISPLAY \
-e XDG_RUNTIME_DIR \
-e PULSE_SERVER \
-p 8000:8000 \
${username}/${image}:${tag} bash
fi
# Attach to the running container
docker exec -it ${name} bash
}
# Function for pushing Docker image
push() {
echo "Pushing Docker image ${username}/${name}:${tag}..."
docker push ${username}/${name}:${tag}
}
# Function for displaying usage instructions
usage() {
echo "Usage: $0 command [-d dockerfile] [-n name] [-i image] [-t tag] [-u username]"
echo "Commands:"
echo " build"
echo " test"
echo " push"
}
# Main script
if [ "$#" -eq 0 ]; then
usage
exit 1
fi
command=$1
shift
# Parse command-line arguments for Dockerfile, name, tag, and username
while getopts n:i:t:u:d: flag
do
case "${flag}" in
n) name=${OPTARG};;
i) image=${OPTARG};;
t) tag=${OPTARG};;
u) username=${OPTARG};;
d) dockerfile=${OPTARG};;
esac
done
case $command in
build)
build
;;
test)
test
;;
push)
push
;;
*)
usage
;;
esac