0

I have been looking to the matrix of interaction of CMD and ENTRYPOINT and I can't found my way to have container running an entrypoint THEN a cmd with multiple commands

version: '3.8'

services:
  test:
    image: debian:buster-slim
    entrypoint: [ "/entrypoint.sh" ]
    volumes:
      - ./entrypoint.sh:/entrypoint.sh
    command: [ "echo" ,"toto","&&","echo","tutu" ]

where entrypoint.sh is a file containing :

#!/bin/bash
set -e
set -x
echo tata
exec "$@"

"should" print

tata
toto
tutu

but it's printing

tata
toto && echo tutu

I found a solution by replacing [ "echo" ,"toto","&&","echo","tutu" ] by "bash -c 'echo toto && echo tutu'" and then it work.

but I don't get why the first method do not work since the documentation say it will do :

exec_entry p1_entry /bin/sh -c exec_cmd p1_cmd

Antonio Petricca
  • 8,891
  • 5
  • 36
  • 74
raphaelauv
  • 670
  • 1
  • 11
  • 22
  • Does this answer your question? [CMD doesn't run after ENTRYPOINT in Dockerfile](https://stackoverflow.com/questions/54447913/cmd-doesnt-run-after-entrypoint-in-dockerfile) – Max Nov 02 '21 at 14:34
  • No as said I have took to the matrix of interaction of CMD and ENTRYPOINT , it seams that it's not working as espected – raphaelauv Nov 03 '21 at 15:05

1 Answers1

2

The problem is generated by the exec command, which synopsis is:

exec [command [argument...]]

so it will only accept one command with multiple arguments.

Solution:

The solution is the one that you pointed out, by using sh -c '':

services:
  test:
    image: debian:buster-slim
    entrypoint: [ "/entrypoint.sh" ]
    volumes:
      - ./entrypoint.sh:/entrypoint.sh
    command: ["sh", "-c", "echo toto && echo tutu"]

because the final result will satisfy the exec command with one command and multiple arguments


On docker side, the official documentation explains the ENTRYPOINT vs CMD very well with this table:

docker table

source

If you combine CMD and ENTRYPOINT in the array context the result would be /entrypoint.sh "echo" "toto" "&&" "echo" "tutu" because each parameter of the CMD will be a parameter for the ENTRYPOINT

Here's the output of the example above executed directly in the terminal:

# ./entrypoint.sh "echo" "toto" "&&" "echo" "tutu"
+ echo tata
tata
+ exec echo toto '&&' echo tutu
toto && echo tutu

And this is the result of the docker-compose up

# docker-compose up
test_1  | + echo tata
test_1  | tata
test_1  | + exec echo toto '&&' echo tutu
test_1  | toto && echo tutu
root_test_1 exited with code 0

As you can see each parameter is passed in the array form so the '&&' is parsed as a string (note the single quotes).

Note:

The result you expected is this one:

# ./entrypoint.sh echo toto && echo tutu
+ echo tata
tata
+ exec echo toto
toto
tutu

In this scenario as you see the only parameter passed to the exec is the first echo toto.

echo tutu is executed in bash terminal after ./entrypoint.sh script exits.

Obviously if this would be parsed by docker as a separate command it will never be executed because the ENTRYPOINT will exit before the echo tutu command.

adripo
  • 38
  • 5