-
Notifications
You must be signed in to change notification settings - Fork 0
/
readydocker.sh
executable file
·92 lines (77 loc) · 2.38 KB
/
readydocker.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
#!/usr/bin/env bash
###############################################################
# Ready Docker
###############################################################
#
# Ensure Docker.app is running and ready to bring containers up
#
# Designed to be used in environments where you want to bring
# up a container without human interaction.
#
# Example Usage:
#
# readydocker.sh && docker-compose up -d
#
# Expectations:
#
# - Docker should be located at /Applications/Docker.app
# - /tmp/ should be writable
# - docker command should be on $PATH
##############################################################
# Checks if another readydocker is running
##############################################################
lock_exists() { [[ -e /tmp/readydocker.lock ]]; }
##############################################################
# Adds lock
##############################################################
add_lock() { touch /tmp/readydocker.lock; }
##############################################################
# Removes a lock if it exists
##############################################################
remove_lock() {
if lock_exists ; then
rm /tmp/readydocker.lock
fi
}
###############################################################
# Runs when the script exits but only if lock has been obtained
###############################################################
exit_script() {
remove_lock
# Clear trap
trap - SIGINT SIGTERM
kill -- -$$
}
##############################################################
# Returns the status code of docker stats which will be 0
# if successful
##############################################################
is_docker_ready() { docker stats --no-stream >/dev/null 2>&1; }
# Check for another readydocker
if lock_exists ; then
echo "Waiting for another readydocker process to release lock"
# Wait until another process has released the lock
while lock_exists ; do
echo -n "."
sleep 1
done
# Final newline
echo ""
fi
# Register traps
trap exit_script SIGINT SIGTERM
trap remove_lock EXIT
# Adds lock to work with concurrent readydocker commands
add_lock
echo "Checking if Docker.app is ready"
if ! is_docker_ready ; then
echo "Starting Docker.app"
open /Applications/Docker.app
while ! is_docker_ready ; do
echo -n "."
sleep 1
done
# Final newline
echo ""
fi
echo "Docker.app is ready"