So, yes,
we're going to dive out to our command line now, and we're
going to be working with our Docker containers.
So we're going to take a look at our image resource,
the container resource, looking at mapping network
ports, and connecting host volumes.
We're just about ready to go ahead and start extending to our
resource management, but when we look inside of our Terraform
directory, we've just got our terraform file. So I'm going to
create a directory to host the web pages.
I can create them on the host, so echo hello, through to then
our web directory, and then the index page.
Now, obviously, this is a really simple page, but there's
nothing stopping me putting more complex pages and page
resources within that web directory.
We're going to map that web directory through to the required directory
inside of our web server that we're going to create, and we're going to
do that by going into our terraform file.
We've already discussed that the terraform file is JSON, but just to keep
everything the way that it has been set up so far, I'm going to go through
and escape and colon, and bottom left‑hand side of the screen, we can see
then that we're setting the auto indent,
expand tab, our tab spaces to be 2, our shift width to be 2, so some of the
basic things then that was going to make sure that everything aligns. It's
more for readability purpose than anything else.
We're going to start defining our resources now. It was easy before where we
could have just pasted this directly from the provider, but now we'll go
through and looking at defining our own resources.
Our resources are going to have two elements in their
name that uniquely identifies it.
So we can use docker to link back to our provider, and then we're going to say
this is an _image, and we can then go through and say nginx. We must uniquely
identify this resource inside of this terraform file. Then to set up our
properties is relatively straightforward.
We can go through and assign the name, and this is going to be the
name of the image inside of our Docker repositories.
So we would install this from Docker using nginx, and then
the version latest, so nginx:latest.
We can also go through and specify that we don't need to worry about keeping
the image. It's up to us, but if we go through keep_locally and just move the
screen up a little bit just to put this into the center, and this has a
Boolean value of false, so we're not storing our images locally once we
finish, meaning that we tidy up our resource totally when we have finished.
When we go through now, the second resource that we're going to need is our
container resource.
So, again, we need to make sure this is uniquely identified, so we
can call it docker_container and nginx. Open up then our brace
brackets, and we can start putting the properties, and there are a
few that we can put in here, the first one being the image that we
are going to use for this.
We clone the image into our running container.
So this then is our docker_image.nginx, and then going down
and drilling down to the latest version.
We don't quote this, as it's now an object that we can find
within our metadata that we're looking at here.
We can go through and assign a name to the container.
This is how Docker will see it.
We can give a more interesting name, but I'm just going to call it
webserver. Again, we're back to strings for this, so be careful with
your string data and your variable data.
We're going to set up some ports now. The ports will
allow me to communicate to my web server.
The web server is going to be listening on Port 80, so we can say the
internal to the container is port 80, but externally on my host, we're
going to be able to access it then through the external 8000, so port
8000 on my localhost. And we can move that back in so the brace bracket
is tidy underneath ports.
We can set our volumes in much the same way, and this is how
we can then map our web directory through to where it needs
to be inside of our container.
So our container path, the NGINX web server is going to be expecting to
find its glorious web pages underneath /usr/share, and then nginx, sorry,
the page just jiggled there a little bit, and html. So that's the document
root of our web server, but we want to map our host_path, and this must be
a full path, and that's going to be then for me, my user account,
/home/vagrant/terraform, and then the web directory that we just created
and populated.
Now I know I've only got a single web page in there, but as I say, we
could have then as many web assets as we needed within that directory. I
can save it, and then go through and run my terraform apply. Remember, the
init was to make sure I had the right provider. Now we get to see the
plan, what it's going to do.
I have to accept that plan by typing yes, and we see then the
provider and Docker are doing the hard work underneath.
Now if I go through and connect to my localhost on port 8000, I should
be able to see my web page. So that's all working the way that it
should, and we have got our Docker resources up and running. I can use
docker ps to show me my containers, and we can see then the port
mappings through this as well.
If I look at docker image,
I can go through and see then the image that we downloaded.
If I go through, though, and destroy everything, so what we create,
we can also destroy, so terraform and destroy, it's going to tidy up after.
So I still have to go through and accept the destruction with yes,
but when we look at the images, we said keep_locally no,
so the image has also now been removed.
And when we go through and take a look at the Docker containers,
we don't have any Docker containers running or stopped.
So we've tidied up everything that we created so we're not leaving, if you like, our host in a mess.