Reputation: 1704
I have this docker container to run terraform.
alias terraform='docker run -i -t -v ~/.aws:/root/.aws:ro -v $(pwd):/app -w /app/ rubendob/terraform:0.11.8'
is just a copy of the official terraform image. Nothing fancy.
FROM golang:alpine
MAINTAINER "HashiCorp Terraform Team <[email protected]>"
ENV TERRAFORM_VERSION=0.11.8
RUN apk add --update git bash openssh
ENV TF_DEV=true
ENV TF_RELEASE=true
WORKDIR $GOPATH/src/github.com/hashicorp/terraform
RUN git clone https://github.com/hashicorp/terraform.git ./ && \
git checkout v${TERRAFORM_VERSION} && \
/bin/bash scripts/build.sh
RUN rm -rf /var/lib/apt/lists/*
WORKDIR $GOPATH
ENTRYPOINT ["terraform"]
So I called this way:
alias terraform='docker run -i -t -v ~/.aws:/root/.aws:ro -v $(pwd):/app -w /app/ rubendob/terraform:0.11.8'
Then I have the next folder structure and it was working ok since ups, I decided to run some terraform stuff in the dev folder.
ls -ls tf
total 0
0 drwxr-xr-x 3 ruben.ortiz staff 96 15 sep 23:43 dev
0 drwxr-xr-x 6 ruben.ortiz staff 192 11 sep 19:53 modules
0 drwxr-xr-x 4 ruben.ortiz staff 128 15 sep 12:39 prod
I ran the container like
terraform plan tf/prod/
and worked ok but container created then the .terraform folder with tfstate, and other stuff.
So if I want to run the same command but to dev environment simply cannot because it detects and previous .terraform folder
ls -lisah tf/.terraform/
total 8
901814 0 drwxr-xr-x 5 ruben.ortiz staff 160B 15 sep 12:38 .
885805 0 drwxr-xr-x 6 ruben.ortiz staff 192B 15 sep 23:54 ..
901815 0 drwxr-xr-x 15 ruben.ortiz staff 480B 16 sep 00:05 modules
901821 0 drwxr-xr-x 3 ruben.ortiz staff 96B 10 sep 23:02 plugins
901819 8 -rw-r--r-- 1 ruben.ortiz staff 567B 16 sep 18:43 terraform.tfstate
And if I enter into the dev folder, as I just set up a volume to the current directory it is not able to see the shared modules folder.
How do you do guys to workaround this?
Thanks!
Upvotes: 0
Views: 1827
Reputation: 212
I have to agree with the comments here. I would encourage you to re-evaluate the benefits you are gaining from this process.
That being said, the reason it’s causing conflicts is because you are trying to invoke 2 different workspaces from a common directory. You can avoid this by overriding the working directory when you enter the container (see https://docs.docker.com/engine/reference/run/#workdir) or simply changing directory to the correct context.
I would also suggest you try an alternative to managing environments using different workspaces.
Upvotes: 3