Skip to content

Instantly share code, notes, and snippets.

@jomido
Last active June 6, 2017 13:06
Show Gist options
  • Save jomido/0651c6b6311f3975e0c37694e1de9252 to your computer and use it in GitHub Desktop.
Save jomido/0651c6b6311f3975e0c37694e1de9252 to your computer and use it in GitHub Desktop.
local docker dev off any image (like python:3.6-alpine)
#!/usr/bin/env bash
# BEGIN SANITY BOX------------------------------------------
set -e # immediately exit if any command fails |
set -u # immediately fail on undefined variables |
set -o pipefail # show error of last failed command |
IFS=$'\n\t' # control what word splitting means |
# END SANITY BOX--------------------------------------------
# usage:
# `./docker-dev <DIR_NAME> <IMAGE_NAME>`. This will:
# a) create a directory called <DIR_NAME> if it does not exist
# b) create a Dockerfile in there, if it doesn't exist
# c) apply <IMAGE_NAME> as the base image for the Dockerfile
# d) build a local image of the Dockerfile
# e) run the image, and:
# i) volume mount <DIR_NAME> to /app in the container
# ii) give you a terminal
# f) remove the container on exit
# This will let you edit code locally, but run that code inside the container.
# If the directory and Dockerfile already exist, then just do:
# `./docker-dev <DIR_NAME>`
# That will build and run the container at the terminal.
dir_name=$1
image_name=${2-python:3.6-alpine}
# template rendering function
render_template() {
eval "echo \"$(sed 's/\"/\\\\"/g' $1)\""
}
# create the directory if it doesn't exist
if [ ! -d "$dir_name" ]; then
mkdir $dir_name
fi
# render the Dockerfile from the template if it doesn't exist
if [ ! -f "$dir_name/Dockerfile" ]; then
render_template ./Dockerfile.template > ./$dir_name/Dockerfile
fi
# build image
docker build -t docker-dev-$dir_name ./$dir_name
# run terminal in container, mount local code into it, and remove container
# when it stops NOTE: /bin/bash might not be present on some images, so you
# may have to use something else.
docker run -it -v ${PWD}/$dir_name/:/app \
--rm \
--entrypoint /bin/bash \
docker-dev-$dir_name
FROM ${image_name}
WORKDIR /app
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment