How to build docker images , containers and docker services with Terraform using docker provider.

Docker has been a vital and very important tool for deploying your web applications securely and because of light weighted technology and they way it works has captured the market very well. Although some of the steps are manual but after deployment docker make things look very simple.

But can we automate even before the deployment takes place? So here comes terraform into play which is a infrastructure as a code tool which automate docker related work such as creation of images, containers and service with few commands.

In this tutorial we will see what is docker , terraform and how can we use docker provider in terraform to automate docker images and containers.

Table of content

  1. What is Docker?
  2. What is Terraform?
  3. How to Install terraform on ubuntu machine.
  4. What is Docker provider?
  5. Create Docker Image, containers and docker service using docker provider on AWS using terraform
  6. Conclusion

What is docker ?

Docker is an open source tool for developing , shipping and running applications. It has ability to run applications in loosely isolated environment using containers. Docker is an application which helps in management of containers in a very smooth and effective way. In containers you can isolate your applications. Docker is quite similar to virtual machine but it is light weighted and can be ported easily.

Containers are light weighted as they are independent of hypervisors load and configuration. They directly connect with machines ie. hosts kernel.

Prerequisites

  • Ubuntu machine preferably 18.04 version + , if you don’t have any machine you can create a ec2 instance on AWS account
  • Recommended to have 4GB RAM
  • At least 5GB of drive space
  • Ubuntu machine should have IAM role attached with full access of ec2 instance or it is always great to have administrator permissions to work with terraform demo.

You may incur a small charge for creating an EC2 instance on Amazon Managed Web Service.

What is Terraform?

Terraform is a tool for building , versioning and changing the infrastructure. Terraform is Written in GO Language and the syntax language of configuration files is hcl which stands for HashiCorp configuration language which is much easier than yaml or json.

Terraform has been in use for quite a while now . I would say its an amazing tool to build , change the infrastructure in very effective and simpler way. It’s used with variety of cloud provider such as Amazon AWS, Oracle, Microsoft Azure , Google cloud and many more. I hope you would love to learn it and utilize it.

How to Install Terraform on Ubuntu 18.04 LTS

  • Update your already existing system packages.
sudo apt update
  • Download the latest version of terraform in opt directory
wget https://releases.hashicorp.com/terraform/0.13.0/terraform_0.13.0_linux_amd64.zip
  • Install zip package which will be required to unzip
sudo apt-get install zip -y
  • unzip the Terraform download zip file
unzip terraform*.zip
  • Move the executable to executable directory
sudo mv terraform /usr/local/bin
  • Verify the terraform by checking terraform command and version of terraform
terraform               # To check if terraform is installed 

terraform -version      # To check the terraform version  
  • This confirms that terraform has been successfully installed on ubuntu 18.04 machine.

What is Docker provider in terraform?

Docker provider helps to connect with docker images and docker containers using docker API. So in case of terraform , we would need to configure docker provider so that terraform can work with docker images and containers.

There are different ways in which docker provider can be configured. Lets see some of them now.

  • Using docker host’s hostname
provider "docker" {
  host = "tcp://localhost:2376/"
}
  • Using dockers IP address
provider "docker" {
  host = "tcp://127.0.0.1:2376/"
}
  • In case your docker host is remote machine
provider "docker" {
  host = "ssh://user@remote-host:22"
}
  • Using docker socket

unix:///var/run/docker.sock is a Unix socket the docker daemon listens on. Using this Unix socket you can connect with multiple docker images and containers.

This Unix socket is also used when containers need to communicate with docker daemon such as during the mount binding.

provider "docker" {                             
host = "unix:///var/run/docker.sock"
    }

Create a Docker Image, container and service using docker provider on AWS using terraform

Let us first understand terraform configuration files before we start creating files for our demo.

  • main.tf : This file contains actual terraform code to create service or any particular resource
  • vars.tf : This file is used to define variable types and optionally set the values.
  • output.tf: This file contains the output of resource we wish to store. The output are displayed
  • terraform.tfvars: This file contains the actual values of variables which we created in vars.tf
  • provider.tf: This file is very important as we provide information to terraform regarding on which cloud provider it needs to execute the code

Now, In below demo we will create docker image , container , services using docker provider. Now Lets configure terraform files which are needed for this demo. In this demo we only need one file that is main.tf to start with.

main.tf

provider "docker" {                                 # Create a Docker Provider
host = "unix:///var/run/docker.sock"
 }

resource "docker_image" "ubuntu" {                  # Create a Docker Image
    name = "ubuntu:latest"
}

resource "docker_container" "my_container" {        # Creates a Docker Container
  image = docker_image.ubuntu.latest         # Using same image which we created earlier
  name = "my_container"
}

resource "docker_service" "my_service" {            # Create a Docker Service
  name = "myservice"
  task_spec {
   container_spec {
     image = docker_image.ubuntu.latest     # Using same image which we created eearlier
    }
   }
  endpoint_spec {
    ports {
     target_port = "8080"
       }
    }
}
  • Now your files and code are ready for execution . Initialize the terraform
terraform init
  • Terraform initialized successfully ,now its time to run the terraform plan command.
  • Terraform plan is a sort of a blueprint before deployment to confirm if correct resources are being provisioned or deleted.
terraform plan

NOTE:

If you intend to create Docker service on same machine without having multiple nodes please run below command first so that our docker service gets created successfully using terraform

docker swarm init
  • After verification , now its time to actually deploy the code using apply.
terraform apply
  • Now lets verify all the three components one by one if they are created successfully using terraform with docker provider.
  • Verify docker image
docker images
  • Verify docker containers
docker ps -a
  • Verify docker service
docker service ps 

Conclusion:

In this tutorial we will see what is docker , terraform and how can we use docker provider in terraform to automate docker images and containers.

Hope this tutorial will help you in understanding the terraform and provisioning the docker components using terraform. Please share with your friends if you find it useful.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s