polonskiy / crowdr

Crowdr is a tool for managing multiple Docker containers
MIT License
95 stars 9 forks source link

Option to run docker script in foreground #25

Closed CRTX closed 7 years ago

CRTX commented 7 years ago

Sometimes I have docker containers that are only supposed to run once and return a result. Right now it's not possible since it's forced to run in a background process.

Currently I have to do a two commands.

crowdr run crowdr exec php phpunit

Then I have to hack up a tail -f /dev/null inside my Dockerfile as the ENTRYPOINT to keep the container permanently running when it doesn't need to.

Can we have a flag to disable -d mode? I don't mind -d being the default mode as long as there's a way to disable it.

That way I could just do crowdr run php phpunit, get the output I need, get rid of my Dockerfile hack and run phpunit with just one command.

This would also save me from not having to manually enter a long env variable list that I need along with all the rest of the parameters that I'm currently manually sending to a docker run.

I would really appreciate if this feature could be added because I love crowdr!

polonskiy commented 7 years ago

Hi! Why do you use crowdr for such containers in first place? Crowdr is a simple orchestrator-like tool. Can you, please, provide your full config file?

CRTX commented 7 years ago

Yes. Although it helps me orchestrate single run foreground scripts.

Here's is a simple example of mine.

My crowdr config:

https://github.com/CRTX/AbstractFactory/blob/master/.crowdr/config.sh

But right now my travis-ci.org looks like this:

sudo: required

services:
    - docker

before_install:
    - phpenv config-rm xdebug.ini

install:
    - docker run --rm -v $(pwd):/app composer/composer install
    - sudo wget -O /usr/local/bin/crowdr https://raw.githubusercontent.com/polonskiy/crowdr/master/crowdr
    - sudo chmod +x /usr/local/bin/crowdr
    - crowdr build

script:
    - docker run -v $(pwd):/app --rm abstractfactory-php phpunit

after_success:
    - travis_retry docker run -e TRAVIS=$TRAVIS -e TRAVIS_JOB_ID=$TRAVIS_JOB_ID -v $(pwd):/app --rm abstractfactory-php coveralls -v

I have to use a long commands. I wish I could do:

script:
 - crowdr run php phpunit //I need to see foreground output

after_success:
 - travis_retry crowdr run php coveralls -v //need foreground output here too

These commands are easier to read but I can't see output. I also don't like I have to type env variables again. :(

I can only use crowdr for crowdr build right now.

polonskiy commented 7 years ago

What about this workaround?

entry.sh

#!/bin/bash
phpunit --version

.crowdr/config.sh

#!/bin/bash

crowdr_project="example01"
crowdr_name_format="%s-%s"

crowdr_config="

test1 image phpunit/phpunit
test1 volume $PWD/entry.sh:/entry.sh
test1 entrypoint /entry.sh
test1 after.run show_output

"

show_output() {
    docker logs -f $(crowdr_fullname test1)
}
$ crowdr run
example01-test1
PHPUnit 6.0.6 by Sebastian Bergmann, Julien Breux (Docker) and contributors.
CRTX commented 7 years ago

That's close. The only thing is that I can't run /bin/phpunit or /bin/coveralls separately at different times.

polonskiy commented 7 years ago

You can use multiple entrypoints:

entry1.sh

#!/bin/sh

yes $0 | head -5

entry2.sh

#!/bin/sh

yes $0 | head -5

Dockerfile

from alpine

copy entry* /
run chmod +x /entry*

.crowdr/config.sh

#!/bin/bash

crowdr_project="example01"
crowdr_name_format="%s-%s"

crowdr_config="

test1 build $PWD
test1 entrypoint /entry1.sh
test1 after.run show_output test1

test2 build $PWD
test2 entrypoint /entry2.sh
test2 after.run show_output test2

"

show_output() {
    docker logs -f $(crowdr_fullname $1)
}
CRTX commented 7 years ago

I might use this for small projects. But for larger projects, I would have 5-6+ entrypoint.sh files for every different /bin/*.bin program. Is this really the best way?

polonskiy commented 7 years ago

There is no best way. I think crowdr is a wrong tool for such jobs, at least for now. And I need to see more similar use cases, to implement proper solution.

The problem is not foreground mode - you can easily achieve it by adding test1 detach false in your crowdr config, but crowdr will discard your output anyway. You need a run command for a separate container, but it conflicts with crowdr's all or nothing design.

Feel free to modify crowdr for your needs.