Speed up your Docker workflow with Bash functions

Make your terminal work for you

Introduction

I am a web developer and I use docker-compose in all of my projects. Sometimes it is just one container for the frontend part of the app, sometimes it is the whole stack: client, api, database, proxy server.

The problem

Given the circumstances, I have to type many long commands to start up, manage and stop my stack. I would guess that I use some form of docker-compose at least 50 times a day, with the length of the command being 20-60 characters depending on the project. The history feature of Bash is not perfect either, pressing the up and down arrows until you find the command you are looking for requires more attention than you might expect.

Photo Soumil Kumar by on Pexels

The goal

I would like to substitute most of the commands I use by 2 or 3 character long mini-commands. I should choose short commands that you are not likely to use. I chose du, dud, ds, dd, da, dss and dus.

  • docker-compose up -d — starting the containers in detached mode
  • docker-compose stop — stopping the containers
  • docker-compose exec <servicename> <command> — attaching to a running service with a shell of our choice
  • docker-compose up <servicename> -d — starting a specific service in detached mode
  • docker-compose stop <servicename> — stopping a specific service

The solution

I am going to define my functions in the home directory, in a file named .bash_functions. This file should be sourced at the beginning of the terminal session, so I extend my .bashrc file with the following code:

if [ -f ~/.bash_functions ]; then
. ~/.bash_functions
fi
function set-compose-command {
COMPOSE_COMMAND="docker-compose"
if [ -f docker-compose.dev.yml ]
then
COMPOSE_COMMAND="docker-compose -f docker-compose.dev.yml"
fi
}
function dud {
set-compose-command
sh -c "$COMPOSE_COMMAND up -d"
}
function du {
set-compose-command
sh -c "$COMPOSE_COMMAND up"
}
function dd {
set-compose-command
sh -c "$COMPOSE_COMMAND down"
}
function ds {
set-compose-command
sh -c "$COMPOSE_COMMAND stop"
}
# Stop service
function dss {
if [ -z $1 ]; then
echo "Please specify a service."
return 1
fi
set-compose-command
sh -c "$COMPOSE_COMMAND stop $1"
}
# Start service
function dus {
if [ -z $1 ]; then
echo "Please specify a service."
return 1
fi
set-compose-command
sh -c "$COMPOSE_COMMAND up -d $1"
}
function da {
local service=${1:-dev}
local shell=${2:-sh}
set-compose-command
sh -c "$COMPOSE_COMMAND exec $service $shell"
}

Conclusion

My docker-compose workflow is now faster and also more comfortable. No more typing the same long commands over and over again.

den@workstation:~/work/myproject$ dud
Starting myproject_myservice_1 ... done
den@workstation:~/work/myproject$ da myservice
/usr/src/myproject # touch somefile.txt
/usr/src/myproject # ^D
den@workstation:~/work/myproject$ ds
Stopping myproject_myservice_1 ... done
den@workstation:~/work/myproject$
Photo by cottonbro on Pexels

React Developer, frontend enthusiast, student