Welcome back to this component of the course
where you're going to get some practical experience
of using Docker Compose and Docker Compose is a way
to create and manage multi container applications.
Now, it works using a YAML configuration file,
which can be called compose.yaml or dockercompose.yaml.
Now, generally Docker Compose
is part of a multi-step process.
First, you define each of your container images
using a Docker file,
which you've done earlier in the course.
You push those container images to a repository,
which you're going to be doing later in the course.
You define a Docker Compose file
and then use that file using the Docker Compose command.
Now, inside the text-based instructions
for this component of the course,
is a Docker Compose file.
What I want you to do,
is, to copy all of that into your clipboard
and then paste it in to your favorite text editor.
Now, select all of that text
and there should be a way to convert tabs to spaces.
This is really essential,
because YAML files can't have tabs inside.
In the case of this editor, I can click on editor
and then convert tabs to spaces.
And there we go, this is a Docker Compose file.
Now this Docker Compose file, under services,
defines two containers.
We've got the DB Container and the Wordpress Container
and as you've guessed it,
this is a multi container deployment of Wordpress.
Wordpress requires the application part
and the database part.
So the application part is defined within this container
and the database part is defined within this container.
Additionally, it defines two named volumes at the bottom,
mariadb_data and wordpress_data.
And these are used within both of these containers.
So we can see here, we're using wordpress_data.
So the named volume defined here,
and this is being mapped inside the Wordpress container
to this folder.
Likewise, the MariaDB named volume,
if we scroll up to the top,
is being used within the database container
and this is mapped to the folder
where the mariadb_data files
are stored inside that container.
Now, what I want you to do is to go ahead
and save this into a folder on your local machine.
I suggest your home folder
where we were running the commands
in the previous demo component.
So I'm gonna go ahead and click file and then save as.
I'm gonna go to my home folder
and I'm going to save this file as compose.yaml
and then press save.
Then I'm gonna go to my terminal,
make sure I'm in my home folder and clear the screen.
And to use that compose.yaml file,
we need to run a docker compose up -d.
Now, if I don't specify anything else on this command line,
it's going to look for that Docker Compose file
using one of its default valid names
and compose.yaml is one of those names.
Now, the up option here is going to tell Docker Compose
to bring up the things defined in that file.
So this will have the effect of running containers
and creating any named volumes.
The -d option is going to run this detached,
so it's going to run it in the background.
This works in the same way
as when you've used the -d option earlier in the course.
So let's go ahead and run that command.
And you can see that it begins
by pulling the Wordpress image,
because we don't have that stored locally.
So it's going to download all of the file system layers
which form the Wordpress image.
From earlier in the course,
I already have a local stored copy at the MariaDB image
and so it's going to use that local image.
And then it's going to create two named volumes,
mariadb_data and wordpress_data.
And in this case, it's prefixing
the volumes which it creates
and the containers which it creates with my username,
which is acantril.
So now all of this is running.
If I run a docker ps and press ENTER,
we can see that we've got these two containers running,
the Wordpress container and the database container.
If we run a docker volume ls,
likewise, we've got two named volumes
which have been created by the Docker Compose command.
So now let's open our web browser
and go to http://localhost:8081 and press ENTER.
And what you should see
is the Wordpress installation screen.
So this is running inside the Wordpress container
and if we go back to our text editor, we can see this,
because we're using this ports directive here
to map port 80 of this container
to port 8081 on our Docker host,
and that's why we can access it using port 8081
in our web browser.
Now, we're going to have to choose the language
that we want Wordpress to use.
So select the language and click on continue.
Then it's going to ask for a site title.
I'm going to use All the Cats
and you can do the same.
For username, go ahead and put admin.
And then for the password,
leave the default generated strong password.
Although, what I want you to do,
is to copy this into your clipboard
and note it down somewhere safe.
You'll also need to enter your email.
So I'll go ahead and enter mine.
You should enter your own email.
Scroll down to the bottom and click install Wordpress.
Once the install is complete, click on login.
Enter admin for username.
Enter the strong password that you just copied down
and click on login.
After a few moments, it should log you in
and the Wordpress installation should be fully functional.
So now we're gonna go ahead and just create a test post.
So click on posts.
And then add new.
Click on the plus sign here and then go to gallery.
We're going to add a gallery.
Click on upload and then go ahead and select some images.
If you don't have any images on your local machine,
you can just go ahead and go to Google Images,
download a few cat images
and then upload these to this gallery.
In my case, I'm going to be inclusive.
I'm going to pick a cat, a dog and a different type of cat.
I'm gonna give the post a nice title, so Two Cats and a Dog.
Then click publish and publish again.
And once that's done, click on view post.
So this is now stored some data,
different types of data on the Wordpress container
and the database container.
On the Wordpress container, it's stored these actual images,
whereas on the database container,
it's stored the metadata and information about this post.
And both are required for this post
and Wordpress to be fully functional.
So now we're going to demonstrate
how the data that we've stored in both of these containers
can now live on past the lifecycle of the containers.
And we're going to demonstrate that
using the Multi Container Management functionality
provided by Docker Compose.
So let's go back to our terminal
and we're just gonna clear the screen
to make it easy to see.
If we run a docker ps,
we can see that we've got two running containers,
Wordpress and the database.
Now, still within that folder
where we have our compose.yaml file,
I'm going to go ahead and run a docker compose down
and press ENTER.
And that's going to stop and remove
any of the containers defined
within this compose.yaml file.
So it's going to remove the database container
and it's going to remove the Wordpress container.
If we just clear the screen and run a docker ps,
we can see that we've got no running containers.
If we run a docker ps -a and press ENTER,
we can see that we've got no stopped containers.
These have been completely removed.
However, if we run a docker volume ls and press ENTER,
it hasn't deleted these named volumes.
And remember, these named volumes
are actually storing the Wordpress data
and the data from MariaDB
and if we create new containers and set them
to use these named volumes,
they'll use the data within these named volumes,
which should contain the post that we just created.
So let's test that.
If we run a docker compose up -d,
it's going to recreate both the Wordpress
and the database containers.
Now, because we already have the Docker images
for these stored locally, this should be pretty fast.
And in addition, it will utilize the existing named volumes.
It won't create new ones.
So let's go ahead and run that command.
We can see that it's created both of the containers,
it's created the network
and it's going to reuse those existing named volumes.
So now if we go back to the web browser
and just hit refresh,
we should see the existing post
continues to work with no problems.
We can open each of these images in new tabs
and see that we're directed to the original images
that are stored within the Wordpress container.
Now again, this is all possible
because we've used named volumes.
So these are volumes which persist past the lifetime
of the existing containers.
Even though we used the Docker Compose down command
to delete those containers,
when we recreated those containers,
it used the existing named volumes
which contained the database data and the Wordpress data.
So this is the metadata of the post and the image data.
So that data persisted past the lifetime
of any individual container and it was reused
when we ran new containers using Docker Compose.
So I hope this demonstrates
both the power of named volumes again,
but also how easy it is
to manage multi container applications
using Docker Compose and compose.yaml files.
So it's really powerful.
You can use Docker Compose files to define containers
which images are used, any commands to run,
any named volumes.
You can specify environment variables,
how containers respond to container host restarts.
You can also define links between different containers,
and you can manage applications
which consist of multiple containers easily
using Docker Compose.
Now, that's all you need to know at an introductory level.
What we need to do now,
is to clean up all of the resources
that we've used within this demo component of the course
and we'll do that using docker compose down.
Again, this will delete those containers
and the network that the containers use.
We'll need to delete the volumes manually though.
So if we run a docker volume ls,
we'll be able to see the names of the volumes.
And then we'll need to run a docker volume rm
and then one of the volume names
and then again, docker volume rm
and then the name of the other named volume.
So in this case, my username _wordpress, _data.
At this point, what we should be able to do
just to confirm everything has been deleted,
I'll clear the screen and then run a docker ps -a.
We've got no running or stopped containers.
Then docker volume ls,
and we've got none of those named volumes
that we created during this demo component of the course.
At this point, that is everything I wanted
to cover practically about Docker Compose
and Docker named volumes using Docker Compose.
So go ahead and when you're ready,
you can move on to the next part of the course.