Skip to content

Instantly share code, notes, and snippets.

@mraxus
Last active October 10, 2016 09:22
Show Gist options
  • Save mraxus/c305e6c5e92d960efec0adc902c65421 to your computer and use it in GitHub Desktop.
Save mraxus/c305e6c5e92d960efec0adc902c65421 to your computer and use it in GitHub Desktop.
Bash script to translate docker-compose service name with hostname:port
#!/bin/bash
########## C O N F I G ##########
DOCKER_HOST_NAME=localhost
#################################
cmd="curl $@"
service_name=`echo $cmd | sed -n 's/.* \([a-zA-Z0-9_][a-zA-Z0-9_]*\)\/.*/\1/p'`
row=`docker-compose ps |grep _${service_name}_1`
### Check if the service does exist in the current project
if [[ $row == "" ]]; then
echo "There is no service called '$service_name'";
exit -1;
fi
status=`echo $row | awk '{print $3}'`
### Check if the service is running
if [[ $status != "Up" ]]; then
status=`echo $row | awk '{print $4}'`
### Check if the service is running
if [[ $status != "Up" ]]; then
echo "Service is not running";
exit -1;
fi
port=`echo $row | awk '{print $5}'`; port=${port:8:5}
else
port=`echo $row | awk '{print $4}'`; port=${port:8:5}
fi
real_host="${DOCKER_HOST_NAME}:$port"
### Build the find/replace strings with leading space and trailing / for a 'more exact' string match
IFS="¢"
find=" ${service_name}/"
replace=" ${real_host}/"
unset IFS
echo $real_host
cmd=${cmd/${find}/${replace}}
### Run the translated curl command
$cmd
@mraxus
Copy link
Author

mraxus commented May 30, 2016

Running ./durl.sh users/v1/users/1 will be translated into curl docker:32878/v1/users/1

What the script does is to translate the users hostname into the docker host + port (in the example docker:32878)

I have added an alias so that I can just run durl xxx.

@mraxus
Copy link
Author

mraxus commented May 30, 2016

As an addition docker-machine ip default could be added to find the DOCKER_HOST_NAME as well

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment