Docker container for Yandex Cloud infrastructure management with Terraform
This Docker container provides a ready-to-use environment for working with Yandex Cloud infrastructure using Terraform. It includes:
- Yandex Cloud CLI (
yc
) - Terraform with Yandex Cloud provider
- Pre-configured Terraform templates
- Essential tools (curl, jq, git, SSH, etc.)
- Docker installed on your system
- Yandex Cloud account with appropriate permissions
- OAuth token for Yandex Cloud API access
docker build -t yandex-terraform .
Create a directory for your Terraform configuration:
mkdir my-terraform-project
cd my-terraform-project
Get your credentials from Yandex Cloud Console:
- YC_TOKEN: OAuth token
- YC_CLOUD_ID: Cloud ID
- YC_FOLDER_ID: Folder ID
docker run -it --rm \
-e YC_TOKEN=your_oauth_token_here \
-e YC_CLOUD_ID=your_cloud_id_here \
-e YC_FOLDER_ID=your_folder_id_here \
-v $(pwd):/app \
yandex-terraform terraform [command]
# Initialize Terraform
docker run -it --rm \
-e YC_TOKEN=your_token \
-e YC_CLOUD_ID=your_cloud_id \
-e YC_FOLDER_ID=your_folder_id \
-v $(pwd):/app \
yandex-terraform terraform init
# Plan infrastructure changes
docker run -it --rm \
-e YC_TOKEN=your_token \
-e YC_CLOUD_ID=your_cloud_id \
-e YC_FOLDER_ID=your_folder_id \
-v $(pwd):/app \
yandex-terraform terraform plan
# Apply changes
docker run -it --rm \
-e YC_TOKEN=your_token \
-e YC_CLOUD_ID=your_cloud_id \
-e YC_FOLDER_ID=your_folder_id \
-v $(pwd):/app \
yandex-terraform terraform apply
# Destroy infrastructure
docker run -it --rm \
-e YC_TOKEN=your_token \
-e YC_CLOUD_ID=your_cloud_id \
-e YC_FOLDER_ID=your_folder_id \
-v $(pwd):/app \
yandex-terraform terraform destroy
# Check yc version
docker run -it --rm yandex-terraform yc version
# List available clouds
docker run -it --rm \
-e YC_TOKEN=your_token \
yandex-terraform yc resource-manager cloud list
# List compute instances
docker run -it --rm \
-e YC_TOKEN=your_token \
-e YC_CLOUD_ID=your_cloud_id \
-e YC_FOLDER_ID=your_folder_id \
yandex-terraform yc compute instance list
The container includes a default Terraform configuration:
main.tf:
terraform {
required_providers {
yandex = {
source = "yandex-cloud/yandex"
}
}
required_version = ">= 0.13"
}
provider "yandex" {
token = var.yc_token
zone = "ru-central1-a"
}
variables.tf:
variable "yc_token" {
description = "Yandex Cloud OAuth token"
type = string
sensitive = true
}
variable "cloud_id" {
description = "Yandex Cloud ID"
type = string
}
variable "folder_id" {
description = "Yandex Cloud Folder ID"
type = string
}
variable "zone" {
description = "Yandex Cloud default zone"
type = string
default = "ru-central1-a"
}
Variable | Description | Required |
---|---|---|
YC_TOKEN |
Yandex Cloud OAuth token | Yes |
YC_CLOUD_ID |
Yandex Cloud ID | No (but recommended) |
YC_FOLDER_ID |
Yandex Cloud Folder ID | No (but recommended) |
YC_ZONE |
Default zone (default: ru-central1-a) | No |
Mount your local directory to /app
in the container to persist Terraform state and configuration:
-v $(pwd):/app
- Never hardcode credentials in Dockerfiles or source code
- Use environment variables for sensitive data
- Use .gitignore to exclude sensitive files:
*.tfstate *.tfstate.backup .terraform/ terraform.tfvars .env
- "YC_TOKEN must be set" error: Ensure you pass the YC_TOKEN environment variable
- Permission errors: Check that your token has sufficient permissions in Yandex Cloud
- Network issues: Verify you can access Yandex Cloud APIs from your network
Run container with debug output:
docker run -it --rm \
-e YC_TOKEN=your_token \
-v $(pwd):/app \
yandex-terraform terraform plan -verbose
Add to your ~/.bashrc
or ~/.zshrc
:
alias yterraform='docker run -it --rm -e YC_TOKEN=$YC_TOKEN -e YC_CLOUD_ID=$YC_CLOUD_ID -e YC_FOLDER_ID=$YC_FOLDER_ID -v $(pwd):/app yandex-terraform'
Then use:
yterraform terraform plan
Create docker-compose.yml
:
version: '3.8'
services:
yc-terraform:
image: yandex-terraform
environment:
- YC_TOKEN=${YC_TOKEN}
- YC_CLOUD_ID=${YC_CLOUD_ID}
- YC_FOLDER_ID=${YC_FOLDER_ID}
volumes:
- .:/app
working_dir: /app
Use with:
docker-compose run --rm yc-terraform terraform plan
For Yandex Cloud specific issues, refer to:
For Docker issues, refer to Docker documentation and ensure your Docker installation is up to date.