Using Terraform to Create a Docker Volume and Network

Hands-On Lab

 

Photo of Travis Thomsen

Travis Thomsen

Course Development Director in Content

Length

01:00:00

Difficulty

Intermediate

We’ve been asked to deploy a MySQL container by our manager to the dev environment. He has the following requirements: We must use Terraform. The container needs to be on a private network. * The MySQL data should persist on its own volume.

What are Hands-On Labs?

Hands-On Labs are scenario-based learning environments where learners can practice without consequences. Don't compromise a system or waste money on expensive downloads. Practice real-world skills without the real-world risk, no assembly required.

Using Terraform to Create a Docker Volume and Network

The Scenario

We’ve been asked to deploy a MySQL container by our manager to the dev environment. He has the following requirements:

  • We must use Terraform.
  • The container needs to be on a private network.
  • The MySQL data should persist on its own volume.

Logging In

Use the IP address and credentials provided on the hands-on lab overview page, and log in with SSH to the server.

There's a fair bit of creating and editing files in this lab. All of the examples here involve using Vim, but use whichever text editor is most comfortable.

Create the Variables File

First, we're going to create variables.tf, where we will be adding four variables: container_name, mysql_root_password, mysql_network_name, and mysql_volume_name:

vi variables.tf

with these contents:

variable "container_name" {
  description = "The MySQL container name."
  default     = "mysql"
}

variable "mysql_root_password" {
  description = "The MySQL root password."
  default     = "P4sSw0rd0!"
}

variable "mysql_network_name" {
  description = "The MySQL's network'."
  default     = "mysql_internal_network"
}

variable "mysql_volume_name" {
  description = "The MySQL's Volume'."
  default     = "mysql_data"
}

Create the Image file

Now we'll create image.tf:

vi image.tf

Add the docker_image resource and call it mysql_image, then set the name to mysql:5.7:

resource "docker_image" "mysql_image" {
name = "mysql:5.7"
}

Create the Network file

Next we'll create network.tf:

vi network.tf

We need a few things in this one:

  • Add the docker_network resource and call it private_bridge_network.
  • Set the name to use the mysql_network_name variable.
  • Set the driver to bridge.
  • Set internal to true.

It should look like this when we're done:

resource "docker_network" "private_bridge_network" {
  name     = "${var.mysql_network_name}"
  driver   = "bridge"
  internal = true
}

Create the Volume file

We've got to specify persistent storage, so we'll create volume.tf:

vi volume.tf

with these contents:

resource "docker_volume" "mysql_data_volume" {
  name = "${var.mysql_volume_name}"
}

Create the Main file

The last file we'll create is main.tf:

vi main.tf

This one has quite a few moving parts. We've got to make sure these get done:

  • Add the docker_container resource and call it mysql_container.
  • Set the name to use the container_name variable.
  • Set the image to use the name of the image coming from docker_image.
  • Create an environment variable for MYSQL_ROOT_PASSWORD and set it to the mysql_root_password variable.
  • Configure the container volume to use the volume created by docker_volume and make sure the container_path is set to /var/lib/mysql.
  • The container needs to use the network created by docker_network.

When we're finished, it should look like this:

resource "docker_container" "mysl_container" {
  name  = "${var.container_name}"
  image = "${docker_image.mysql_image.name}"
  env   = [
    "MYSQL_ROOT_PASSWORD=${var.mysql_root_password}"
  ]
  volumes {
    volume_name    = "${docker_volume.mysql_data_volume.name}"
    container_path = "/var/lib/mysql"
  }
  networks_advanced {
    name    = "${docker_network.private_bridge_network.name}"
  }
}

Deploy the infrastructure

We'll initialize Terraform with this:

terraform init

The we must validate the files:

terraform validate

Next, we generate a Terraform plan:

terraform plan -out=tfplan

Finally, we need to deploy the infrastructure using the plan file:

terraform apply tfplan

Conclusion

Well, we've done it. We created a Docker volume and network using Terraform. We can check to see if the volume was set up by running a docker command:

docker volume inspect mysql_data

We can also look in the Docker volumes directory:

sudo ls /var/lib/docker/volumes/mysql_data/_data

This will show us our MySQL data files. Everything is in fact running properly. Congratulations!