visit
Let’s take another example, suppose you are developing something in any of the programming languages. Surely that utility or program will have some dependencies as well. While installing those dependencies on the local system, it can corrupt your complete system or package manager for dependency management. A decent example is Pip which is a dependency manager of Python 😉.
These are some example scenarios which we have faced actually and based on that we got the motivation for writing this blog.To resolve all this problem we just need one thing i.e. containers. I can also say docker as well but container and docker are two different things.
But yes for container management we use docker.So let’s go back to our first problem the terraform one. If we have to solve that problem there are multiple ways to solve this. But we tried it to solve this using Docker.As Docker says“Build Once and Run Anywhere”So based on this statement what we did, we created a Dockerfile for required Terraform version and stored it alongside the code. Basically our Dockerfile looks like this:-
FROM alpine:3.8
MAINTAINER OpsTree.com
ENV TERRAFORM_VERSION=0.11.10
ARG BASE_URL=//releases.hashicorp.com/terraform
RUN apk add --no-cache curl unzip bash \
&& curl -fsSL -o /tmp/terraform.zip ${BASE_URL}/${TERRAFORM_VERSION}/terraform_${TERRAFORM_VERSION}_linux_amd64.zip \
&& unzip /tmp/terraform.zip -d /usr/bin/
WORKDIR /opstree/terraform
USER opstree
In this Dockerfile, we are defining the version of Terraform which needs to run the code.
In a similar fashion, all other above listed problems can be solved using Docker. We just have to create a Dockerfile with exact dependencies that are needed and that same file can work in various environments and projects.
IMAGE_TAG=latest
build-image:
docker build -t opstree/terraform:${IMAGE_TAG} -f Dockerfile .
run-container:
docker run -itd --name terraform -v ~/.ssh:/root/.ssh/ -v ~/.aws:/root/.aws -v ${PWD}:/opstree/terraform opstree/terraform:${IMAGE_TAG}
plan-infra:
docker exec -t terraform bash -c "terraform plan"
create-infra:
docker exec -t terraform bash -c "terraform apply -auto-approve"
destroy-infra:
docker exec -t terraform bash -c "terraform destroy -auto-approve"