1

I have a shell script which runs as follows :

image_id=$(docker ps -a | grep postgres | awk -F' ' '{print $1}')
full_id=$(docker ps -a --no-trunc -q | grep $image_id)
docker exec -i -t $full_id bash

When I run this from the base linux OS, I expect to actually enter the postgres container which is a running container. But the issue is that the shell script hangs on 3rd line during ' docker exec' step.

My end goal is using the bash script, enter a running postgres container and run another bash script inside that container. However the same command when I run it from command line, it works fine and gets me into the postgres container.

Please help, I have spent hours and hours to solve this but no progress.

Thanks again

1
  • What's the final command you want to run in the container? Commented Nov 27, 2015 at 1:54

1 Answer 1

2

Your setup is a bit more complex than it needs to be.

Docker ps can filter containers directly with the --filter= option

docker ps --no-trunc --quiet --filter="ancestor=postgres"

You can also --name containers when you run them which will be less fraught with danger than the script you are attempting

docker run --detach --name postgres_whatever postgres
docker exec -ti postgres_whatever bash

I'm not sure that your script is hanging as opposed to sitting there waiting for input. Try running a command directly

Using naming

exec_test.sh

#!/usr/bin/env bash
docker exec postgres_whatever echo "I have run the test"

When run

$ ./exec_test.sh
I have run the test

Without naming

exec_filter_test.sh

#!/usr/bin/env bash
id=$(docker ps --no-trunc --quiet --filter="ancestor=postgres")
[ -z "$id" ] && echo "no id" && exit 1
docker exec "${id}" echo "I have run the test"

When run

$ ./exec_filter_test.sh 
I have run the test
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Matt. That really helped .

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.