8000 import with docker workfkow by Geoelycom · Pull Request #4 · Geoelycom/Terraform · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

import with docker workfkow #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions terraform_imports/.terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions terraform_imports/docker.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Terraform configuration
# Configuration-driven import relies on the import block, which has two required arguments:

# - resource_type: The type of resource to import, in this case, docker_container.
# - resource_name: The name of the resource to import, in this case, my_container.

import {
id = "305226eb8001ec8363cf1c2500b63566651362a7dbd45ada90b7163e33d8d215"
to = docker_container.web
}

# generating configuration with the cli is better than manually defining the resources by using
# terraform plan -generate-config-out=generated.tf flag.

# The import block is used to import existing resources into Terraform state. The id argument specifies the ID of the resource to import, and the to argument specifies the name of the resource in the Terraform configuration.

resource "docker_image" "nginx" {
name = "nginx:latest"
}
26 changes: 26 additions & 0 deletions terraform_imports/generated.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# __generated__ by Terraform
# Please review these resources and move them into your main configuration files.

# __generated__ by Terraform from "305226eb8001ec8363cf1c2500b63566651362a7dbd45ada90b7163e33d8d215"
resource "docker_container" "web" {
env = []
image = docker_image.nginx.image_id
name = "hashicorp-import"
ports {
external = 8081
internal = 80
ip = "0.0.0.0"
protocol = "tcp"
}
}

# its possible to bring some resources under terraform management by using the terraform import command. this is often the case for resources defined by a single unique ID or Tag, such as Docker images

# terraform import docker_container.web 305226eb8001ec8363cf1c2500b63566651362a7dbd45ada90b7163e33d8d215

# In the above generated.tf file, the docker_container.web resource specifies the SHA256 hash ID of the image used to create the container. This is how Docker stores the image ID internally, so the import operation loaded the image ID directly into your state. However, identifying the image by its tag or name would make your configuration easier to understand.
# but we can make it easily readable by using the image name instead of the sha256 hash. To do this, we can use the docker_image data source to look up the image by its name and tag. The data source will return the SHA256 hash ID of the image, which we can then use in our docker_container resource.
# we retrieve it with the following command:
# docker image inspect -f {{.RepoTags}} `docker inspect --format="{{.Image}}" hashicorp-import`

# include it into the resources block : name: "nginx:latest"
3 changes: 3 additions & 0 deletions terraform_imports/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
provider "docker" {

}
19 changes: 19 additions & 0 deletions terraform_imports/terraform.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
terraform {
/* Uncomment this block to use Terraform Cloud for this tutorial
cloud {
organization = "organization-name"
workspaces {
name = "learn-terraform-import"
}
}
*/

required_providers {
docker = {
source = "kreuzwerker/docker"
version = "~> 3.0.2"
}
}

required_version = "~> 1.5"
}
0