Skip to content
This repository has been archived by the owner on Oct 22, 2023. It is now read-only.

Behaviour of command without shell entrypoint is unexpected #52

Closed
binkley opened this issue Jan 14, 2019 · 5 comments
Closed

Behaviour of command without shell entrypoint is unexpected #52

binkley opened this issue Jan 14, 2019 · 5 comments

Comments

@binkley
Copy link
Contributor

binkley commented Jan 14, 2019

I've tried several YAML variations on quoting multi-line strings. Latest is:

    run:
      container: build-env
      command: './gradlew assemble
            && java -jar build/libs/docker-java11-0.0.1-SNAPSHOT.jar'

In each case, batect complains:

Task '&&' not found in root project 'docker-java11'.

I'm sure this is PEBKAC. What am I doing wrong?

@binkley
Copy link
Contributor Author

binkley commented Jan 14, 2019

I'm not sure this is a multiline issue. I was trying YAML multiline options from: https://yaml-multiline.info/

When I tried using a single-line, single-quoted string, I still had the same complaint:

  run:
    run:
      container: build-env
      command: './gradlew assemble && java -jar build/libs/docker-java11-0.0.1-SNAPSHOT.jar'

@binkley
Copy link
Contributor Author

binkley commented Jan 14, 2019

Ah, I get it now. Trying echo $0 $SHELL $- reveals that this is not passed to /bin/sh as I had expected. And recognizing the batect.yml file is for Docker Compose, I checked the docs on command. Those docs are not explicit, but I infer that the value of the command line in YAML is passed to the exec() call, so does not invoke /bin/sh.

The complaint about "task" comes from Gradle, not Batect.

This works, if annoying in it's verbosity:

  run:
    run:
      container: build-env
      command: '/bin/sh -c "./gradlew assemble && java -jar build/libs/docker-java11-0.0.1-SNAPSHOT.jar"'

A little more nicely:

  run:
    run:
      container: build-env
      command: >-
        /bin/sh -c './gradlew assemble
        && java -jar build/libs/docker-java11-0.0.1-SNAPSHOT.jar'

(Note: Do not indent further at "&&"; this changes the parsing somehow.)

@binkley
Copy link
Contributor Author

binkley commented Jan 14, 2019

@charleskorn I'm ok with closing this. Contra, it could be a documentation request. Since Docker Compose itself doesn't explain this well, I'm uncertain if Batect should, though it is a gotcha for the unaware.

@charleskorn
Copy link
Collaborator

Thanks for the feedback @binkley.

What you're seeing is the behaviour of ENTRYPOINT and CMD from Docker (see https://docs.docker.com/engine/reference/builder/#cmd) - I'm guessing the image you're using doesn't have an entrypoint set, and so the command you give is executed directly, rather than being interpreted by a shell (which would handle the && in the way you expect).

If you wanted to remove the need to use /bin/sh -c '...', you could set your entrypoint (in your Dockerfile) to execute sh. For example, add this to your Dockerfile:

ENTRYPOINT ["/bin/sh", "-c"]

(There's more explanation of this in the docs I linked to above.)

I think it's worthwhile adding some docs around this, as it's a common point of confusion - so thanks for the suggestion.

As a further aside, batect does not use Docker Compose internally - while similar, they are independent.

@charleskorn charleskorn changed the title Unable to quote multi-line command Behaviour of command without shell entrypoint is unexpected Jan 16, 2019
@charleskorn
Copy link
Collaborator

I've updated the docs with a note about this - thanks again for the feedback @binkley!

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

No branches or pull requests

2 participants