1

I'm trying to automate something that should be a fairly simple task, but I can't find the right flag / way to do it.

I'm automating a bunch of docker containers, and I want to know which shell I'm using in some human readable form.

So, I start/create a container:

docker run -v /home/igor/Downloads:/home/Downloads/ -h debian7 -p 8081:8080 -itd --name igorDebian72 debian:7

And then trying to get into its bash which has nicely changed prompt and colours:

sudo docker exec -ti igorDebian72 /bin/bash -c "`export PS1="\e[1;32m[\u@ \\H \\W \\@]\$ \e[m "`"

But, this seems to "run" the command and exit back to host, so it's not useful.

If I run these two things separately:

sudo docker exec -ti igorDebian72 /bin/bash 

This get's me into the containers bash, and then:

export PS1="\e[1;32m[\u@ \\H \\W \\@]\$ \e[m "

Gives me exactly what I want. Bash prompt with visible container name and a different colour than host machine.

How to achieve that in a single line?

P.S. In a meantime and using comments from here I've tried this:

sudo docker exec -ti igorDebian72 bash --rcfile  <(echo 'export PS1="\e[1;32m[\u@ \\H \\W \\@]\$ \e[m "')

sudo docker exec -ti igorDebian72 bash --rcfile  <(export PS1="\e[1;32m[\u@ \\H \\W \\@]\$ \e[m ")

It remained in bash, but seems like the script was not executed.

I tried saving it in a .sh file, then doing this:

sudo docker exec -ti igorDebian72 bash --rcfile <(echo trueColors.sh)

Again, remains in the container, but does not run the script.

Balkyto
  • 1,460
  • 4
  • 22
  • 47
  • 1
    surprised this is marked as a duplicate when that existing question has no ties to docker – dtc Jun 30 '17 at 00:21
  • This question is not a duplicate, and it would actually be very helpful to know the answer to it, because the other solution doesn't seem to work with Docker. – bosnjak Mar 11 '19 at 12:45

1 Answers1

0

The docker exec command aims is to run a process into a container then exit when the process is done.

In the first case, you are running /bin/bash -c 'export ....' who end after import is done. This is why you are exiting from the terminal.

In the second one, you are running /bin/bash who exit only when you close the terminal. You will not be able to do both in the same line. What you can do is :

  • Putting the PS1=.... into the dockerfile as  ENV PS1 \e.... so the environment variable is already set when you run the container

  • Then use docker exec /bin/bash to enter the container and run command into it manually

Titouan Freville
  • 489
  • 4
  • 13
  • OP is using the `-ti` flag on `docker exec`, so it allocates a tty and will run interactively. So whilst your first statement is true, it doesn't apply to this case. – arco444 Jan 12 '17 at 10:48
  • Yes it applies. As you said, `-it` allocate a tty and allow to interact with it but it doesn't mean that the process will be maintain after the command was done (try it by running a basic ubuntu container, it will exit after command was well executed) – Titouan Freville Jan 12 '17 at 10:54
  • The reason the process is not being maintained is because the wrong bash command is being used. Using OPs bash command has the same behaviour irrespective of whether its being run via docker or not, i.e. it exits immediately after the command is run – arco444 Jan 12 '17 at 11:00
  • I don't really get what you mean then. (I'm not very knowledgeable in linux system). Though I read the issue you past in comment from the question, and it's a better answer than mine ^^ – Titouan Freville Jan 12 '17 at 11:08
  • 1
    Err. If I add it to a dockerfile, and then run it manually, that does not differ at all from what I'm doing right now. :) So it seems that there's no way to set these things easily? – Balkyto Jan 12 '17 at 14:18
  • @Balkyto go check the subject from arco444. It is the best way to do what you wish. – Titouan Freville Jan 12 '17 at 14:19
  • As reminder : use `bash --rcfile ` instead of `/bin/bash -c` – Titouan Freville Jan 12 '17 at 14:20
  • Tried, managed to stuck my terminal only – Balkyto Jan 12 '17 at 14:39