Reputation: 385
I am trying to run Terraform within a Docker container in my Windows machine. I am actually successfull in running Terraform within a Docker container. However I want to run the command 'terraform apply --auto-approve' instead of plain 'terraform apply' so that terraform dont ask for confirmation to apply the changes.
I am using AWS as the provider. I have a dockerfile of my own which I wrote and it uses hashicorp/terraform:light as the base image.
The following is my dockerfile:
# Use an official HashiCorp Terraform runtime as a base image.
FROM hashicorp/terraform:light AS base
#To create working directory.
WORKDIR /trojanwall-infra
# To add contents to the working directory.
ADD . /trojanwall-infra
# Copy the contents.
COPY . /trojanwall-infra
# To check the current version of Hashicorp Terraform.
# Use the base image to create a packaged image.
FROM base AS package
#To add to the working directory.
WORKDIR /root/.aws
# To copy the AWS credentials to root folder.
COPY ./Key/aws/config /root/.aws
COPY ./Key/aws/credentials /root/.aws
# Use the packaged image to create a final image.
FROM package AS final
#To add to the working directory.
WORKDIR /trojanwall-infra
# To Run Terraform init and initialize.
RUN terraform init
#RUN command inside the container.
CMD ["plan"]
CMD ["apply"]
The above dockerfile actually works, but it would ask 'yes'/'no' for confirmation while running as a Docker container. I would like to use it somewhat as the following:
#RUN command inside the container.
CMD ["apply", "--auto-approve"]
But it says that this is an invalid format. Guess dockerfile's CMD cannot run multiple commands this way.
Can anyone provide some insight and help me out?
Upvotes: 0
Views: 9868
Reputation: 385
I have fixed the issue using 'ENTRYPOINT' in the docker file itself. The reason why simple 'apply' and 'plan' works in the Docker file is because the command terraform
is set as 'ENTRYPOINT' in the base image itself (ie, hashicorp/terraform
).
Having that in mind, I created a file named 'run' (sipmply created a file with no extensions) in the filesystem and added the commands terraform plan
and terraform apply --auto-approve
into it and also added a new line of code 'ENTRYPOINT /trojanwall-infra/run'
into the '.dockerignore' file. Therefore, once the Docker container is created and run, if first fetch the 'run' shell file in the file system and run it and making '--auto-approve'
come into the picture.
Modified Dockerfile:
.dockerignore (Last section from the original one)
# Use the packaged image to create a final image.
FROM package AS final
#To add to the working directory.
WORKDIR /trojanwall-infra
# To Run Terraform init and initialize.
RUN terraform init
#RUN Terraform run command inside the container.
ENTRYPOINT /trojanwall-infra/run
run (File without an extension yet still acts like a shell file inside the container)
terraform plan
terraform apply --auto-approve
This was a new information for me and I hope this becomes useful for everyone who seeks answers.
Thanks guys.
Upvotes: 0
Reputation: 8605
What version of Terraform are you using? My version (0.14.8) doesn't have an --auto-apply
option, and I can't see it mentioned anywhere else on the web. I suspect you are meaning to use -auto-approve
, which should work the way you've tried:
# ...
# RUN command inside the container.
CMD ["apply", "-auto-approve"]
Upvotes: 1