Empower Your Docker Infrastructure: Automate, Secure, and Scale with the New Docker Terraform Provider
By Adesoji Alu
We’re thrilled to introduce the Docker Terraform Provider — a new tool designed to streamline the way users and organizations manage Docker-hosted resources. This includes managing repositories, configuring teams, and defining organization settings, all within Terraform’s infrastructure-as-code ecosystem. With this provider, Docker resources are now easier than ever to automate, secure, and scale, whether you’re managing a small project or a complex organizational setup.
A New Era of Working with Docker Hub
The Docker Terraform Provider presents a transformative way to interact with Docker Hub, bringing in the best practices of infrastructure-as-code that are foundational to cloud-native environments. Integrating Docker Hub with Terraform enables organizations to manage resources efficiently, improve security protocols, and collaborate more effectively across teams, all while synchronizing Docker components with other infrastructure.
The Challenge
For many teams, managing Docker Hub resources manually can be time-consuming and error-prone, especially as projects expand. Configurations managed manually often lead to inconsistencies, weakened security, and limited collaboration without version control. This is where the Docker Terraform Provider makes a difference, allowing Docker Hub resources to be managed as code, thus enhancing consistency, auditability, and automation.
The Solution
The Docker Terraform Provider introduces the following features:
- Unified Management: Use the same code and structure to manage Docker repositories, teams, users, and organizations across environments.
- Version Control: Changes to Docker Hub resources are tracked within your Terraform configuration, providing an auditable, version-controlled system for Docker infrastructure.
- Collaboration and Automation: Teams can seamlessly automate the provisioning and management of Docker Hub resources using Terraform, enhancing both productivity and adherence to best practices.
- Scalability: Whether managing a single repository or an entire organization, the Docker Terraform Provider effortlessly scales to meet your needs.
Example Use Case
At Docker, we faced challenges with managing Docker Hub resources, particularly when permissions were limited for creating repositories. The Docker Terraform Provider now empowers anyone in the company to create repositories directly from code without requiring elevated permissions on Docker Hub. By reducing these permissions and streamlining workflows, the new provider has boosted developer efficiency and strengthened security protocols.
Here’s an example Terraform configuration managing a repository, a team, team permissions, and a Personal Access Token (PAT):
terraform {
required_providers {
docker = {
source = "docker/docker"
version = "~> 0.2"
}
}
}
# Initialize provider
provider "docker" {}
# Define local variables for customization
locals {
namespace = "my-docker-namespace"
repo_name = "my-docker-repo"
org_name = "my-docker-org"
team_name = "my-team"
my_team_users = ["user1", "user2"]
token_label = "my-pat-token"
token_scopes = ["repo:read", "repo:write"]
permission = "admin"
}
# Create repository
resource "docker_hub_repository" "org_hub_repo" {
namespace = local.namespace
name = local.repo_name
description = "This is a generic Docker repository."
full_description = "Full description for the repository."
}
# Create team
resource "docker_org_team" "team" {
org_name = local.org_name
team_name = local.team_name
team_description = "Team description goes here."
}
# Team association
resource "docker_org_team_member" "team_membership" {
for_each = toset(local.my_team_users)
org_name = local.org_name
team_name = docker_org_team.team.team_name
user_name = each.value
}
# Create repository team permission
resource "docker_hub_repository_team_permission" "repo_permission" {
repo_id = docker_hub_repository.org_hub_repo.id
team_id = docker_org_team.team.id
permission = local.permission
}
# Create access token
resource "docker_access_token" "access_token" {
token_label = local.token_label
scopes = local.token_scopes
}
Future Work
Our journey with the Docker Terraform Provider has only just begun. Future updates will add support for other Docker services such as Docker Scout, Docker Build Cloud, and Testcontainers Cloud. These improvements will offer even more functionality, ensuring that as your Docker needs grow, the provider will evolve to meet them.
To stay updated, check out the official Docker Terraform Provider repository and track ongoing feedback through our issue tracker.
Get Started with the Docker Terraform Provider
We’re excited to see how this provider will empower teams to work more effectively with Docker Hub. By automating, securing, and scaling infrastructure, teams can stay focused on what matters most — building innovative software.
Ready to dive in? Here are some resources to get you started: