As developers, time is one of our most valuable resources. The repetitive setup steps involved in our daily workflows can quickly eat up time and energy that could be better spent writing code. In this post, I’ll show you how to automate essential tasks for your development process on macOS using Automator combined with shell scripting.
This guide will walk you through automating the following steps for a Docker-based project workflow:
• Start Docker Desktop
• Wait for Docker to be fully up and running
• Launch a project using Docker Compose
• Open a new terminal window to start your development environment
By automating these tasks, you’ll free up your time and streamline your development workflow.
Step 1: Setting Up the Environment
Before we dive into the automation process, let’s make sure you have the necessary tools installed:
• Docker Desktop: You’ll need Docker to run containers locally.
• iTerm2: While macOS Terminal is fine, I recommend using iTerm2, a more feature-rich terminal emulator that can make your development process more enjoyable.
• Automator: This built-in macOS tool allows you to create custom automation workflows without needing additional software.
• Node Version Manager (nvm): A tool to manage multiple Node.js versions on your machine, especially useful for projects that require specific versions.
Once you’ve installed these tools, we’ll create a simple automation script using Automator and a Bash script.
Step 2: Writing the Automation Script
Here’s the breakdown of our automation script:
1. Open Docker Desktop: Start Docker if it isn’t already running.
2. Check Docker Status: Wait until Docker is fully up and ready to use.
3. Launch Docker Compose: Automatically bring up your Docker containers using docker-compose.
4. Open iTerm2 and Start Dev Environment: Open a new terminal tab and run the necessary commands to launch your development server.
Here’s the Bash script to automate these tasks:
#!/bin/bash
# Open Docker Desktop application
open -a "Docker"
# Set a timeout (e.g., 30 attempts, waiting 10 seconds each time)
MAX_ATTEMPTS=30
attempt=1
# Function to check if Docker is running
is_docker_running() {
osascript -e 'tell application "System Events" to (name of processes) contains "Docker"'
}
# Wait for Docker to start
while ! is_docker_running; do
if [ $attempt -gt $MAX_ATTEMPTS ]; then
echo "Docker failed to start after $MAX_ATTEMPTS attempts. Exiting..."
exit 1
fi
echo "Waiting for Docker to start... Attempt $attempt/$MAX_ATTEMPTS"
attempt=$((attempt+1))
sleep 10
done
echo "Docker application is running. Waiting for Docker daemon to be ready..."
# Wait for Docker daemon to be ready
attempt=1
while ! /usr/local/bin/docker version >/dev/null 2>&1; do
if [ $attempt -gt $MAX_ATTEMPTS ]; then
echo "Docker daemon failed to be ready after $MAX_ATTEMPTS attempts. Exiting..."
exit 1
fi
echo "Waiting for Docker daemon to be ready... Attempt $attempt/$MAX_ATTEMPTS"
attempt=$((attempt+1))
sleep 10
done
echo "Docker is up and running."
# Export PATH to include paths for docker-compose and Docker
export PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/homebrew/bin
# Navigate to the project directory
cd /Users/your_username/your/project/folder/api-next || { echo "Failed to navigate to project directory"; exit 1; }
# Run Docker Compose
if docker-compose up -d; then
echo "Docker Compose started successfully."
else
echo "Failed to start Docker Compose. Exiting..."
exit 1
fi
# Open a new iTerm2 tab to run nvm use 18 && npm run dev
osascript <<EOF
tell application "iTerm2"
tell current window
create tab with default profile
tell current session
write text "cd /Users/your_username/your/project/folder/api-next && nvm use 18 && npm run dev"
end tell
end tell
end tell
EOF
echo "Opened a new iTerm2 tab to run 'nvm use 18 && npm run dev'"
This script will handle the entire process, ensuring that Docker is running, your containers are started, and your development environment is launched seamlessly.
Step 3: Setting Up Automator
Automator allows us to automate this process easily, enabling you to run this script with just a few clicks. Here’s how you can set it up:
1. Open Automator: Search for Automator in Spotlight and open it.
2. Create a New Document: Select “Application” as the document type, which allows you to run the automation like any other app.
3. Add a “Run Shell Script” Action: In Automator, search for “Run Shell Script” in the search bar and drag it to the workflow area.
4. Paste the Script: Copy and paste the Bash script you wrote earlier into the “Run Shell Script” action.
5. Save the Automation: Save the document with a meaningful name like “Docker Dev Automator”.
Now you can run your automation anytime you need by simply double-clicking the Automator application.
Step 4: Running the Automation
Every time you run the Automator app, it will:
• Open Docker Desktop
• Wait for Docker to be up and running
• Start your Docker containers using Docker Compose
• Open a new terminal tab in iTerm2 and start the development server
With this setup, you’ve automated the most tedious tasks in your workflow, ensuring you can focus on the code that matters.
Conclusion
By combining Automator and shell scripting, you can significantly streamline your development workflow on macOS. The script we created automates Docker startup, launches your containers, and opens your dev environment—all with a single click. This small automation can save you a lot of time and effort, letting you focus on writing code instead of managing your environment.
Automation not only improves efficiency but also ensures that your environment is consistent across different machines or sessions. Once you’ve set it up, you can work faster, with fewer interruptions, and without worrying about the manual steps in your process.
Happy coding, and enjoy the productivity boost!