Skip to content

Instantly share code, notes, and snippets.

@eokoneyo
Last active January 31, 2020 15:47
Show Gist options
  • Save eokoneyo/ec4f68ec4306773bbe93fa0fcd93351b to your computer and use it in GitHub Desktop.
Save eokoneyo/ec4f68ec4306773bbe93fa0fcd93351b to your computer and use it in GitHub Desktop.
Expose external port of docker disposable container to .env file
#!/bin/bash
API_DOCKER_IMAGE_NAME="<image name>"
API_PORT=<port> #specify the port exposed in your dockerfile
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
ROOT="$(dirname "${SCRIPT_DIR}")"
if [[ $(docker ps --filter ancestor=${API_DOCKER_IMAGE_NAME} --format "{{.Status}}") ]]; then
#save name of running disposable for our image for reuse
RUNNING_DISPOSABLE=$(docker ps --filter ancestor=${API_DOCKER_IMAGE_NAME} --format "{{.Names}}")
API_URL=$(docker port ${RUNNING_DISPOSABLE} ${API_PORT})
# output api url to .env file to be used
if [ -f "${ROOT}/.env" ]; then
sed -i '' "s/^API_URL=.*/API_URL=$API_URL/g" ${ROOT}/.env
else
echo "API_URL=${API_URL}" >> ${ROOT}/.env
fi
else
echo "the docker image isnt running, start it and try again"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment