Hi, I'm Dalia. Hi, I'm Trisha. And in this video, we're going to show you
how to set up MongoDB in a Docker container. We're going to be using IntelliJ IDEA. We decided
to pair on this because I have a lot of knowledge about Docker and Trisha has a lot of knowledge
about MongoDB from the time she worked there. So we're starting here. I'm sharing my
screen. Trisha is going to show me how to do certain things with Mongo. We actually
recently had to do this exact same thing. So Trisha, do you want to walk us through? Yep. Exactly. So we created
a spring boot application. The spring boot application was, the
idea was to allow restaurant bookings. So as a customer, I can book online a restaurant
so I can go there and have a meal out in the real world. Yay. And this is a microservices
application. The code we're gonna look at today is the restaurant service. This is the
metadata of restaurants. So, you know, their name, their opening hours, you know, their capacity,
all the things to do with the restaurant. And we decided for this service to use
MongoDB as the data store for this, the reason being the thing I learned
when I was working at MongoDB, is that the way that the
document database works with, it's sort of flexible schema. It works really
nicely if you're not entirely sure of exactly the amount of data, you're going to be storing
about an individual entity. So for restaurants, for example, some of them, you might just have
like name and opening hours and some, you might have like name and opening hours and indoor
capacity and outdoor capacity and smoking versus non-smoking. So this kind of flexible schema, it
fits very nicely with the MongoDB way of working. So that's why we decided to
use MongoDB for this service. Yeah that's a great point. It's the
flexible schema is the key here. And here's our main class. So
if we tried to run it right now, oh, Trisha is probably going to ask me why
are you not using the shortcuts Ctrl+Shift+F10 in order to get it running, I love these gutter
icons, and it also tells you, oh, go ahead. I do like the gutter icons because they're very
visual. So you know exactly what you're running, especially if you've got like a test
class, that's more than one thing in there. Yeah. That's a great point too. You can
just run one method or so on and so forth. So there we go, starting here and we see an
error. It'll stop logging here. So if we go backwards, we'll see that it has an error.
And if you look at the stack it's saying that it can't find Mongo, which it makes sense
because we don't have Mongo installed right now. So what we're going to do is we're going
to set up MongoDB with a Docker container. So I'm going to go to my services
window. And I have Docker set up already. I have Docker running. If I bring
up my icons here, it's running right here. And I have a connected, if you have any questions
of over how I got Docker connected with IntelliJ IDEA, check out my Docker in IntelliJ IDEA,
video, I go over how to set that up and a lot of that info. But for here, we're
assuming that Docker's already set up and we're ready to set up MongoDB. So what I'm
going to do is the first thing I need is an image. So we're going to pull a MongoDB image. So
I'm going to start typing M and actually IntelliJ IDEA has auto-completion here.
So I can just see the recommendations. Mongo comes up right away.
I'm going to select that and then I can leave this blank and it'll
give me the latest or it can choose a version. So Trisha, do you have a preference on. I like to use the version number, an explicit
version number. The most recent one is 5.0.3, which is pretty new. That came out last month. Cool. And actually, if you're ever
wondering about version numbers you can also do Ctrl+Space and
you'll see the versions here. If I start typing 5, so it can kind of narrow
it down 0., you said 3 . Perfect. So that's what we're going to do. And then we're going to click,
not enter, but Ctrl+Enter. I always mix that up and it's going to start pulling MongoDB
from DockerHub. If you already have the image locally, it won't take long
because it's already on your system. But if you don't like me, it's going to take some
time. So now we cue the music doo-do-doo-do-do this is the world of video. We can speed this up. That is true. So you'll watch it speed
up. All right. Looks like that's done. So you'll find under images, the new image right
here, and you'll see all the information about the image. All right. So now that we have the
image, we can create a container from that image. So we're going to right-click on the image.
Click Create Container. Which will bring up a configuration window and in here you'll see,
the name of the configuration is set for us. The server is set for us and the image ID is
set according to what we right-clicked here. So what we're going to do here is we're
gonna set it set a name for the container. I'm going to call it mongo-container. And
then if you want to add any more options, you can click modify options and select the option
you want to add. For me, I want to bind the ports. Mainly because I need to publish the port that MongoDB is going to be running on.
Otherwise I will not be able to access it. And then Trisha can remind
me of the defaults here. 2 7 0 1 7 2 7 0 1 7. And we can set both of
these to the same. But you don't necessarily have to do that if you want to set it
to a different host port. That's fine. And I explained some of that in the networking
video that I made. Mainly because a lot of people try to mess around with ports and they don't
really understand what exactly it's doing. So I do have a networking Docker, a video that I talk
about you know, what you're doing here. Exactly. Which I found very helpful because I always
struggle with ports. I'm like, which one is host and which one is container
and what are they supposed to be? So yes. Do check out it out. The other thing you can do is also
you can bind the mounts. So if you want the data to stick around after you delete
the container, then you can bind your volumes. I'm not going to go into that or set it
right now. You can look into that because for our dev environment, we don't
necessarily want the data persisted we're just in development right
now. But if you want to set it, you can definitely do that in this window. So
that's all I'm going to do. And then I'm going to click run. So if you're working in the
terminal, this is the equivalent of running the Docker run command. You can do that in
the terminal, or you can set it in IntelliJ IDEA so that you can just keep running the config over
and over you don't have to run the same commands. So I'm going to click Run. Is there a way to see what Docker command it ran
when you did that. The equivalent command line? It used to show up in the config and that
was actually not there anymore. Let's see if there's a way to see it. It used
to show up at the bottom here. Show command preview. That's a good point. Right there. Yeah. That's really
helpful in case you also wanna know, you know, how this would look like if
you're running it from the terminal. Good point, Trisha. All right, so let's go
back to the log. So there's a bunch of tabs that are associated with each container and it
has all the information about your container. So here, this is just the build log. So
the process of building your container and what happened. It looks like
it was created successfully. And this is the log of the process in the
container. So this is the MongoDB log and Trisha, I'm going to need your
help here to decipher the log. Everything in MongoDB is JSON So everything's going to have like curly braces
and and inverted comments like that. But yeah, I would be looking, hopefully there's
something which says the service started, Okay. Let's search. What any of that is?
Yeah. Let's let's see if I can search. Look for the port number. Look for 27017. Oh yeah, let's do that. Here you go, it says starting on that port. Then we got listening... And
waiting for connections. Perfect. So, once you see waiting
for connections, you know, it's ready and it's waiting
for something to connect to it. Perfect. So that's the main log
line that we look for there. Any other things that we want to point out here? I think maybethat you have like the
properties, so you can see the name here, any environment variables that were set
for you. Port bindings volume bindings, and oh you can even browse the file system.
I thought that was really helpful. Yeah. This is really helpful because for me, I
don't really understand containers cause it's just this magic black box which happened
somewhere else. And so having the ability to kind of drill into that black box and see
what's there, I find that extremely helpful. Yeah. Is there anything you
want to do with the container to make sure it's up and running properly? Yeah, so one of the things that we were doing
when we were playing with MongoDB before is if you're used to working with
MongoDB, especially if he used to working from the command line, like we're
often taught in the getting started stuff. Often you want to sort of use the
Mongo command to once, you know, the database is up and running. You want
to be able to query it and sort of see, is it really running? And is it
the database I expect it to be? What I found confusing about Docker was I can't
do that from my terminal command line, right? Because it's not really running,
it's running in a magic container but Dalia showed me a trick, right? Show me the trick. You can right-click
on the container here and click Exec and then create and run. Right, so I want to run a command line command
called mongo, which is the repl basically for MongoDB. So should be able to run commands
just from that, if that goes, see if it works. Is it just mongo or is it mongo bash? You should be able to it from just mongo. All right. Let's try it out.
Let's see. Let's see if it works. Oh, that did work. Okay, cool. If you scroll
up to the top of that. Yeah. Let's do that Then it's basically, yeah, it shows you're
running the Mongo shell. So you basically that's the same as if you touch
Mongo for the command line and you're running a local MongoDB . This
is the shell that you'd normally see. So then you can type stuff in here to have
a look at what's going on. So the default, the first place you want to, the first thing
you want to check is show dbs, just to see that you've got some databases semi-colon, or
you don't need need a semi-colon. Who knew?, so, yeah, we've got a nice, empty,
clean instance with basically no databases and no data in our
admin databases, but it is there and it is working. Perfect. So maybe this is a good time to try to insert
some data. Okay. So we're going to go to our restaurant controller and we're
going to find the saveRestaurant method. So let's see it's down here. We're Trisha
showed me this really useful way. Oh, go ahead. Before you click on that,
let me explain what this is. So, this is a spring boot controller. It's
a restful controller. So what it does is it shows all the URIs that you would usually use
a browser or an external service to call this service. So what Dalia is going to do here is
instead of using a browser to call these URIs, we'll use the IntelliJ IDEA
built in HTTP Client to do that. So go ahead, click on that. Let's do that. And so we're doing a POST request. We are running on local host and I have some JSON data. So y'all, don't
have to sit through us typing that out. You're going to post an individual
restaurant to the restaurant service. Yes. So let's run that and see what happens. So hopefully that works. Okay. Yay. Response
code: 200 that's what we like to say. Yes. So that means everything went okay. So let's
figure out if there's any restaurants. We could actually go to our let's see,
localhost and then let's do restaurants. All right. It looks good. Except the other thing
that I like to really check is how things look in the actual database. And actually the database
Explorer is something that I always use. So let's set that up really quick. So what this is going to
do is it's going to have a connection to MongoDB, and it's going to show you your
tables right in IntelliJ IDEA. I like to see the data right there. So
I like to always connect my database to IntelliJ IDEA. So let's do that Data Source
from URL. And let's type in the URL Trisha, you're going to have to remind me, is it
mongodb or mongo? It's mongodb. Okay. I always mix the two, sometimes it's mongo,
sometimes it's mongodb://localhost:27017. Okay, perfect. And then look IntelliJ IDEA picks
up that it's mongoDB one. Perfect. And then let's click.
Okay. And see if it connected. It's going to bring up the configuration
window and asks you if you want to configure, anything else. I'm just going to take all the
defaults because everything is set to the default. It doesn't have any drivers. So
if you just click download here, it's going to go off and
download the missing drivers. I find this the most useful feature of this whole
window, because the number of times that I've been trying to connect to a database, especially from
JDBC or something like that. And you're like, what is the magic incantation for my driver
files to connect to my JDBC data source. Totally agree with that. Let's do the test
connection to make sure that it's running properly and here it is. So it looks
like it's succeeded and the connection was successful.
So let's click. Okay. That's connecting to MongoDB in
the database Explorer. One of the things I like about this is it's treating
NoSQL databases, the same as SQL databases so that you can take a look at the, at your data
sources, take a look at the data that's in there and poke around with it the same way you would,
if it was MySQL or one of the other SQL database. Yeah, and I always do this with MySQL containers
as well, because a lot of times it's really easy to set up a MySQL database. And then I just
connect it with a database Explorer and just go on and create all the tables
and the data and all that. There it is. It's connected
and it has a restaurant table. Collection. Collection. Yes. See,
I come from a SQL background, so you're going to have to keep correcting me. I think that's another thing that's
important to notice here is that the IntelliJ IDEA support for mongo DB is evolving.
It is trying to fit a document shaped database into a relational database
type shape inside the UI. So restaurant is represented as a
table, even though it's a collection to say the word collection. And it kind
of has the concept of fields, which is not quite the same thing in MongoDB.
And then when you click on it, you get a table of data like this as sort of
output in a tabular way. I quite like this because firstly you get a visual representation of your
data, which is one of the things I always like. I'm a very visual person. And secondly, if you
are coming to something like MongoDB from a SQL database, it's kind of nice to see it in a more
familiar form. Whereas normally when you look at the documentation in MongoDB land, it's all about
using the command line and seeing stuff in JSON and it takes a bit of a mental model shift to get
used to using the MongoDB way. Whereas if you use it inside IntelliJ IDEA, it kind of, it feels
a bit more familiar. And a bit more database-y. Yeah, I totally agree. This brought it home for me. When I was like
experimenting with it. So you can see that the record can I call it record? The document,
you can see the document made it in here. And it's all good to go. Anything
else you want to show here? Not really. In here, we don't really have
a lot to show. I mean, the only thing to really point out is if you scroll to the right.
And look back at the dates the opening days, whereas the Saturday there, MongoDB
supports this idea of lists as a type. So it's kind of that one field can have multiple
values. So it's shown almost like a Java list. Yes. Yes. That's a good point. Okay. And
do we want to talk about the console? Yeah. Let's just quickly look
at the console, because again, if you're used to working with MongoDB, you'll
be used to using like a command line console. So if we open the, if we open
the console. This option? Yeah. Let's give that a go. Great. So if this was
a SQL database, this is where you'd be typing your SQL commands, like SELECT * FROM whatever, but
this is not a SQL database. This is a MongoDB database. So this works in a slightly different
way. You want to use MongoDB commands here. For example, we can use
show dbs like we did before. And let's see, is it Ctrl+Enter or Enter?
It's probably Ctrl+Enter Let's try that. Cool. So yes. So you can see all the databases
we saw before admin config and local, but also the new one test, which
is where we ended up putting our test restaurant data. You can switch to using the
test database by using , or by saying, use test. Right. Here's one of the things
I want to point out here. This is this is the way the IntelliJ IDEA does
a whole bunch of console stuff. So you could, if you want to, you can run line one
and line three or the whole thing. You can run the whole script if you like,
or you just run the individual line. So I think if you use Ctrl+Enter, I
think you're all on just that one line. I think so actually I noticed something, so I,
you can do auto-completion, code-completion here, and it'll show you the different databases
that you have. So that's really helpful. And then you can do let's do Ctrl+Enter. Oh,
it asks me which statement I want to run. So do I want to just run, use test or all of it? So that's helpful. We're going to do use test. That's one of the things that makes this
different from a terminal command line in that instead of just doing like one line at a
time, you can build up a set of commands and you can run all of them or run them into the. Cool.
So you're already on db test, that's useful. So one of the things I like to do is
have a look at everything that's in the restaurant collection. So we're on
the test database. So it's db dot. You should be able to say restaurant. So that
means we're going to use that collection dot and then find you should be able
to get code of completion for find. Right. And again, because the
IntelliJ IDEA is going to give you code completion with these things ,
it's really helpful. If you're kind of getting to grips with MongoDB and you're
trying to use something very very new. Yeah. Let's do Ctrl+Enter. And then
we just want to run that. Tada! There we go. Okay. So it looks like
everything is running properly. After we ran this locally, we want it to run
it with Docker Compose so that it's easier for everyone else on the team to get the app up
and running. So, we're going to show you how we set up our application with Docker compose so
that it's both running with two Docker containers, not just one Docker container,
which is the MongoDB one. So, what I'm going to do is I'm going
to create a Docker compose file. So let's go to our project window. I'm
gonna insert and then create a new file. And we're going to call it docker-compose.yml. Yes, add it. So FYI, we do have a Dockerfile
already for the restaurant service. So what we did was we containerized our
application using a Dockerfile. So we already have the Dockerfile. So if you have an
application not running in a Docker container, you're going to want to create a Dockerfile or a
Docker container for it using a Dockerfile. And I go into that in my Docker videos. But
we already have it for this restaurant. So, what we're going to do is we're going to
create two services one for the application itself and one for the MongoDB container. So
first thing is we're going to set the version and this is the file format
version for Docker compose. And then we're going to create two
services. One is the MongoDB service. I'm going to just call it MongoDB. And then I'm
going to tell Docker compose what image to use for that service. And here I can also use
code completion. So I'm going to start typing and I see Mongo came up. And
then what was the version again? Trisha? 5.0.3 Okay. So that's that. Yes. And
then we're going to set the ports. So this is the equivalent of what we did to do
the run command, to get Mongo up and running. This basically codifies it in
one file. So you don't have to create a configuration or do anything
like that or save your commands. This is going to be a one-stop shop. Click that
run button and get the application up and running. This is what I like about Docker compose.
This makes a lot more sense to me than running stuff from the command line,
because I've got configuration file, which spells out exactly what services I
want, what versions they are. And basically I can use something like IntelliJ IDEA to
just run them consistently the same time. Yeah, exactly. That's why we wanted to set it for
the team so that it's easier for them. And then 27017. So that's all we did in our Docker run.
So that's enough for the services for MongoDB. So now I need to create the service for my
application. So I'm going to call it restaurant. You're much better at spelling restaurant than me. We had that. We had a lot of fun
teasing Trisha about that. You had fun. And then we're going to build the image before we
run the service, if it doesn't exist. So that's what I'm doing here. And then I'm going to set the
ports. And in this case we're exposing port 8080 so that we can access it via localhost:8080, and
then we need to set the environment variable. So. What this is going to do is
it's going to change the host name for how to access MongoDB. And I want to explain
that for a second. So when we created the MongoDB container, initially we created it on
the local machine and exposed port 27017 so that we can access it via local host. Now, when
I create a separate container for the application and that separate containers, trying
to connect to the MongoDB container. It's two isolated containers and it
doesn't really know how to access localhost. So what we're going to do
is we're going to tell the services, the application container to connect to the
MongoDB container via its services name. So the way that we're going to do that is we're
going to pass in a spring property. And it's going to be spring.data.mongodb.host=.
And that is going to be set to this name here. So this is going to tell the restaurant service,
when you trying to access the MongoDB container, access it via this not localhost, because
otherwise it just doesn't really understand. Localhost means whatever is in the application
itself. Not anything outside of that. And lastly, what I like to do is I like to add
a depends_on that way I can set MongoDB as a dependency, so that service starts first. And
then my restaurants service starts because the restaurant service is not going to start properly
if the database is not up and running already, and that's all we're going to need to do in the
Docker compose file. So now let's try to run it. And I like the gutter icons here because you
can start and stop services right here. You don't have to, you know, if you want to just
restart MongoDB, you don't have to restart everything in here. You can just restart Mongo. I've been using that a lot in Docker
compose. So I won't start the whole thing, but let's say I want to do some sort of
system level tests with the database. I don't need all the services
running. I just need MongoDB running. So I start MongoDB and then run
some automated tests against MongoDB Yeah, that's a good point. But I like the way that, because you put the
dependency that depends on in restaurant. If you start restaurant, it's going to
make. That MongoDB is running because, and that makes sense because it's, the
restaurants is not gonna work if it's not running. Yeah. And you get some help here in case
you're wondering, you know, what each key is doing. And this is a YAML file. So you want
to be very careful with your indentation and all because if you get that wrong then you know it
won't work quite the way that you expect it to. What I love about that the
errors are not very helpful. Yes. Let's see, we did get an error.
You got an error. So it looks like Failed port. Oh, the port is already
allocated 27017. The reason that we're getting that is because we already have
a port binding to the host port 27017. So what we need to do is we need to stop the
containers that are bound to those two ports. And the hint there is cause it's 27017 it's
almost definitely something running MongoDB. Exactly. Yes. So what we're going to need to do is
go to the container we created earlier and stop it so that it's not a binding that port. And let's try that again. And now it's complaining about 8080. So
now we have to actually stop the 8080 port. We have it running. We don't have it
running in Docker. We have it running and we have it running from IntelliJ IDEA Yes! We have it running. Locally. And so what
we need to do is we need to stop the run here. So that's a good point. Because sometimes
you think, is it running in a Docker container? Or is it running locally? No
matter what the end result is the same, because there, it just means that port is
bound to something or some process out there and you need to stop that process. So let's
now try it again and see what happens. Looks like things are starting. So the way
I like to look at it is I like to expand my Docker-compose node here. And I see the
two services, my mongodb, and my restaurant. Under there I see the container. So if I go
to click it, I can still see the logs here. And I know Trisha told me that it was the 20. All right. I'm going to search.
It's so much easier - 27017. It's searching through your containers. Yes, it
is. Let's go back here. Let's do this. And we are going to try that again, 27017 so it looks
like it's right here. Waiting for connections. Yes. So that's working and then if we go to the
restaurant container, let's see if that is working And it looks like the logs are pretty clean,
so it should be working. Let's go ahead and go to the browser and interact with it. So I'm going to refresh. And it's empty this time. The
reason it's empty is because we're not using the original container anymore.
We're using a new container that Docker compose created. So if we wanted to populate it again,
we'd have to do the same thing we did last time, which is go to our restaurant
controller, click on generate request. And then. You can reuse the one at the top. Oh, good point.
Yeah. If we scroll up, we can just click on that. Looks like it's 200. So if I go
to the browser again and refresh. Looks good! So it's running. So there's
two ways of running your applications with MongoDB. You could do it from just a local
application connected to your container, your MongoDB container, or you can set up
Docker, compose and have two containers talking to each other. And that's all we wanted
to show today. I hope you found it helpful. If you have any questions about, you know,
how to run Docker in IntelliJ IDEA or just Docker in general, I created a video
series that talks about Docker basics, like, you know, what an image is, a container is,
Docker compose, networking and, all that. And I have a specific video about Docker
in intelliJ IDEA in those video series. I'll link that in the description
and you'll find the playlist. So you can go through all those videos and
learn more about Docker. That's all. Thank you for watching. And
thank you for joining me, Trisha. Thanks for having me. This was really fun.