主檔案
terraform {
required_providers {
docker = {
source = "kreuzwerker/docker"
}
google = {
source = "hashicorp/google"
}
random = {
source = "hashicorp/random"
}
}
}
provider "docker" {}
resource "docker_image" "nginx" {
name = "nginx:latest"
keep_locally = false
}
resource "docker_container" "nginx" {
image = docker_image.nginx.latest
name = "tutorial"
ports {
internal = 80
external = 8000
}
}
執行后terraform init,我面臨僅下載 docker 資源的問題:
Initializing the backend...
Initializing provider plugins...
- Finding latest version of hashicorp/google...
- Finding latest version of hashicorp/random...
- Finding latest version of kreuzwerker/docker...
- Installing hashicorp/google v4.11.0...
- Installed hashicorp/google v4.11.0 (signed by HashiCorp)
- Installing hashicorp/random v3.1.0...
- Installed hashicorp/random v3.1.0 (signed by HashiCorp)
?
│ Error: Failed to install provider
│
│ Error while installing kreuzwerker/docker v2.16.0: could not query provider registry for registry.terraform.io/kreuzwerker/docker: failed to retrieve authentication checksums for
│ provider: the request failed after 2 attempts, please try again later: Get
│ "https://github.com/kreuzwerker/terraform-provider-docker/releases/download/v2.16.0/terraform-provider-docker_2.16.0_SHA256SUMS": context deadline exceeded
在關注這篇文章之后,我在本地下載了檔案,我成功執行了 terraform init 但未能運行 terraform apply 并出現以下錯誤:
│ Error: Could not load plugin
│
│
│ Plugin reinitialization required. Please run "terraform init".
│
│ Plugins are external binaries that Terraform uses to access and manipulate
│ resources. The configuration provided requires plugins which can't be located,
│ don't satisfy the version constraints, or are otherwise incompatible.
│
│ Terraform automatically discovers provider requirements from your
│ configuration, including providers used in child modules. To see the
│ requirements and constraints, run "terraform providers".
│
│ failed to instantiate provider "registry.terraform.io/kreuzwerker/docker" to obtain schema: fork/exec
│ .terraform/providers/registry.terraform.io/kreuzwerker/docker/2.16.0/linux_amd64/terraform-provider-docker_2.16.0_linux_amd64.zip: permission denied
系統詳情:
作業系統:Ubuntu 21.10
嘗試了 Terraform 版本:
1.0.6 [在 https://learn.hashicorp.com/ 教程 cli 中使用的相同版本]
1.1.16 [使用 apt-get]
我還啟動了另一個 docker 容器并重現了該問題,并且我能夠使用 terraform init 重現相同的問題
更新:
gahan@jarvis:~/devOps/test$ terraform init
Initializing the backend...
Initializing provider plugins...
- Finding latest version of hashicorp/random...
- Finding latest version of kreuzwerker/docker...
- Finding latest version of hashicorp/google...
- Installing hashicorp/google v4.11.0...
- Installed hashicorp/google v4.11.0 (signed by HashiCorp)
- Installing hashicorp/random v3.1.0...
- Installed hashicorp/random v3.1.0 (signed by HashiCorp)
?
│ Error: Failed to install provider
│
│ Error while installing kreuzwerker/docker v2.16.0: could not query provider registry for registry.terraform.io/kreuzwerker/docker: failed to retrieve authentication checksums for provider: the request
│ failed after 2 attempts, please try again later: Get "https://github.com/kreuzwerker/terraform-provider-docker/releases/download/v2.16.0/terraform-provider-docker_2.16.0_SHA256SUMS": net/http: request
│ canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)
?
gahan@jarvis:~/devOps/test$ cat /etc/group | grep docker
docker:x:998:gahan
gahan@jarvis:~/devOps/test$ docker pull python:alpine3.13
alpine3.13: Pulling from library/python
5758d4e389a3: Pull complete
9292b3ab1647: Pull complete
35d95eb0acaf: Pull complete
cfda6539f3f2: Pull complete
f4471b8ea909: Pull complete
Digest: sha256:93eb0ba98b15791a071ec8bce2483e670e52c83af51962d3255b4f8f93b52d24
Status: Downloaded newer image for python:alpine3.13
docker.io/library/python:alpine3.13
uj5u.com熱心網友回復:
非常感謝@Tapan和其他社區成員的幫助。
由于我遵循了安裝后步驟的所有步驟,因此也重新迭代了權限..
最后,我還創建了一個 docker 容器來重新創建問題,并在進一步除錯時發現,即使wget可以下載命令報告的校驗和,但在處理 terraform 時,它可能正在使用其他一些我還不知道的 url/協議但它在某種程度上與我的 ISP [Airtel 光纖連接] 沖突。
結果,我使用了warp 桌面客戶端并嘗試使用對我有用的 terraform 命令,沒有任何問題。
uj5u.com熱心網友回復:
@Gahan,我想問題出在權限上,如果我沒記錯的話,docker 需要 sudo 訪問權限才能運行,而 terraform 與普通用戶訪問權限一起作業。如果您錯誤地查看以下行
.terraform/providers/registry.terraform.io/kreuzwerker/docker/2.16.0/linux_amd64/terraform-provider-docker_2.16.0_linux_amd64.zip: permission denied
它顯示“權限被拒絕”。
我想您可以嘗試將用戶 組從 sudo 更改為 .terraform 目錄的本地用戶并試一試。
chown <user>:<group> .terraform
chmod 750 .terraform/providers/registry.terraform.io/kreuzwerker/docker/2.16.0/linux_amd64/terraform-provider-docker_2.16.0_linux_amd64.zip
更新 :
此設定在 MacOS 上運行良好,我只是嘗試使用上面的 main.tf 內容在 docker 中創建 ngnix 容器,并且我能夠成功初始化和應用 terraform。我正在使用 Mac 作業系統。Terraform 版本 - 1.1.1,Docker 版本 - 4.4.2
接下來,我嘗試在 ubuntu 機器(20.04 LTS)上復制相同的內容,并在嘗試運行“terraform apply”時遇到了與上述類似的問題
錯誤:
Error: Error pinging Docker server: Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get "http:///var/run/docker.sock/_ping": dial unix /var/run/docker.sock: connect: permission denied
在進一步挖掘中,我能夠解決它。
問題的主要原因是我們的自定義用戶無權執行 docker 的命令。因此,當 terraform 嘗試運行 docker 相關命令時,會失敗。我們可以執行以下步驟來解決這個問題
- 如果您正確安裝了 docker,應該有一個可用的 docker 組。這你可以檢查 /etc/group 路徑
$ cat /etc/group
- 將您的用戶 ID 添加到 docker 組。在我的情況下,用戶名是“tapan1991”
$ sudo usermod -aG docker $User_Name
Eg : sudo usermod -aG docker tapan1991
從會話中注銷并再次登錄
執行 terraform plan/apply 命令
$ terraform apply
希望這可以幫助!!
參考:在“terraform apply”上 ping docker 服務器時出錯
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/435036.html
標籤:码头工人 ubuntu 地形 哈希公司 ubuntu-21.04
