-1

When I do:

docker exec -ti myContainer /bin/bash

I have a new bash terminal on running container myContainer.

Now when I write

docker exec -ti lescompanions /bin/bash -c "echo youpi"

docker only outputs youpi and returns with no interactive terminal created. I was actually expecting docker to create the terminal and run echo youpi within the newly created terminal.

Where am I wrong?

---- EDIT ----

Now how shall I run echo youpi in the new terminal on the existing container and not have the terminal return to the host after the execution of echo youpi?

Stefano
  • 4,730
  • 1
  • 20
  • 28
arennuit
  • 855
  • 1
  • 7
  • 23
  • that's the way `bash -c 'whatever'` **always** works, even without docker: It runs the script passed to `-c`, then it exits. – Charles Duffy Nov 15 '17 at 18:14
  • 1
    Perhaps you should ask how to make bash do this *in general*, factoring Docker out of the question? Scoping it as a Docker question adds no value, when the real problem is generic to bash and can be reproduced without Docker even being installed. – Charles Duffy Nov 15 '17 at 18:15
  • so in the docker case, is there a way to create a new container terminal, have it execute a command, and hope the new terminal stays alive afterwards? – arennuit Nov 15 '17 at 18:15
  • What **exactly** are you asking? Do you want the copy of `bash` to persist after it runs the initial command? (This is a reasonable request, and I'm certain we already have Q&A covering it outside the scope of `docker`). Do you want the TTY created by Docker to be available for other commands even after the initial command exits? (If so, err, *why*? What value is there to that?) – Charles Duffy Nov 15 '17 at 18:17
  • 1
    It sounds like what you want, for instance, is already answered in the preexisting question [Invoke bash, run commands inside new shell, then give control back to user](https://stackoverflow.com/questions/7120426/invoke-bash-run-commands-inside-new-shell-then-give-control-back-to-user). – Charles Duffy Nov 15 '17 at 18:18

3 Answers3

1

You are not doing anything wrong, it's just how bash works. I've checked it on my Ubuntu (without Docker):

$ /bin/bash
(no output, bash instance running)

$ /bin/bash -c "echo youpi"
youpi
(bash finished)
LLL
  • 1,777
  • 1
  • 15
  • 31
  • All right. So is there a way to have **echo youpi** executed into my newly created terminal AND WITHOUT RETURNING? – arennuit Nov 15 '17 at 18:00
  • Remove `-c "echo youpi"` and it will stay running – mandark Nov 15 '17 at 18:08
  • Sure, but I would like to both create the new terminal on the container AND have it execute a command AND have the new terminal stay alive afterwards. Another way to put it is that I would like the host to execute a command in a container terminal... – arennuit Nov 15 '17 at 18:12
  • 1
    Most probably it's not what you want but a workaround would be: bash -c "echo youpi && read x". Of course instead of "read x" you can do sleep or execute another command. – LLL Nov 15 '17 at 18:16
1

docker exec takes a command to run a process inside the container while the -it flag attaches an interactive session against that process.

Your session will only live as long as the command given to exec

As others have mentioned the command /bin/bash -c "echo youpi" simply uses bash to run the command echo "echo youpi" and terminates, hence your terminal session terminates also.

stacksonstacks
  • 8,613
  • 6
  • 28
  • 44
0

echo youpi is a command that is run inside the container. The echo command prints the arguments given to it, which is youpi in this case, and that is also what you see.

mandark
  • 762
  • 5
  • 21
  • All right, now how shall I keep my new container terminal running rather than bash returning to the host after bash's execution? – arennuit Nov 15 '17 at 18:06
  • 2
    Try `-c "echo youpi && /bin/bash" ` – mandark Nov 15 '17 at 18:17
  • 1
    `-c "echo youpi && exec /bin/bash -li"`, rather. Otherwise you have two separate copies of bash in memory; the `exec` tells the current copy to replace itself. – Charles Duffy Nov 16 '17 at 02:39