0

How can one run docker commands from a bash (sh) script? I am trying to run:

#!/bin/bash
container= $(docker ps --format "{{.ID}} {{.Names}}" -a | grep testServer | awk '/'$imid'/ { print $1 }');
echo $container

but I get a blank for the container.

TIA

Casey Harrils
  • 2,793
  • 12
  • 52
  • 93

2 Answers2

1

It looks like the container id is printed to sterr and the $() construct only captures stdout.

As per this comment, you can use 2>&1 to redirect stderr to stdout, but I'm not quite sure how to work that into your snippet with the grep/awk chaining going on.

Community
  • 1
  • 1
captncraig
  • 22,118
  • 17
  • 108
  • 151
0

Did some testing - as suggested - and found that the following works from bash.

#!/bin/bash
container=$(docker ps --format "{{.ID}} {{.Names}}" -a | grep testServer  | awk '/'$imid'/ { print $1 }')
echo $container
if [[ $container = *[!\ ]* ]]; then
        echo "found something";
else
        echo "DID NOT find anything";
fi

I guess I just had to play around with it some more.

Pang
  • 9,564
  • 146
  • 81
  • 122
Casey Harrils
  • 2,793
  • 12
  • 52
  • 93