Reputation: 8397
I have been trying for 2 hours to rewrite this SSH remotelly executed command to multiline form to be more readable, but everytime I have something wrong. I tried multiple ways as described in similar questions but I just suck at BASH. I am trying to do
echo "Deploying $1 to remote"
sudo ssh -i ../keys/key.pem username@$2 '
id=$(docker ps -a -q -f name=$1); if [ -n "$id" ]; then docker rm --force $1; fi;
docker run -d --network host -v /var/log/$1/:/var/log/$1/
-e SERVER_PORT=80
--name $1 username/image:$1'
I want to run docker image remotelly with some arguments created remotelly ($id
) and some expanded locally before executing the script ($1, $2
).
Upvotes: 0
Views: 492
Reputation: 158812
You will probably find this easier to write if you write it as a standalone script:
#!/bin/sh
container="$1"
id=$(docker ps -aq -f name="$container")
if [ -n "$id" ]; then
...
fi
...
Run this by hand on the target system to make sure it does what you want.
Once you have that, you can copy the script and run it in two separate commands:
echo "Deploying $1 to remote"
scp -i ../keys/key.pem launch-container.sh "username@$2:/tmp/launch-container.sh"
ssh -i ../keys/key.pem "username@$2" sh /tmp/launch-container.sh "$1"
You also might look into various system-automation tools that are purpose-built for this kind of task. I'm partial to Ansible for not requiring a dedicated server and working over ssh in the same way you're showing; it has a set of Docker-related commands that can do this set of tasks fairly directly.
Upvotes: 2