-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocker.sh
81 lines (73 loc) · 1.98 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
#!/bin/bash
# Default values
username="pufferai" # replace with your Docker Hub username
name="base"
tag="latest"
# Parse command-line arguments for name, tag, and username
while getopts n:t:u: flag
do
case "${flag}" in
n) name=${OPTARG};;
t) tag=${OPTARG};;
u) username=${OPTARG};;
esac
done
# Function for building Docker image
build() {
# 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}/${name}:${tag}..."
docker build -t ${username}/${name}:${tag} .
}
# Function for testing Docker image
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}/${name}:${tag} and executing shell..."
docker run --name ${name} -it ${username}/${name}:${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 [-n name] [-t tag] [-u username] command"
echo "Commands:"
echo " build"
echo " test"
echo " push"
}
# Main script
if [ "$#" -eq 0 ]; then
usage
exit 1
fi
case $1 in
build)
build
;;
test)
test
;;
push)
push
;;
*)
usage
;;
esac