Last active
January 31, 2020 15:47
-
-
Save eokoneyo/ec4f68ec4306773bbe93fa0fcd93351b to your computer and use it in GitHub Desktop.
Expose external port of docker disposable container to .env file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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