【发布时间】:2021-08-01 05:40:45
【问题描述】:
我正在尝试在我的 Windows 机器的 Docker 容器中运行 Terraform。 我实际上成功地在 Docker 容器中运行 Terraform。但是,我想运行命令 'terraform apply --auto-approve' 而不是普通的 'terraform apply',这样 terraform 就不会要求确认以应用更改。
我使用 AWS 作为提供商。 我有一个自己编写的 dockerfile,它使用 hashcorp/terraform:light 作为基础镜像。
以下是我的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"]
上述 dockerfile 确实有效,但在作为 Docker 容器运行时会询问“是”/“否”以进行确认。 我想在某种程度上使用它:
#RUN command inside the container.
CMD ["apply", "--auto-approve"]
但它说这是一种无效的格式。猜猜dockerfile的CMD不能这样运行多个命令。
谁能提供一些见解并帮助我?
【问题讨论】:
-
apply可以在不添加terraform的情况下工作吗,例如CMD ["terraform", "apply", "-auto-approve"]之类的东西? -
我不这么认为。实际上,如果我们正常运行 Terraform,我们可以运行
terraform apply -auto-approve,但不要认为在容器内运行时会出现这种情况。当我按照您的建议运行上述内容时,出现以下错误:来自发件人的错误:无法匹配排除模式:模式中的语法错误 -
不看定义,我猜
hashicorp/terraform运行时容器将terraform设置为它的入口点,所以你不需要将它包含在CMD 中。 -
您的问题与 terraform 自动应用无关 - 错误来自其他地方
-
嘿,我已经解决了这个问题。我会单独留下答案。
标签: docker dockerfile terraform devops terraform-provider-aws