Taylor Vance
Taylor Vance

Reputation: 1001

Does docker-compose have anything comparable to npm scripts, for executing container commands on demand?

I have a docker compose project with, among others, a "php-container". I often need to execute commands on that running container, for example $ docker exec -it php-container ../vendor/phpunit/phpunit/phpunit tests/.

Is there any way to do that with less typing? Ideally, I would define an alias in my docker-compose.yml, attach it to the php-container, and simply type $ docker do testmystuff. That would, in this example, run unit tests on the php-container, as if I had typed the full exec command from the first paragraph.

Something like this (simplified structure to get the point across--I realize there are design issues with this "schema").

# docker-compose.yml

# ...

services:
  php:
    container-name: php-container

# ...

usercommands:
  php-container:
    testmystuff: "../vendor/phpunit/phpunit/phpunit tests/"
    render-something: "./mycontainerscript render more-args-here"
    bash-php: "bash"

Upvotes: 1

Views: 64

Answers (1)

neonwatty
neonwatty

Reputation: 346

Not that I'm aware of - though that could be cool.

How about a parameterized system alias or bash script in the meantime?

Something like

#!/bin/bash

CONTAINER=$1
TEST1=$2
TEST2=$3

docker exec ${CONTAINER} ${TEST1}
docker exec ${CONTAINER} ${TEST2}

Say this was test.sh then you could save a few keystrokes as

bash test.sh php-container <TEST1> <TEST2>

If your container and / or tests had stable names then this could become even more of a keystroke saver.

Your test.sh would express the entire exec command for a single test

#!/bin/bash

docker exec php-container ../vendor/phpunit/phpunit/phpunit tests/

and all you're left with is

bash test.sh

Upvotes: 1

Related Questions