容器内的Terraform删除文件

时间:2019-04-22 23:00:46

标签: docker terraform ubuntu-18.04

我发现我的Ubuntu 18.04主机上的docker没有保留安装到主机上容器中的文件。

图片:hashicorp/terraform

我正在使用--mount将目录绑定到容器中,该目录是terraform配置文件的存储位置。然后,我执行容器,该容器执行terraform,然后写入其状态文件和所有其他内容。
在所有情况下,这些文件中有70%无法在容器中保存下来。我可以看到它们在容器运行时在主机上创建,但是当容器完成其工作时,文件就会消失。
那是码头工人还是地形问题?

添加更多信息:

docker run --mount type=bind,source='/home/david/demo',target=/demo -w /demo -it hashicorp/terraform plan -out tfstate

terraform version 
0.11.13
docker version
Client:
 Version:           18.09.5
 API version:       1.39
 Go version:        go1.10.8
 Git commit:        e8ff056
 Built:             Thu Apr 11 04:43:57 2019
 OS/Arch:           linux/amd64
 Experimental:      false

Server: Docker Engine - Community
 Engine:
  Version:          18.09.5
  API version:      1.39 (minimum version 1.12)
  Go version:       go1.10.8
  Git commit:       e8ff056
  Built:            Thu Apr 11 04:10:53 2019
  OS/Arch:          linux/amd64
  Experimental:     false

1 个答案:

答案 0 :(得分:0)

在阅读完有关您问题的所有评论后,我将总结一下我如何测试您的情况和结果

我的docker版本:

Docker version 18.09.1, build 4c52b90

地形:

Terraform v0.11.13
+ provider.azurerm v1.24.0

我创建了一个文件夹,其中包含具有以下配置的main.tf文件:

provider "azurerm" {
    version = "=1.24.0"
    subscription_id = "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"
    client_id = "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"
    client_secret = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX="
    tenant_id = "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"
}

resource "azurerm_resource_group" "rg" {
    name = "testResourceGroup"
    location = "westus"
}

我在代理后面,所以我已经执行了第一时间,因为我要连接到azure,所以我使用init来下载插件:

docker run --env HTTPS_PROXY="http://myproxyfqdn:port" --rm --mount type=bind,source='/Docker/NFS/terraform',target='/terraform' -w /terraform -it hashicorp/terraform:full init

执行完此命令后,主机上的文件夹将刷新,并使用插件创建.terraform文件夹:

# ls -ltra
-rw-r--r-- 1 root root  759 Apr 23 09:00 main.tf
drwxr-xr-x 3 root root 4096 Apr 23 09:09 .terraform

然后我使用-out参数执行了计划,该计划创建了计划文件供以后使用:

# docker run --env HTTPS_PROXY="http://myproxyfqdn:port" --rm --mount type=bind,source='/Docker/NFS/terraform',target='/terraform' -w /terraform -it hashicorp/terraform:full plan -out testplan
Refreshing Terraform state in-memory prior to plan...
The refreshed state will be used to calculate this plan, but will not be
persisted to local or remote state storage.


------------------------------------------------------------------------

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  + azurerm_resource_group.rg
      id:               <computed>
      location:         "westus"
      name:             "testResourceGroup"
      tags.%:           <computed>

Plan: 1 to add, 0 to change, 0 to destroy.
------------------------------------------------------------------------

那在我的文件夹中创建了计划文件:

# ls -ltra
-rw-r--r-- 1 root root  759 Apr 23 09:00 main.tf
drwxr-xr-x 3 root root 4096 Apr 23 09:09 .terraform
-rw-r--r-- 1 root root 5291 Apr 23 09:11 testplan

然后应用创建的terraform.tfstate计划:

# docker run --env HTTPS_PROXY="http://myproxyfqdn:port" --rm --mount type=bind,source='/Docker/NFS/terraform',target='/terraform' -w /terraform -it hashicorp/terraform:full apply testplan
azurerm_resource_group.rg: Creating...
  location: "" => "westus"
  name:     "" => "testResourceGroup"
  tags.%:   "" => "<computed>"
azurerm_resource_group.rg: Creation complete after 2s (ID: /subscriptions/8d43a801-58b6-4dde-84cc-...c60e6/resourceGroups/testResourceGroup)

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

主机文件夹上的更新:

# ls -ltra
-rw-r--r-- 1 root root  759 Apr 23 09:00 main.tf
drwxr-xr-x 3 root root 4096 Apr 23 09:09 .terraform
-rw-r--r-- 1 root root 5291 Apr 23 09:11 testplan
-rw-r--r-- 1 root root 3748 Apr 23 09:11 terraform.tfstate

我没有问题,每次执行都更新了主机文件夹中的数据。