We have talk about what is docker in the previous article. One thing we haven’t talk about is Docker Networking.
If you haven’t read the previous article, you can read it here. Understand what is docker in 5 minutes
Let’s get into it.
How you can connect multiple containers ?
How is the containers talk to each other ?
How you could connect the application running in a container to your local machine ?
How could you connect to the world wide web from your containers.
We start off with What is networks / requests ?
In many applications, you’ll need more than one container — for two main reasons:
- It’s considered good practice to focus each container on one main task (e.g., run a web
server, run a database, …) - It’s very hard to configure a Container that does more than one “main thing” (e.g., run a web server AND a database)
Multi-Container apps are quite common, especially if you’re working on “real applications.”
Often, some of these Containers need to communicate through:
- Either with each other
- Or with the host machine
- Or with the world wide web
— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —
Let’s get our hands dirty!
This node js application here (Click to download), has a couple of dependencies and some custom code to build a web api.
A little background of this web application. This application returns data when requests are sent to web api.
It accepts GET requests to the api endpoint (‘/favorite’), (‘/movies) , (‘/people), and POST request (‘/favorite).
We are going to send the GET request to (‘/movies”), then fetch the data from the response and send it back using our own POST (“/favorite”) api to whoever requests this data.
And in this application, we also establish our connection to the MongoDB database, therefore, you will need a running MongoDB to work.
With that connection, we can store our data (our favorite movies, the response from GET “/movies”) into our database.
Step 1 : Now, we will build the using the docker file using
docker build -t your-preferred-name .
Next, we run the image using the container name demoapp in a detached mode and remove it when it did. Exposing it to port 3000: 3000 of our local machine because this app is using port 3000.
Step 2 :
docker run --name demoapp -d --rm -p 3000:3000 favorites-node-app
However, you will realize that the container is not running. Why?
To troubleshoot it, we run the docker container again in not detached mode, and you will see “MongoNetworkError,” which means the connection failed because MongoDB is not installed.
This is due to MongoDB is not installed in the container, and it failed to connect to the database.
Now, what if we installed MongoDB in our local machine and connected the container with that?
First, we go to MongoDB installation guide and install MongoDB on our local machine.
Then, we change the connection to host.docker.internal.
What this code does is tell docker to look for that MongoDB in our local machine. This is the path that docker will understand automatically.
Now, if we rerun Step 1 and Step 2, you will see the app is up and running now.
Next, we are going to validate if the API works.
TA-DA! It works like a charm
To recap, what does the above-mentioned step does?
We are connecting the docker container to our host machine by using host.docker.internal.
— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —
Do you still remember why we failed to run MongoDB in the first place?
Yes, we did not have MongoDB installed.
But what if we only want to connect to the World wide web now? What if we only want our docker to make a connection to the internet?
Can try this out by changing the MongoDB code into a comment first, then we add “app. listen(3000)” to the above of it.
app.listen(3000);
//mongoose.connect(
// 'mongodb://host.docker.internal/swfavorites',
// { useNewUrlParser: true },
// (err) => {
// if (err) {
// console.log(err);
// } else {
// app.listen(3000);
// }
// }
//);
We run the docker image and container again.
docker build -t favorite-node-app .
docker run --name connectwww -d --rm -p 3000:3000 favorites-node-app
Then we send GET request to the /people and /movie again. The response should be returned.
To recap, what does the above-mentioned step does?
We connect the docker container to the world wide web by sending request via HTTP to the www. This is doable by default for docker. Nothing we need to configure.
— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —
Lastly, we are going to connect our docker container to another docker container.
Communicating with other Containers is also quite straightforward. You have two main options:
- Manually find out the IP of the other Container (it may change, though)
- Use Docker Networks and put the communicating Containers into the same Network
Option 1 is not great since you need to search for the IP on your own, and it might change over time.
Option 2 is perfect, though. With Docker, you can create Networks via the docker network, and you can then attach multiple Containers to one and the same Network.
Now, let’s go with Option 1.
- We start and run a docker container named “MongoDB” with a mongo image.
- We check whether the docker container is running using docker ps.
However, we know this is not going to work, because our mongo db is still connecting to host.docker.internal.
Hence, we need to change it.
What we do is
docker container inspect mongodb
and then we try to find the IP address from the network settings.
We run the docker image and container again.
docker build -t favorite-node-app .
docker run --name mydemoapp -d --rm -p 3000:3000 favorites-node-app
We will see that app up and running and be able to examine it with the postman.
Option 2: We will create a network and put two containers under that network.
docker network create my-app-network
I created this docker network named my-app-network.
docker run -d --name mongodb --network my-app-network mongo
Then, I run my MongoDB using the mongo image and put it under the network that I created just now -> my-app-network.
Lastly, rerun the docker build and run the images created using docker file as below:
docker build -t favorite-node-app .
docker run --name myapp --network my-app-network -p 3000:3000 favorites-node-app
Now, you can examine that by sending GET request to the /favorites endpoint, and it shall return you with an empty response.
This is due to we created a new MongoDB in a separate container, and it should be empty.
To recap, what does the above-mentioned step does?
We are connecting our container that contains our app to another docker container that contains our MongoDB. With 2 options, we connect both of them using the IP address, or we put both of them under the same docker network.
Related Articles:
- Cloud Computing , let’s talk about it [Part 2]
- AWS ! the terms you must know 🤫
- ISTQB 🧙🏻♀️The software testing Myth, do you need that?
- Why software testing is needed in the first place ?
- What is Agile Methodology ?
- Java 101 for software testing (Using fruits and Vehicle as example)
- What do you need to know to test using Selenium in Java? (that I wished somebody had told me)
- “Understand what Docker is in 5 minutes : Challenged Accepted”
❤️
or connect with me in LinkedIn to discuss more !
#Softwaretesting #softwaretestere #SDLC #STLC #ISTQB #Java101 #SeleniumJava #docker #dockercontainer #dockerimage #docker101