-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenter-container.sh
executable file
·40 lines (33 loc) · 1.07 KB
/
enter-container.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
#!/bin/bash
# Show running containers on error
running_containers() {
docker ps --format "{{.Names}}"
}
err() {
[ ! -z "$1" ] && echo $1 # Error Message
echo Running containers:
running_containers | awk '{print "- " $0}'
exit 1
}
# Check if a container name was provided
[ -z "$1" ] && err "Please provide a container name"
# Use the provided container name
CONTAINER_NAME=$1
running_containers | grep $CONTAINER_NAME > /dev/null || err "$CONTAINER_NAME not found"
# Define shell
SHELL=$2
[ -z $SHELL ] && SHELL=bash
# Enter the container
docker exec -itu 0 $CONTAINER_NAME /bin/$SHELL && exit 0
# Adding > /dev/null freezes when trying to enter dean-nginx, why?
# Try different shells if failed
if [ $SHELL != bash ]; then
echo Failed to enter using $SHELL, trying bash
docker exec -itu 0 $CONTAINER_NAME /bin/bash && exit 0
fi
if [ $SHELL != sh ]; then
echo Failed to enter using bash, trying sh
docker exec -itu 0 $CONTAINER_NAME /bin/sh && exit 0
fi
# Failed
err "Failed to enter $CONTAINER_NAME"