One-off Docker-Compose commands

2018-01-29 | #docker, #webdev

Docker-Compose defines a collection of services as individual containers, which all start simultaneously with a predefined command on "docker-compose up" (or "docker-compose up servicename" to start a specific service).

But sometimes you want to execute something different then the default command in a container, to install a new dependency and write it to a project lock file for example.

We could execute something like this using

docker-compose exec servicename bash -c "install something"

but this requires, that the container is already running (by having run "docker-compose up" beforehand for example).

If you'd like to start the individual container, execute a custom command and remove the container automatically afterwards, you should rather be doing this:

docker-compose run --rm servicename bash -c "install something"

The "--rm" parameter automatically throws away the container on shutdown.