12

I'm having an issue storing the output of docker run -it -d -p 43211:3000 --name appname -h hostname -v $PWD/local_dir:/root/remote_dir repo/imagename in a BASH varibale. I tried `backticks`, I also tried running it like the official docs say BASH_VAR=$(docker run ...), I even tried storing the output in a file with docker run --...>$FILE_DESCRIPTOR, but no luck storing the error situation, the situation when the name is already used by another container, like so:

$ FATA[0000] Error response from daemon: Conflict. The name "appname" is already in use by container 7c84d8d703c8. You have to delete (or rename) that container to be able to reuse that name.

I want to say that it works for the success situation, so I'm able to store in BASH_VAR the full container ID, upon running the application successfully, but unfortunately this solves only half the problem I'm facing.

Any help would be appreciated.

Thanks!

4
  • 5
    does output=$(docker run -it -d -p 43211:3000 --name appname -h hostname -v $PWD/local_dir:/root/remote_dir repo/imagename 2>&1) work for you? 2>&1 redirects stderr to stdout. Commented Mar 15, 2015 at 19:26
  • Thank you, kind sir! Yes, @kek2mgl, it worked like a charm! The reason I need this is because I'm building an interactive docker container management bash script, with Dialog and I need to parse the output of all commands I issue behind the scenes. You can answer the question so I can mark your comment as being the right answer. Commented Mar 15, 2015 at 19:54
  • I have bash script like this '#!/usr/bin/bash result=$(docker run -e 'SITE=WL' -h pingnet --name pingnet --rm pingnet 2>&1) echo ${result}' and doesnt display any result Commented Mar 25, 2016 at 23:32
  • Maybe the issue comes from the non interactivity of your terminal. You can try with the command script to emulate an interactive terminal. Commented Nov 5, 2017 at 20:37

3 Answers 3

8

What you need is to capture standard error and store it in a variable.

Using

BASH_VAR=$(command)

or

BASH_VAR=`command`

will capture standard output and not standard error.

This is the right syntax to store standard error messages in a variable:

BASH_VAR=$( { command; } 2>&1 )
Sign up to request clarification or add additional context in comments.

Comments

0

You could use a while loop to read each line of output as it's produced and do something with it that way.

while read -r line_of_output; do
  echo "line: $line_of_output"

done < <(docker ... 2>&1)

Also, if you're writing an interactive script, you might want to check out the select bash builtin

$ help select
select: select NAME [in WORDS ... ;] do COMMANDS; done
     Select words from a list and execute commands.

Comments

0

you might need to break out of the while loop , but yes it is possible, example:

  MYSQL_IMAGE_NAME='mysql/mysql-server:5.7'
  docker pull "$MYSQL_IMAGE_NAME"
  docker images
  docker run --name=mysql-server-container -d "$MYSQL_IMAGE_NAME"
  docker container ls

  while read -r line ; do
     GENERATED_PWD="$(echo $line | grep 'GENERATED ROOT PASSWORD'|awk '{print $5}')"
     test -z $GENERATED_PWD || break
  done < <(docker logs --tail 3000 --follow mysql-server-container)
  echo "GENERATED_PWD: $GENERATED_PWD"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.