Complete Intro to Containers (feat. Docker) Complete Intro to Containers (feat. Docker)

Networks & Docker: Client Side Container

Check out a free preview of the full Complete Intro to Containers (feat. Docker) course:
The "Networks & Docker: Client Side Container" Lesson is part of the full, Complete Intro to Containers (feat. Docker) course featured in this preview video. Here's what you'd learn in this lesson:

Brian demonstrates how to connect two different containers by live coding a Node.js application in a container and connecting it to the container created in the previous section.

Get Unlimited Access Now

Transcript from the "Networks & Docker: Client Side Container" Lesson

>> So I want you to make a new directory inside of your project directory, and we're gonna call this one networking, cd into networking. And, Let's do this. We're gonna say npm init -y. And we're gonna say npm install mongodb @hapi/hapi, and we need hapi-pino. P-I-N-O, I think that's all we need, let me check.

[00:00:39] Yeah, that should be good. That'll take a second to install a few of those things. And here, I'm going to create an index.js. Actually let's just do it here from the file explorer. Close the rest of those. And here inside the networking, we're gonna say New File and this is gonna be called index.js.

[00:01:08] And I'm gonna come back over here to networking with Docker, and we are going to copy everything here. It's a little bit more complicated app, it talks to a database. Again, you don't necessarily have to understand this to understand what we are doing here. But to just momentarily walk you through the code here.

[00:01:34] It's going to connect to mongodb via the official driver. Let's make sure that we get the right version. That was a big problem for me last time. To be safe, when you're doing npm install, You wanna do @hapi/hapi, hapi-pino and mongodb@3. Just to make sure that you're getting the correct driver for this.

[00:02:11] Let's even say @3.3. Let's just do that. So this will downgrade your mongodb to 3.35. There we go, 3.4 might work, it might not, we're not going to check. Okay, going back. We're gonna start a hapi server, it's going to instantiate a MongoClient. And it's going to connect to either if I provide an environmental variable of it Mongo connection string it'll connect to that.

[00:02:43] Otherwise it's going to default to connecting to just a locally running MongoDB. So you could run this locally if you're running MongoD, on your laptop. Okay, we'll give it a DB name of dockerApp and we'll give a collection name of count. Anytime that you hit the slash route it's going to say how many items do I have in my, what do I call it?

[00:03:05] Count collection. So collections are like tables in SQL terms, right? And databases are databases, right? So and then whenever you call /add, it's going to insert just a literally empty object, right? Which is just gonna have an ID. So that's all it's gonna do is it's just going to continually add things to your database.

[00:03:26] Basically, the best application ever. I stand by this work. No, I'm just kidding. That's it, yeah, that's really just it. Okay, and again, you could just say brew install mongodb, and then run mongod in the background. All this would just work and that'd be fine, but we're gonna do it with containers.

[00:03:51] Yeah, that's actually what I have here, okay, npm-install mongodb@3.3, so that's what we want. So if you're having issues, make sure you have mongo 3.3 and not 3.4. Okay, now what we're going to do, Is we need to give it a Docker file. So really any old Docker file is gonna work.

[00:04:18] So let's just copy the one from the, Which one should we get, not the volumes, let's do it from the more complicated node app. Not that one, this one. And we're just going to copy that into a new Docker file here. Basically, we just need a working node application.

[00:04:47] So this one that does, copies everything over, run the npm install. Don't need the expose, you can delete that. Then the command here of node, index.js, right? The key here is that we just need to start the node application in a working environment. So I'm going to come in here to where am I?

[00:05:15] I'm in networking. So I'm going to say docker build --tag and we'll call this node-with-mongo or something like that. You can call it whatever you want. What do I call here? App-with-mongo, so we'll stick to app-with-mongo. That'll take a second to build. And then it should be packaged up and ready to go, and there we go.

[00:05:48] So now we're going to run this app and connect to the other database that's running. So we're gonna say, docker run -p 3000:3000 --network=app-net --env, we have to tell it where to connect, right? So we're gonna say MONGO_CONNECTION_STRING =mongodb://db:27017. Again, remember, if you put db, it's going to know because they're on the same network that db goes over to the db container, cuz we named it.

[00:06:33] And then we're gonna run it with app-with-mongo, or whatever you called your container. I think my notes actually say my app-with-mongo, as long as you call it the same thing, it doesn't actually matter. So you can see it started up correctly. And now if I go over here to local host 3000, you can see that it has this count number.

[00:06:58] Can I make that bigger? I can. So that's what we expect. It has nothing in that collection right now. If I open it with /add, then it's gonna say, cool, I inserted something. And now if I go back to the other one and refresh, you can see that its count is 1.

[00:07:14] Or if I come back over here and just hit refresh a bunch of times. And I go back over here to /, you can see now I have 18 items in the database. So now I have one container, the app-with-mango container talking to the MangoDB container. They're doing it via the app network that we set up for ourselves.

[00:07:36] So I recognize this is a bit burdensome to do, right? It's not very fun to set up the network and make sure all the networks are talking to each other. It'd be better if there was a tool that just did all this stuff for us. There is, but you can kind of get my teaching style where I want you to feel the pain and then I want to give you the tool that solves the pain.

[00:07:56] And I don't want to give you the tool that solves the pain before you understand why something sucks first, right? I wanted something to suck first. And then I want you to be happy for the tool that solves the thing that sucks, right? So the next thing we're going to talk about is Docker Compose.

[00:08:09] And Docker Compose takes care of all this stuff for you just kind of out of the box.