From f1c6698b32a71a730ba3f5a73014508184c2921e Mon Sep 17 00:00:00 2001 From: Drew Date: Thu, 26 Nov 2020 23:15:54 -0600 Subject: [PATCH] Initial version of docker image --- .dockerignore | 1 + .gitignore | 0 Dockerfile | 26 ++++++++++++++++++++++++++ README.md | 22 ++++++++++++++++++++++ bin/tfw | 43 +++++++++++++++++++++++++++++++++++++++++++ pipeline.yaml | 0 6 files changed, 92 insertions(+) create mode 100644 .dockerignore create mode 100644 .gitignore create mode 100644 Dockerfile create mode 100644 README.md create mode 100755 bin/tfw create mode 100644 pipeline.yaml diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..5aa4358 --- /dev/null +++ b/.dockerignore @@ -0,0 +1 @@ +pipeline.yaml diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e69de29 diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..3048a34 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,26 @@ +FROM docker.io/debian:buster-slim +ENV DEBIAN_FRONTEND=noninteractive + +RUN apt-get update \ + && apt-get -y upgrade \ + && apt-get -y install --no-install-recommends \ + ca-certificates \ + curl \ + unzip \ + && apt-get clean \ + && rm -rf /var/lib/apt/lists/* + +ARG TERRAFORM_VERSION +ENV TERRAFORM_VERSION="${TERRAFORM_VERSION:-0.13.5}" +RUN useradd --create-home --shell /bin/bash terraform +USER terraform +COPY --chown=terraform:terraform . /home/terraform +RUN chmod +x /home/terraform/bin/* \ + && echo "export PATH=/home/terraform/bin:\${PATH}" >> "/home/terraform/.bashrc" \ + && /home/terraform/bin/tfw -version \ + && rm -f /home/terraform/.terraform/*.zip + +VOLUME /home/terraform/source +WORKDIR /home/terraform/source +ENTRYPOINT ["/home/terraform/bin/tfw"] +CMD ["-help"] diff --git a/README.md b/README.md new file mode 100644 index 0000000..3fd4b26 --- /dev/null +++ b/README.md @@ -0,0 +1,22 @@ +# Terraform Docker Image + +This repository is for building a container image that uses a simple terraform +wrapper to work on terraform files mounted in the `/home/terraform/source` +directory. It is also the image base for creating custom terraform images +with special plugins preloaded onto the image. + +## Building + +Create a container with a specific version of terraform + +```bash +docker build -t terraform:0.13.0 --build-arg TERRAFORM_VERSION=0.13.0 . +``` + +## Usage + +Mount the current directory as the terraform source and run the show command + +```bash +docker run -v $(pwd):/home/terrform/source terraform:0.13.0 show +``` diff --git a/bin/tfw b/bin/tfw new file mode 100755 index 0000000..0dfe974 --- /dev/null +++ b/bin/tfw @@ -0,0 +1,43 @@ +#!/usr/bin/env bash + +TERRAFORM_BIN_PATH="${TERRAFORM_BIN_PATH:-$HOME/.terraform}"; +TERRAFORM_VERSION=${TERRAFORM_VERSION:-"0.13.5"} + +platform='unknown' +platform_uname=$(uname) +if [[ "$platform_uname" == 'Linux' ]]; then + platform='linux' +elif [[ "$platform_uname" == 'FreeBSD' ]]; then + platform='freebsd' +elif [[ "$platform_uname" == 'Darwin' ]]; then + platform='darwin' +else + echo "[$platform_uname] is not recognized as a valid platform" + exit 1 +fi + +arch="unknown" +arch_uname=$(uname -m) +if [[ "$arch_uname" == 'x86_64' ]]; then + arch='amd64' +elif [[ "$arch_uname" == 'i686' ]]; then + arch='386' +else + echo "[$arch_uname] is not recognized as a valid arch" + exit 1 +fi + +TERRAFORM_URL="https://releases.hashicorp.com/terraform/${TERRAFORM_VERSION}/terraform_${TERRAFORM_VERSION}_${platform}_${arch}".zip +TERRAFORM_PATH="$TERRAFORM_BIN_PATH/$TERRAFORM_VERSION" +TERRAFORM_CMD="$TERRAFORM_PATH/terraform" +if ! type "$TERRAFORM_CMD" > /dev/null 2>&1; then + if [ ! -f "$TERRAFORM_PATH.zip" ]; then + echo "Downloading $TERRAFORM_URL" + mkdir -p "$TERRAFORM_PATH" + curl -ls "$TERRAFORM_URL" -o "$TERRAFORM_PATH.zip" + fi + cd "$TERRAFORM_PATH" && unzip "$TERRAFORM_PATH.zip" && cd - || exit 2 +fi + +# shellcheck disable=SC2068 +$TERRAFORM_CMD $@ diff --git a/pipeline.yaml b/pipeline.yaml new file mode 100644 index 0000000..e69de29