Back to blog

Install and use my Docker shortcuts on Linux

Install four Bash functions for Docker, load them from .bashrc, and restore your previous configuration using the installer's backups.

December 5, 20265 min read
Install and use my Docker shortcuts on Linux

For everyday Docker tasks, I use a few Bash functions to list my Compose projects, stop a project's containers, or start its services.

I have packaged these functions in a small installer so I no longer have to copy them into my .bashrc manually. It creates the functions file, adds the code that loads it, and backs up existing files.

Prerequisites

This tutorial is for Linux users with Bash 4 or later, curl, Docker, and the Docker Compose plugin.

Check your environment:

bash --version
curl --version
docker --version
docker compose version

Your user account must already be able to run Docker. The installer only configures the Bash shortcuts.

1. Download the installer

Download the script to a directory of your choice:

curl -fsSL \
  https://teddyjeanbaptiste.dev/scripts/install-docker-functions.sh \
  -o install-docker-functions.sh \
  && curl -fsSL \
  https://teddyjeanbaptiste.dev/scripts/install-docker-functions.sh.sha256 \
  -o install-docker-functions.sh.sha256

This command downloads the file without running it. You can then inspect its contents:

vim install-docker-functions.sh

If the download fails, stop and check the URL and error message before continuing.

2. Install the functions

Run the script with Bash, without sudo:

sha256sum -c install-docker-functions.sh.sha256 \
  && bash install-docker-functions.sh

Verification should display install-docker-functions.sh: OK. If it fails, && prevents execution: download both files again before retrying. The checksum is regenerated at build time from the script's bytes. It checks that the file matches the published hash; it does not guarantee authenticity if both the script and hash are compromised together.

Before changing any user configuration files, the script explains which files are affected, possible replacements, backups, and how to restore the previous configuration. It then asks:

Continuer l’installation ? [o/N]

The prompt is in French: it means “Continue installation?”. Enter o or oui (French for “yes”) to proceed. Pressing Enter, declining, or providing no response cancels the installation.

Quick option: download and run

If you choose to run the script without inspecting its code first, you can combine both operations:

curl -fsSL \
  https://teddyjeanbaptiste.dev/scripts/install-docker-functions.sh \
  -o install-docker-functions.sh \
  && curl -fsSL \
  https://teddyjeanbaptiste.dev/scripts/install-docker-functions.sh.sha256 \
  -o install-docker-functions.sh.sha256 \
  && sha256sum -c install-docker-functions.sh.sha256 \
  && bash install-docker-functions.sh

The && operators run Bash only if both downloads and SHA-256 verification succeed. Confirmation is still required before changes are made. It describes the script's intended actions; it does not replace reviewing the downloaded code. Do not use curl … | bash: this installer requires downloading it to a file.

Installed files

The installer creates or updates:

~/.config/bash/functions/docker.sh

It also adds this block to ~/.bashrc if it is not already present:

# Fonctions Docker
if [[ -f "$HOME/.config/bash/functions/docker.sh" ]]; then
    source "$HOME/.config/bash/functions/docker.sh"
fi

The comment is kept in French to match the block generated by the installer. The functions live in a separate file, loaded when you open an interactive Bash terminal.

If docker.sh already exists, its contents may be replaced. The previous version is backed up. If the script detects a different way of loading the same file, it stops to avoid adding a competing declaration.

When loaded, the functions file also removes aliases named dprojects, dstop, dstopall, and dup. If you have already customized these commands, review the backups and the loading order in your .bashrc.

3. Activate the commands

Load the file in your current terminal:

source "$HOME/.config/bash/functions/docker.sh"

Check that all four functions are available:

type dprojects dstop dstopall dup

Bash should identify each command as a function.

4. Use the shortcuts

CommandPurpose
dprojectsLists Compose projects with running containers.
dstop my-projectStops the running containers belonging to the specified project.
dstopallStops all running containers.
dupBuilds images and starts Compose services in the background.
dup my-serviceBuilds and starts a service, along with its dependencies according to the Compose configuration.

To find project names you can use with dstop:

dprojects

Then replace my-project with one of the returned names:

dstop my-project

If no running containers match, the function displays a message.

To use dup, switch to the directory containing your Compose file:

dup

This function runs:

docker compose up --build -d

It can therefore rebuild images and recreate containers.

Be careful with dstopall: it also stops containers that are not managed by Compose. All functions target the active Docker context, which you can check with:

docker context show

The stop functions do not remove containers or volumes.

5. Restore your previous configuration

On each installation, the script backs up the previous state of the files in a directory under:

~/.local/state/docker-functions/backups/

It then prints a restore command. Use the exact path shown in your terminal, in this form:

bash /path/shown/restore.sh

Restoring returns the files to their state before that installation. It can overwrite changes made since then, so review those changes before running it.

Then close the terminal and open a new one. Restoring the files does not remove functions that are already loaded in an open Bash session.