Skip to content

Instantly share code, notes, and snippets.

@okaufmann
Last active April 21, 2023 08:45
Show Gist options
  • Save okaufmann/64c4273287b618270bf4bf9451393b53 to your computer and use it in GitHub Desktop.
Save okaufmann/64c4273287b618270bf4bf9451393b53 to your computer and use it in GitHub Desktop.
The d, A simple docker-compose wrapper.

The d

A simple wrapper arond docker-compose for local docker development inspired by https://github.com/shipping-docker/vessel.

Why another wrapper

I like to write as less possible. Especially for my local toolchain. As I often use Laravel one of my favourite shortcuts is a for php artisan. So I created the d. Unlike Vessel, I needed a wrapper that could be used in any project, and not just Laravel.

Install

Download using your favorite command:

curl -sL https://gist.githubusercontent.com/okaufmann/64c4273287b618270bf4bf9451393b53/raw/d -o d
# or
wget https://gist.githubusercontent.com/okaufmann/64c4273287b618270bf4bf9451393b53/raw/d -qO d

Then make it executable with chmod +x d

Alias I recommend the following function in your .bashrc or .zshrc so you can just type d over ./d:

function d() {
    command ./d "$@"
}

You can paste the following bash code to temporarily enable the alias:

function d() {
    command ./d "$@"
}

export -f d

Usage

The d searches for the file docker/local/docker-compose.yml. There should be at least one service called app in it.

The d is used to forward all commands to the container. composer install or further commands can be executed simply by using

d composer install

Unfortunately, there are a few commands that can be both Linux and docker-compose commands such as rm, ps, kill, top, exec, help. To execute these commands, you can simply mark them with a leading dot so that they are treated as docker-compose * commands. For example, to delete a container, you can use

d.rm app

To forward commands to a container other than app, you can overwrite the name with the environment variable C.

An example:

C=redis d bash

tasty

#!/usr/bin/env bash
# The d
# A simple wrapper for the docker-compose command.
# https://gist.github.com/okaufmann/64c4273287b618270bf4bf9451393b53
projectName=$(basename "$PWD")
# rewrite php artisan shortcut
if [ "$1" = "a" ]; then
set -- "php" "artisan" "${@:2}"
fi
dockerComposeFilePath=./docker/local/docker-compose.yml
# check whether we should run a docker compose command...
# (conflicting commands will be ignored: rm, ps, kill, top, exec, help)
if [[ "$1" =~ ^(build|bundle|config|create|down|events|images|logs|pause|port|pull|push|restart|run|scale|start|stop|unpause|up|version)$ ]]; then
docker compose -p $projectName -f "$dockerComposeFilePath" "$@"
elif [[ "$1" =~ ^(.rm|.ps|.kill|.top|.exec|.help) ]]; then
# rewrite conflicting commands to its original one
firstArg="$1";
set -- "${firstArg:1}" "${@:2}"
docker compose -p $projectName -f "$dockerComposeFilePath" "$@"
else
# ...or forward the command to the container
containerName=${C:-app}
docker compose -p $projectName -f "$dockerComposeFilePath" exec $containerName "$@"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment