DEV Community

Zhalok Rahman
Zhalok Rahman

Posted on

My Docker Experience as a Beginner.

Recently I have started to get my hands dirty with docker. Here I would like to share some of my silly mistakes as a beginner and how I solved them.
Basically, I was trying to dockerize my MySql database instance as me and my partner are working on completely different systems.
Starting from installing docker in my system to creating the run the MySQL in a container was fine. But the problem arose when I wanted to connect my node js application with my dockerized database.
Now before I explain the problem that I was facing I would like to share a bit of knowledge on how to connect a node js application with a MySQL database in short. For connecting node js application to any MySQL database server, we typically use a MySQL client using an npm package called "MySQL". https://www.npmjs.com/package/mysql. Now for establishing the connection between my node js app and MySQL server, there is an API provided by the "MySQL" npm package called "createConnection()". In the function, there are some options that need to be passed. A typical createConnection function looks something like this:

`const mysql = require("mysql");
const connection = mysql.createConnection({
host:"localhost",
port:"3306",
user:"root",
password:"03041959",
insecureAuth:true,
database:"BookLook",

});`

The problem was that I did not understand what the port number and the host needed to be used to connect with my dockerized database. Before jumping into the actual solution that worked, I would first like to go through the failed solutions that I initially tried and why they failed.
Solution1: I first ran the MySQL server in the docker container, then found the hostname using the command "select @@hostnames" and I got the docker container ID as the result, so I thought probably a MySQL server running in a docker container should have the hostname as the docker container ID. Then I changed the host value from "localhost" to "". But it did not work out.
The reason that this solution failed is that even if the MySQL instance is running inside a docker container, they are still on the same machine. Therefore, the dockerized MySQL instance is also running on localhost. So the host was basically not the problem. Then what was it? Why I was not able to connect and make queries to my dockerized MySQL instance using the NodeJS app. Well, the problem was with the port.
Here I would like to note something that may help in future similar problems. If you are experiencing communication issues between two processes running on the same server, you should be 90% certain that the issue is related to the port. Ports allow processes to communicate with one another. However, there are other ways for inter-process communication, such as "shared memory". However, that's an OS topic and I am not diving into it.
Now that I understood that the problem was with the port, I did some digging with the Docker communication via the port. Then I figured out that Docker has a concept called "port mapping". To learn about port mapping, you can read this article:

https://docs.docker.com/config/containers/container-networking/

In short, the docker mapping creates two ports. One for the Docker host and another one in the Docker container for running the application. The Docker host post is mapped to the Docker container port. So whenever you are trying to communicate with any dockerized application, you need to communicate via a docker host port and that will be redirected to the docker container port (where the application is running.
) that corresponds to the Docker host port. How can you specify the port mapping that is also in the link mentioned above?
Ok, so let's explain the final successful solution, I simply mapped the port 3306:3306. But while mapping the port I was facing some errors as this port was already in use by localhost as my MySQL-server was already running at 3306 port. Therefore, I closed the MySQL server and then ran my Docker container with the above mentioned port mapping. Boom, it worked.

Top comments (0)