One of the first things that we need to look at configuring is our
Terraform providers. These providers give us an interface to the
container or cloud provider that we are working with.
So rather than having to build everything inside of Terraform itself,
we can have this provider or interface to our cloud
providers such as AWS or Google Cloud.
We can go through and browse these at the website
registry.terraform.io. We're going to be using our Docker provider, so
we can search for the Docker provider from the Terraform site.
Once we've got the basic usage,
we can then go through and add it to our main.tf, our
configuration file that Terraform is going to use.
So when working with our providers,
we can make sure that we've got the underlying infrastructure that's
needed, so we're making sure then that we've got the package docker.io
installed. We're going to be using Docker in our next module, so it
kind of makes sense that we've got it up and running now in any case.
We can go through and make sure that our local user is added to the
Docker group.
This saves me having to use sudo with Docker. We can exit and log
in again to pick up the new group membership. I must log out and
log in again to pick up that membership.
Once I've done that,
I can go through and create myself a working directory and move into
that directory, and we can create our Terraform file.
These Terraform files are written in a JSON format.
Once we've created the file, we can use terraform init to go
through and read the format, and then if we've provided our
provider details, it will go through and make sure the provider is
downloaded before we do anything more.
When we take a look at that Terraform file,
we start our main block with terraform. Remember this is written in
the JSON format, which describes a data structure.
We open up then our brace brackets and we put in our required_providers, so
these have to exist on our system. And we can then specify the provider name,
we're going to call it docker. And then we assign it the value of our
dictionary, so the source. Where do we download it from? From our provider
registry. The version that we want, this is the latest version, and then close
off all of our dictionaries.
We can also provide configuration options should we need any then for
our provider docker. We've called our provider here docker, but to be
honest, we could use any name that we want.
This is really a variable that we are populating, but obviously the source
needs to be correct and existing in the registries. So we're going to go
through and get this stage up and running.
We'll then go ahead and look at Terraform and Docker,
installing Docker, adding our account to the Docker group, and
then creating the star of our main.tf.
Once we've done that, we can initialize it, downloading our provider.
Now look at this, this is hardly our glorious command line.
I fired up the browser,
and you can see that we're at registry.terraform.io and browse/providers.
And, obviously, from here we can look at the providers that are
available and we can get clues of how to install them. Now we want
to be working with Docker, so we can go through and search then for
Docker. There are a few available, but the top left is a reasonable
one to go for.
And when we take a look at the Provider Downloads,
we can see then that it's been busy this week,
but over all time we're up to 4 million downloads.
We've got documentation that then shows me how I can use it,
giving me a full version to use, but obviously, the main idea is to
go to USE PROVIDER, and this will give me the JSON code to install
the provider. So I can copy this out and I can paste it into my
main Terraform file later.
So now we are at our glorious command line, and we can begin by making sure
that we are up and running as far as Docker is concerned. We can use the
docker now, but we can also use it in our next module.
So let's get our docker.io package all up and running.
So, of course, they're putting on our container management
system, and once this is up and running,
we also need to make sure that our user account is added to the Docker group to
save us having to run the Docker commands as the root user.
Now, bizarrely, we've got the gpasswd command, which in this case has
nothing to do with the password. We're adding in then our $USER. So
we can just take out our variable, and we're adding our user account
through to the docker group. Now to pick this up, we must log out and
log back in again.
I'm using Vagrant so it's vagrant ssh. And now I can go through and take a look
at my id, and I can see that I belong to the docker group.
Now as far as Terraform is concerned, we just need a working directory, so
I'm creating a directory called terraform. We can move into that
directory. Remember, escape and dot brings up our last argument, and then
I can create a file called main.tf, tf obviously for Terraform, but these
are written in a JSON format.
Now we don't have to do a lot of writing, because the writing has been
done for us. This is what we copied from the provider website. I just
deleted a couple of lines from the top of the file, but we should be
ready to go with this. And once we've got that then created, we can go
through and run our terraform init, and we should see then that the
plugin is being downloaded. So that's got us up and running as far as
the plugin.
The plugin gives me the ability to talk to Docker, and of course
that's what we want to do in this instance,
but we need to put a little bit more into our main.terraform file for it to actually do something.