Skip to content

Instantly share code, notes, and snippets.

@cjlyth
Created June 17, 2014 01:58
Show Gist options
  • Save cjlyth/ed53bb60f4ee82e7aca8 to your computer and use it in GitHub Desktop.
Save cjlyth/ed53bb60f4ee82e7aca8 to your computer and use it in GitHub Desktop.
This gist can be used to serve a directory using the http-server nodejs module
#!/usr/bin/env bash
DATA_DIR="$( cd ${1:-.} && pwd )"
[[ -d "${DATA_DIR?}" ]] || {
echo "Unable to find directory: ${DATA_DIR}"
exit 1
}
CONTAINER_NAME="$(basename ${2:-${DATA_DIR}})"
docker restart ${CONTAINER_NAME?} &> /dev/null && {
echo "Docker container '${CONTAINER_NAME}' restarted"
} || {
echo "Running docker image 'cjlyth/http-server' as '${CONTAINER_NAME}'"
docker run -d -P \
--name="$CONTAINER_NAME" \
-v $DATA_DIR:/data \
cjlyth/http-server || {
docker stop $CONTAINER_NAME | xargs --no-run-if-empty docker rm
exit 2
}
}
docker port $CONTAINER_NAME 8080 | xargs --no-run-if-empty -I{} xdg-open http://{} 1>/dev/null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment