Introduction to Docker
Docker is a popular open-source containerization platform that allows developers to package and deploy applications in lightweight containers. These containers provide a consistent and isolated environment for the application to run, making it easier to develop, test, and deploy applications across different environments.
One of the key benefits of using Docker is that it allows developers to create portable and self-contained applications. With Docker, developers can package the application and all of its dependencies into a container, ensuring that the application will run the same way regardless of the host environment. This makes it easier to move the application between different environments, such as development, testing, and production, without having to worry about compatibility issues.
Another advantage of Docker is that it allows developers to create scalable and highly available applications. Docker containers can be easily run on multiple hosts and orchestrated with tools like Kubernetes to create a cluster of containers that can automatically scale up or down based on demand. This makes it easy to build applications that can handle large amounts of traffic and remain highly available, even in the face of failures or disruptions.
In addition, Docker provides a number of tools and features that make it easy to use and manage containers. For example, Docker offers a registry where developers can share and download pre-built containers, as well as a public registry called Docker Hub that contains a large number of popular open-source containers. Docker also provides a command-line interface and API that allow developers to easily manage and automate their containers.
Overall, Docker is a powerful platform that allows developers to build, deploy, and manage applications in containers. By providing a consistent and portable environment for applications to run, Docker makes it easier for developers to create scalable and highly available applications that can be easily deployed across different environments.
Implementation of Docker
To work on this first install Docker from here. After downloading open the terminal. Run these commands:
$ docker ps # To see a list of containers inside docker
I am developing a JavaScript application on my localhost which uses the MongoDB database. We use MongoDB using the docker image.

Sample Workflow of Docker
- Use the docker pull command to pull the latest MongoDB and Mongo-express images from the Docker registry. For example:
- $ docker pull mongo
- $ docker pull mongo-express
- By default, Docker creates networks for the containers. But for the sake of our convenience, we will create a new network “mongo-network”.
- $ docker network ls
- $ docker network create mongo-network
- $ docker network ls
- Use the docker run command to start a new container from the MongoDB and Mongo-Express images that I pulled in step 1. This command will start the MongoDB database and make it available on our local machine. For example:
- $ docker run -d
-p 27017:27017
-e MONGO_INITDB_ROOT_USERNAME=admin
-e MONGO_INITDB_ROOT_PASSWORD=password
–name mongodb
–net mongo-network
Mongo
- Then we will get the container ID. To check the logs of this container to see what’s happing inside. For example:
- $ docker logs (container ID)
- There we will find that Network is saying “waiting for connection on port 27017”. We want to connect Mongo-Express to the running MongoDB container on startup. For example:
- $ docker run -d
-p 8081:8081
-e ME_CONFIG_MONGODB_ADMINUSERNAME=admin
-e ME_CONFIG_MONGODB_ADMINPASSWORD=password
–name mongo-express
–net mongo-network
-e ME_CONFIG_MONGODB_SERVER=mongodb
mongo-express
- Then we will get the container ID. To check the logs of this container to see what’s happing inside. For example:
- $ docker logs (container ID)
- we will get this,
- Welcome to mongo-express
————————
Mongo Express server listening at http://0.0.0.0:8081

- Now we have to connect the NodeJS server with the MongoDB container. The way to do this is usually to give a protocol of the database and the UI, and the UI for the MongoDB database would be localhost and the port that is accessible.
- We are going to use the MongoClient here, which is a node module, and using that MongoClient we are connecting to the MongoDB database(“user-account”).
- MongoClient.connect(mongoUrlLocal, mongoClientOptions, function (err, client)
let mongoUrlLocal = “mongodb://admin:password@localhost:27017”;
let mongoUrlDocker = “mongodb://admin:password@mongodb”;
let db = client.db(‘user-account’);
- Start our nodejs application locally – go to the app directory of the project Install the latest version of nodejs: https://nodejs.org/en/
- npm install
- node server.js
- Access the nodejs application UI from the browser
- When we are done developing the application, use the docker stop command to stop the MongoDB container. For example:
- $ docker stop mongo

- Using Docker-Compose:
- docker-compose -f docker-compose.yaml up
- docker-compose -f docker-compose.yaml down
- When we restart a container, everything that we configured in that container’s application will be gone. So data is lost. To say there is no data persistence in the containers themselves.
By following these steps, we can develop a JavaScript application that uses the MongoDB database of the Docker running on our local host. This allows us to take advantage of the benefits of Docker, such as portability and isolation, while still being able to develop and test the application locally.