In this video you're going to install and run a Maria
database in a container on your Linux instance.
Plan is to learn how to run Maria DB inside
a container. If you're not familiar with Marie to
be it's an open source database that was forked for my sequel.
You will install the docker package to manage this container. Then you
will run a Maria database instance using
the docker command line application.
Finally, you will test database access to the
Maria to be container using the Maria DB client. Okay, let's
get started.
Here we are back in the terminal once again.
So most workloads have now moved over to
Containers like we've talked about before.
So having a good understanding of how to run an application
and manage containers is really pivotal to
today's jobs.
So I have chosen for our example Maria database
for us to run as a container on our
ec2 instance. So like I said in the intro if
you're not familiar with Reed to be it's a popular
relational database. It's open source, and it's a fork from the original
my sequel database which was purchased by
Oracle and is now becoming more and
more prior proprietary.
So but before we can actually set this
up and run Marie to be we need a way
to manage the container.
So this is where a Docker comes in and if you
if you haven't heard a doc or maybe have Dockers just
a sweet of tools that makes managing containers easy and seamless across
any host.
So the two main parts that we're gonna use in the docker package
is the docker server and the docker CLI.
So to install the docker package, we'll use
sudo again and we'll use our yum package manager. So
let's get started with that. So we're gonna install.
Docker package
And we can just do sudo yum install docker.
It is gonna ask us are we sure we want to do
this we are so we'll hit yes, and it'll install
the docker package for us.
Okay, let me clear the screen here.
So since we are not a part of the docker group, you will need
to either prefix all your Docker commands
with sudo or to get around this we can use
the user mod command, which we learned about when we were discussing
users to add the ec2 user to
the docker group. And so let's do that.
So we're going to need elevated privileges again. And so
you're gonna use the user mod command and this time we're
going to use a dash a and a Dash G flag. So
the dash a means append.
Whatever group we want to to the user. So whatever
groups this user already has just appended to this and the
g is the group name, of course, so we're gonna
say docker.
And we're gonna say ec2 users. So
that's the user we're logged in that so we're going to do this.
And of course if we do ID like which
we learned we learned about.
ec2 user
We could see now here. We are a part
of the docker group now. Unfortunately, this does
not take effect until we would log out log back in
but
As with most stuff in Lennox, there is a shortcut we can use and
this is called the new group command. And basically
this will simulate the finalization of the user as
it being a part of the docker group. So
in your terminal you can type oops and I need
to spell it correctly first, but new grp and then
the group that we want to finalize
Okay, so now we can run any
Docker command without having to prefix it with sudo and
so to test that.
We should run Docker PS now here you
notice we have this error and it's saying it gives us a nice
area is the saw is the docker Damon running. Well, it
isn't and some packages by default
will not start the unit file.
For whatever reasons and Docker in
this case is one of them so but we already know how to fix this. It's actually
very simple. We're gonna use this system CTL command.
And we're gonna actually start docker.
And we're just going to use the darker dot service.
Okay, so that should be up and running one way
to test. This is run Docker PS again, and we get
a good return. Now as you
guys the headers of these these columns and there's nothing
running. That's because we haven't running anything yet. And so let's go
ahead and start that next.
Okay. So since containers can't run without
a container image. We need to go get one and one way
we could do that is we can use the docker pull command. And
so that what the docker pull command will do was Google will jump out to Docker
Hub, which is kind of like a repository but for container
images and we're gonna just ask it for
the Maria DB image.
And we want the latest so we'll give
it a colon latest and that just says grab me the latest the the
most newest version of the Maria DB container,
which you have.
and so
there we go. We went ahead and pulled it. It says
it's up to date and that's because
I pulled it earlier just to make sure there wasn't any errors when
I was before recording this video. But what you will see
is it'll actually start pulling it down and you
will see that the progress of each layer that
it pulls down. So we already have the Maria container Maria
database container image on disk so from
here.
The next thing we want to do is we want to get ready to go
ahead and run this container. So Docker has
a subcommand called run and it takes a few
arguments like the name of the container a port environment
variables and the actual container image you want to use
So for us we're gonna name this container Maria DB.
So it's just easy for us to find and run other
commands against Maria DB by default uses
the TCP Port of 3306 and
we're going to use that as well.
So we're gonna open that on the host
and obviously it's already running when
it's running in the on the databases running. It's gonna
be listening on 3306 inside the container but we're gonna
do is we're gonna do is like basically a port forward. So we're
gonna when we when we spin this up we will
have it listening on the ec2 host on 3306 and
any connections that hit that 3306 on the host. Well
actually be routed to the database inside the container.
This will make a little more sense once you see it run on the
command.
Next we're also in this we we need to set
some environment variables. So Maria database has like a root
user a root password a database user
and a database password setup as well. And we're just going
to pass in obviously just throw away fake ones here.
Okay, so it's quite of a long of a command. So
what I'm gonna do is I'm gonna paste the command and then we're gonna go over each section.
All right.
So here we are. Here's the command.
So we can see here. We start with Docker we run. So this
detached command means like basically run
this container in the background. If not, if I didn't have
the detach it or run on the foreground and I wouldn't be at my
it would take over my current shell.
Here we do the name of the container and this
is gonna be read Marie DB and so here's the environment variables. I'll
say with setting so we're gonna create a Maria deep database
user called lab Dash user.
And then we're going to set a password for that lab user
just called Lab secret password.
And then we're gonna set the Maria DB root
password to just lab root password.
I know not not very not very clever. And then
here's the port we were talking about. So this says on the
host open port 3306 on our host.
And then send it to the container.
on Port 3306
and then here on the final on the last.
Argument here. This is just the name of the image and
the name of the tag where the
latest in this case. All right, so I'm gonna go ahead run return and
there you can see it's spit out
this hash. And so every container that gets run inside Docker
is a unique ID and this ID is gonna
come in handy later on also we can use it in conjunction
with the name. So, okay. So the one thing we could do
is now let's let's clear the screen. Actually, I'll leave the screen
at one more time in case because that is a long command.
And we can run Docker PS. And so now what
we can see is we see that long hash these
truncated a little bit there and then we
can see the image we use which was the Maria DB container
image. This is just the command that was run that is
run inside that container image that actually starts a container we
can see it's been up. It was always created 23 seconds
ago and it's been running for 22 seconds.
Here's the port mapping. It says anything on
our ec2 instance. That sends a
packet to 3306. Go ahead and forward
that to this container.
Same 3306 it also uses IPv6
here, but we won't do that. And our name
for our container is the mariadb.
Okay, so I'm gonna clear the screen now and let's
explore what some of the docker commands we have that are
important. And the first one is logs. Obviously
seeing a log of an application if you troubleshooting or
if you just want to see if it's working correctly or if
it's working at all. You could use the docker logs
command and then we're going to use the name of our container, which
is Maria DB.
And hit return as you can see I get
all the logs from the standard out and standard error and
standard in standard on standard error from the container that's
running and you can see here. Basically the
last Vlog amount here. It says the buffer pool
loaded completed, but if you were running into errors and your
container wasn't starting this would be a really great place to go ahead and
look to troubleshoot that
okay. So the next thing we want to do, let me clear the screen here. So
now that we have this container running we need
to be able to access it because what good is it if we can't
do anything with it, so
We can do this either. So there's two ways we can actually
test the connectivity and access to the database so
we can either stall them or read database client on our
ec2 instance using yum.
Or we can actually shell into
the running container and use whatever tooling it
has there to connect to it. So not every container
is gonna have a Marie database client or telnet or
whatever else you could use to actually connect to this service. Now
in this case the Maria DB container image
actually has the mariad database client, but
we're gonna skip that for now, but what I want to show you
is how you could connect to it from your your regular
ec2 instance host and actually make a connection all the way
into the Container. So to do that we need to install the Maria
database package here on this ec2 instance.
It's already in our young repo so
you don't have to do a search for it. So just do the regular
pseudo yum.
Install Maria DB.
And there we go. It says it's
gonna install the Marie DB package confirmation. Yes, we know
what we're doing.
Okay, so this actually even though this
is this is gets maybe a little bit convoluted here.
But so Marie DB is a fork of my
Sequel and at this particular point in time
the Marie DB still used the my sequel client. So
the command line was actually called my sequel instead
of Maria DB. So we are gonna actually be using
the my sequel client.
Executable to actually connect to the Marie database
inside the container.
I know that's a little weird it changes later on. But this is
the versions we have right now. So I'm gonna go ahead and clear the screen.
And if you recall where we can we can connect
to it. This container is listening on any
interface on our ec2 instance on Port 3306.
So we should just be able to try to connect to just
local host or 127.0.0.1 or
if you remember it's called the loopback interface.
Using what we did earlier. So let's
go ahead and try that out. So we're gonna use
the my SQL command.
And we're going to use the host flag in this
case. We'll use the Local Host.
3306 port
the user is lab - user.
And password, so we're
not going to add the password to the command line because it would show up in
the bash history. That wouldn't be really safe. But what by doing just
the dash test password to mySQL client will prompt us to
enter our password.
Okay, so from here I should be able to hit return.
And it prompts us for the password. And if
you remember it was lab-secret -
password and then here we
are. We are connected into the Maria database that
is running inside the container from a connection from our
ec2 instance host. So obviously you could
run any my SQL queer Armory database query
that you know, I will just run show databases with
the semicolon and just to make
sure it works and you can tell be in that this is a new instance of
Marie database is not going to be any databases. We
didn't create any.
So there we are so to get out of this you can type quit.
Okay, so we've tested access everything is
running and working so I'm gonna clear the screen
one more time
So if you recalled we have this
Docker it container running. So
let's say you didn't want it to stay running and you wanted it
to just go away.
One way you could do. This is you could just do stop Maria DB.
And as you can see, we get the name of
their container back Docker thinks is done its best.
Effort to make sure it stopped and if we look it's gone, it's not
removed. It's still there. And if we did
a dash a to the PS that'll show even stop containers
and you can see that it exited with a
clean exit 17 seconds ago. So now
if we go back up and we change the stop to start.
It'll start the container back up because we
didn't remove it with all the same environment variables the same
password everything that we had before.
And if we get rid of the a now we can see the
container is running.
And if we want to we could also do
a logs on it and we
can see that everything looks like it's still coming up so ready
for connections. We can see it right there in this
log line. So all great. Okay.
So let's jump back over. Let's do a recap
of what we did in this video.
Okay. So let's talk about we did in this video you install Docker
from the package repository using young you use
the docker command to run the latest version of DB. We
installed the Maria database client and
connected the DB inside the container.
Finally, you got to learn some Docker commands to manage containers like
pull stop start.
These are all come in handy as you grow or you
start using containerization more in your everyday workloads.
Okay, so congratulations, you have finished section
4 in this section, you put your Linux lab
host to good use and you build some great foundational
administrative knowledge of how to manage and monitor
a Linux host in the cloud.
In the next section, you'll learn subscripting Basics
with bash. See you soon.