首页 > 解决方案 > 如何使用 CMD 在 docker 容器中运行 terraform apply --auto-approve

问题描述

我正在尝试在我的 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 不能以这种方式运行多个命令。

任何人都可以提供一些见解并帮助我吗?

标签: dockerdockerfileterraformdevopsterraform-provider-aws

解决方案


您使用的是什么版本的 Terraform?我的版本 (0.14.8) 没有--auto-apply选项,我在网络上的其他任何地方都看不到它。我怀疑你的意思是使用-auto-approve,它应该按照你尝试的方式工作:

# ...

# RUN command inside the container.
CMD ["apply", "-auto-approve"] 

推荐阅读