Skip to content

Instantly share code, notes, and snippets.

@toolness
Last active March 1, 2017 16:31
Show Gist options
  • Select an option

  • Save toolness/81efec95ed9b4ae4e583b680f91d30b2 to your computer and use it in GitHub Desktop.

Select an option

Save toolness/81efec95ed9b4ae4e583b680f91d30b2 to your computer and use it in GitHub Desktop.
Docker shortcuts

This document just conventionalizes some practices laid out in 18F/calc#1406, making it a bit easier to run commands in docker containers without having to constantly prefix them with e.g. docker-compose run app.

Global setup

Using this convention requires first modifying one's .bashrc:

# Make project-specific docker shortcuts work
export PATH="./.docker-bin:$PATH"

You'll also want to add .docker-bin to your global gitignore.

First run git config --get core.excludesfile. If that outputs a filename, you'll want to edit that file. Otherwise, you'll want to run:

touch ~/.gitignore_global
git config --global core.excludesfile ~/.gitignore_global

Now edit your global gitignore file and add the line .docker-bin to it.

Per-project setup

For every docker-based project you want to create shortcuts for, create a directory called .docker-bin in the project's root directory. Populate it with scripts that just shell out to docker-compose.

For instance, if you frequently use jest in your project's app container, you can create the following script at ./docker-bin/jest:

#! /bin/sh

docker-compose run --rm app jest $*

Make this script executable via chmod +x .docker-bin/jest.

Now you can use jest from the root directory of your project's repository, and it will be "proxied" to your app container.

Alternatives

Adam Kendall noted that, if all your main containers across projects are called the same thing, it might be easier to just do a simple prefix with alias. For example, alias d='docker-compose run --rm app $*' would allow you to do d anycommand anyargs, which obviates the need for modifying one's PATH, global gitignore, and separate bin files for every tool.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment