如何使用 CMD 运行 terraform 在 docker 容器中应用 --auto-approve
How to run terraform apply --auto-approve in docker container using CMD
我正尝试在 Windows 机器的 Docker 容器中 运行 Terraform。
我实际上在 Docker 容器中 运行ning Terraform 中取得了成功。但是我想 运行 命令 'terraform apply --auto-approve' 而不是普通的 'terraform apply' 这样 terraform 就不会要求确认应用更改。
我使用 AWS 作为提供商。
我有一个自己编写的 dockerfile,它使用 hashicorp/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 实际上有效,但它会要求 'yes'/'no' 确认,而 运行 宁作为 Docker 容器。
我想按如下方式使用它:
#RUN command inside the container.
CMD ["apply", "--auto-approve"]
但是它说这是一个无效的格式。猜猜 dockerfile 的 CMD 不能运行 多个命令这样。
任何人都可以提供一些见解并帮助我吗?
您使用的是哪个版本的 Terraform?我的版本 (0.14.8) 没有 --auto-apply
选项,我在网络上的其他任何地方都看不到它被提及。我怀疑您打算使用 -auto-approve
,它应该按照您尝试过的方式工作:
# ...
# RUN command inside the container.
CMD ["apply", "-auto-approve"]
我已经使用 docker 文件中的“ENTRYPOINT”解决了这个问题。简单的'apply'和'plan'在Docker文件中起作用的原因是因为命令terraform
在基本图像本身中设置为 'ENTRYPOINT'(即 hashicorp/terraform
)。
考虑到这一点,我在文件系统中创建了一个名为“运行”的文件(简单地创建了一个没有扩展名的文件)并添加了命令 terraform plan
和 terraform apply --auto-approve
并在 '.dockerignore' 文件中添加了一行新代码 'ENTRYPOINT /trojanwall-infra/run'
。因此,一旦 Docker 容器被创建并且 运行,如果首先在文件系统中获取 'run' shell 文件并且 运行 它并使 '--auto-approve'
进入画面。
修改后的Docker文件:
.docker忽略(原文最后一节)
# 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
运行(文件没有扩展名,但在容器内仍然像 shell 文件)
terraform plan
terraform apply --auto-approve
这对我来说是一个新信息,我希望这对寻求答案的每个人都有用。
谢谢大家。
我正尝试在 Windows 机器的 Docker 容器中 运行 Terraform。 我实际上在 Docker 容器中 运行ning Terraform 中取得了成功。但是我想 运行 命令 'terraform apply --auto-approve' 而不是普通的 'terraform apply' 这样 terraform 就不会要求确认应用更改。
我使用 AWS 作为提供商。 我有一个自己编写的 dockerfile,它使用 hashicorp/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 实际上有效,但它会要求 'yes'/'no' 确认,而 运行 宁作为 Docker 容器。 我想按如下方式使用它:
#RUN command inside the container.
CMD ["apply", "--auto-approve"]
但是它说这是一个无效的格式。猜猜 dockerfile 的 CMD 不能运行 多个命令这样。
任何人都可以提供一些见解并帮助我吗?
您使用的是哪个版本的 Terraform?我的版本 (0.14.8) 没有 --auto-apply
选项,我在网络上的其他任何地方都看不到它被提及。我怀疑您打算使用 -auto-approve
,它应该按照您尝试过的方式工作:
# ...
# RUN command inside the container.
CMD ["apply", "-auto-approve"]
我已经使用 docker 文件中的“ENTRYPOINT”解决了这个问题。简单的'apply'和'plan'在Docker文件中起作用的原因是因为命令terraform
在基本图像本身中设置为 'ENTRYPOINT'(即 hashicorp/terraform
)。
考虑到这一点,我在文件系统中创建了一个名为“运行”的文件(简单地创建了一个没有扩展名的文件)并添加了命令 terraform plan
和 terraform apply --auto-approve
并在 '.dockerignore' 文件中添加了一行新代码 'ENTRYPOINT /trojanwall-infra/run'
。因此,一旦 Docker 容器被创建并且 运行,如果首先在文件系统中获取 'run' shell 文件并且 运行 它并使 '--auto-approve'
进入画面。
修改后的Docker文件:
.docker忽略(原文最后一节)
# 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
运行(文件没有扩展名,但在容器内仍然像 shell 文件)
terraform plan
terraform apply --auto-approve
这对我来说是一个新信息,我希望这对寻求答案的每个人都有用。
谢谢大家。