0

I am trying to ssh into server, and into a docker container to run the service. however I am not able to store containerId into a variable to pass it to enter the container.

#!/bin/bash

ssh test_server << EOF
 ls 
 sudo docker ps | grep 'tests_service_image' | colrm 13                 # This command works
 containerId=$(sudo docker ps | grep 'tests_service_image' | colrm 13)  # This doesn't
 sudo docker exec -i "$containerId" bash                           # Throws error since containerId is empty
 ./run.sh 
EOF

exit

2 Answers 2

1

The problem is that you are doing variable/function expansions on your own side. You need to escape those so that those expansions happen on the server side.

#!/bin/sh

ssh test_server << EOF
 containerId=\$(sudo docker ps | grep 'tests_service_image' | colrm 13)
 sudo docker exec -i "\$containerId" bash
 ./run.sh 
EOF

exit
Sign up to request clarification or add additional context in comments.

Comments

0

Edit: Pass it directly to docker exec command like so

sudo docker exec -i $(sudo docker ps | grep 'tests_service_image' | colrm 13) bash

Original Answer:

This is written assuming that the script execution is done post sshing into the server. but modified the answer to above based on the specific query

container ID is stored in variable containerId, you are getting the error Error: No such container: because you are passing a different variable $container instead of $containerId to docker exec command.

4 Comments

It's a typo, corrected it. Still doesn't work.
The error is container not found since containerId is empty when passed into sudo docker exec command
Do not store it in a seperate variable, pass it directly to docker exec command like so sudo docker exec -i $(sudo docker ps | grep 'tests_service_image' | colrm 13) bash
echo $containerid to ensure you have an actual id in the variable.

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.