-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocker.sh
More file actions
executable file
·160 lines (137 loc) · 3.51 KB
/
docker.sh
File metadata and controls
executable file
·160 lines (137 loc) · 3.51 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
153
154
155
156
157
158
159
160
#!/bin/bash
ENV_FILE="./.env" # Set the default path to the .env file
TEMP_ENV_FILE="./temp_env_file" # Temporary environment file
function select_prefix() {
# Query available options from Docker images
AVAILABLE_OPTIONS=($(docker images --format "{{.Repository}}" | sort -u))
# Display available options with numbers
echo "Choose an option from the following:"
echo "0) Custom prefix"
for i in "${!AVAILABLE_OPTIONS[@]}"; do
echo "$((i + 1))) ${AVAILABLE_OPTIONS[i]}"
done
read -p "Enter the number of your choice: " OPTION_NUMBER
# Validate the input
if ! [[ "$OPTION_NUMBER" =~ ^[0-9]+$ ]] || ((OPTION_NUMBER < 0)) || ((OPTION_NUMBER > ${#AVAILABLE_OPTIONS[@]})); then
echo "Invalid input. Please select a valid number."
exit 1
fi
if [ "$OPTION_NUMBER" -eq 0 ]; then
read -p "Enter the container name prefix: " CUSTOM_PREFIX
PREFIX="$CUSTOM_PREFIX"
read -p "Enter the tag for the image: " TAG
PREFIX_TAG="$PREFIX:$TAG"
else
PREFIX="${AVAILABLE_OPTIONS[OPTION_NUMBER - 1]}"
read -p "Enter the tag for the image: " TAG
PREFIX_TAG="$PREFIX:$TAG"
fi
}
function build_image() {
echo "Building image... $PREFIX_TAG"
docker build -t $PREFIX_TAG .
}
function list_images() {
docker images
}
function remove_image() {
docker rmi $PREFIX_TAG
}
function start_container() {
# Create a temporary environment file based on the original .env
cp $ENV_FILE $TEMP_ENV_FILE
sed -i 's/^ENV=.*/ENV=production/' $TEMP_ENV_FILE
read -p "Enter the port to run container on: " CUSTOM_PORT
docker run -d -p 8080:$CUSTOM_PORT --env-file $TEMP_ENV_FILE $PREFIX_TAG
# Clean up temporary environment file
rm $TEMP_ENV_FILE
echo "Do you want to view the logs? (y/n)"
read VIEW_LOGS
if [ "$VIEW_LOGS" == "y" ]; then
view_logs
fi
}
function list_containers() {
docker ps
}
function stop_container() {
docker stop $(docker ps -aq --filter ancestor=$PREFIX_TAG)
}
function remove_container() {
docker rm $(docker ps -aq --filter ancestor=$PREFIX_TAG)
}
function view_logs() {
CONTAINER_ID=$(docker ps -q --filter ancestor=$PREFIX_TAG)
if [ -z "$CONTAINER_ID" ]; then
echo "No active containers found for the specified image tag."
else
docker logs $CONTAINER_ID
fi
}
function remove_unused_images() {
echo "Removing images without a name or tag..."
docker rmi $(docker images | grep "<none>" | awk '{print $3}')
}
function remove_inactive_containers() {
echo "Removing inactive containers..."
docker container prune -f
}
echo "1. List images"
echo "2. List running containers"
echo "3. View logs"
echo "4. Build image"
echo "5. Remove image"
echo "6. Start container"
echo "7. Stop container"
echo "8. Remove container"
echo "9. Build & Start"
echo "10. Remove images without name or tag"
echo "11. Remove inactive containers"
echo "Enter your choice:"
read CHOICE
case $CHOICE in
1)
list_images
;;
2)
list_containers
;;
3)
select_prefix
view_logs
;;
4)
select_prefix
build_image
;;
5)
select_prefix
remove_image
;;
6)
select_prefix
start_container
;;
7)
select_prefix
stop_container
;;
8)
select_prefix
remove_container
;;
9)
select_prefix
build_image
start_container
;;
10)
remove_unused_images
;;
11)
remove_inactive_containers
;;
*)
echo "Invalid choice. Please select a valid option."
;;
esac