0

First: I have searched the forum and also went through documentation, but still cannot get it right.

So, I have a docker command I want to run on a remote server, from my bash script. I want to pass an environment variable – on the local machine running the script – to the remote server. Furthermore, I need a response from the remote command.

Here is what I actually am trying to do and what I need: the script is a tiny wrapper around our Traefik/Docker/Elixir/Phoenix app setup to be able to connect easily to the running Elixir application, inside the Erlang observer. With the script, the steps would be:

  • ssh into the remote machine
  • docker ps to see all running containers, since in our blue/green deploy the active one changes name
  • docker exec into the correct container
  • execute a command inside the docker container to connect to the running Elixir application

The command I am using now is:

CONTAINER=$(ssh -q $USER@$IP 'sudo docker ps --format "{{.Names}}" | grep ""$APP_NAME"" | head -n 1')

The main problem is the part with the grep and the ENV var... It is empty, and does not get replaced. It makes sence, since that var does not exist on the remote machine, it does on my local machine. I tried single quotes, $(), ... Either it just does not work, or the solutions I find online execute the command but then I have no way of getting the container name, which I need for the subsequent command:

ssh -o 'RequestTTY force'  $USER@$IP "sudo docker exec -i -t $CONTAINER /bin/bash -c './bin/app remote'"

Thanks for your input!

2
  • Restructuring this to avoid docker exec would avoid a layer of quoting; so would avoiding the sh -c inside the docker exec command. Do you want that just to be the container's default command, so you can docker run your-image without doing manual steps afterwards? Commented Oct 14, 2020 at 14:52
  • Hmm I like your proposal, but the Docker container already has a default command to start the web app when started, can you put a different command into the Dockerfile? Commented Oct 14, 2020 at 14:55

3 Answers 3

1

First, are you sure you need to call sudo docker stop? as stopping the containers did not seem to be part of the workflow you mentioned. [edit: not applicable anymore]

Basically, you use a double-double-quote, grep ""$APP_NAME"", but it seems this variable is not substituted (as the whole command 'sudo docker ps …' is singled-quoted); according to your question, this variable is available locally, but not on the remote machine, so you may try writing:

CONTAINER=$(ssh -q $USER@$IP 'f() { sudo docker ps --format "{{.Names}}" | grep "$1" | head -n 1; }; f "'"$APP_NAME"'"')
Sign up to request clarification or add additional context in comments.

2 Comments

You are right about the stop, I updated my original question, it was a copy/paste error, it should have stopped after the head -n 1 of course.
@JeroenBourgois OK! I updated my post as well. Did you test the command I suggested?
1

You can try this single command :

ssh -t $USER@$IP "docker exec -it \$(docker ps -a -q --filter Name=/$APP_NAME) bash -c './bin/app remote'"

Comments

0

You will need to redirect the command with the local environmental variable (APP_NAME) into the ssh command using <<< and so:

   CONTAINER=$(ssh -q $USER@$IP <<< 'sudo docker ps --format "{{.Names}}" | grep "$APP_NAME" | head -n 1 | xargs -I{} sudo docker stop {}')

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.