Add docker compose exec bash alias

If you’re frequently using the docker compose exec command and want a faster way to access your Docker containers, this guide will show you how to create a simple alias or function in your .zshrc file on macOS.

Step 1: open your .zshrc file

nano ~/.zshrc

Step 2: Add the de Function

Scroll to the bottom of the file and add the following function:

de() {
    if [ -z "$1" ]; then
        echo "Usage: de <container-name>"
        return 1
    fi
    docker compose exec -it "$1" bash
}

Save the file via Ctrl + X, press Y then enter.

Step 3: Reload shell configuration

Reload the .zshrc file so the new de command is immediately available:

source ~/.zshrc

Step 4: Use the New Command

de ogamex-app

This will execute:

docker compose exec -it ogamex-app bash
Published
Categorized as Bash, MacOS

By Leendert de Borst

Freelance software architect with 10+ years of experience. Expert in translating complex technical problems into creative & simple solutions.

Leave a comment

Your email address will not be published. Required fields are marked *