Skip to content

Commit

Permalink
Add command and change README to have some instructions
Browse files Browse the repository at this point in the history
  • Loading branch information
Cam Spiers committed Jan 2, 2020
1 parent 903237a commit da5c2a1
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 2 deletions.
21 changes: 19 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,19 @@
# readydocker
A macOS tool for starting then waiting for Docker.app to be ready
# Ready Docker

Ready Docker is a macOS tool for starting then waiting for Docker.app to be ready. It is designed to be used in environments where you want to bring up a container without human interaction.

## Example Usage:

```bash
readydocker && docker-compose up -d
```

## Expectations

- Docker should be located at /Applications/Docker.app
- /tmp/ should be writable
- docker command should be on $PATH

## Install

Place `readydocker` script in a folder on your $PATH.
92 changes: 92 additions & 0 deletions readydocker
Original file line number Diff line number Diff line change
@@ -0,0 +1,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 && 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"

0 comments on commit da5c2a1

Please sign in to comment.