If tutorials available on this website are helpful for you, please whitelist this website in your ad blocker😭 or Donate to help us ❤️ pay for the web hosting to keep the website running.

Docker Container
Docker एक platform है , जिसका use करके developers container के साह application build , share और run करते हैं।
for example, जो project 2 years पहले start हुआ तो उसे आज के time पर deploy करते time उसमे dependency issues आ सकते हैं because 2 साल से हर package me update हुआ होगा , even programming language भी update हुई होगी।
इसी problem को docker solve करता। हम अपने application की एक image बनाकर एक container के रूप में run करते हैं। docker में हम जिस भी container में application / micro service run करते हैं उसमे सिर्फ उसी application की dependencies रहती है।
अब आप project की image बनाकर उसे कभी भी run करेंगे तो सभी dependencies आपके project के according ही associate होगी।
●●●
अब आगे बढ़ने से पहले हम कुछ terminologies समझ लेते हैं , जैसे Image / Container / hub क्या है ?
ये project का templates है , इसके अंदर ही सभी tools, code , libraries etc होती है।
ये ready to use software , read only templates हैं , मतलब एक बार image बना दी फिर आप उसके अंदर का code change नहीं कर सकते , इसके लिए आपको code change करके दूसरी image ही बनानी पड़ेगी।
Images 2 type की होती है , एक custom जो हम अपने application की बनाते हैं और दूसरी , application me use hone वाली tools/tech जिसे हम नहीं बना सकते हैं। जैसे nodejs / php / npm ,इनकी image हम नहीं बना सकते। इन images को parent image बोलते हैं।
●●●
ये Image का running instance है , मतलब जब image को run करते हैं तो उसे container कहते हैं। इसे आप process भी बोल सकते हैं जो application को image के साथ run करती है।
container isolated process है , मतलब ये machine par in-dependently run होते हैं , अगर machine पर NodeJS 16 installed है but image में nodejs 12 है तो container application को NodeJS 12 पर ही run करेगा।
●●●
Parent images को जहाँ से हम download करते हैं उस space को Docker Hub कहते हैं। ये एक online tools है जिसे browser / desktop application पर access किया।
हम अपने project की image बनाकर Docker hub में upload भी कर सकते हैं जिसे दुसरे users install करके run कर सकते हैं।
●●●
चलिए अब एक छोटा सा NodeJS का project बनाकर उसकी image बनाकर देख लेते हैं।
इसके लिए सबसे पहले तो docker की official website पर account बना ले , और desktop application को setup कर ले।
mkdir node1 cd node1 npm init npm install express
अब एक index.js file बनाये जिसमे नीचे दिया गया code रख ले।
const express = require('express');
const app = express();
const port = 8081;
// Custom middleware function to log request path.
function logRequestPath(req, res, next) {
console.log(`Hittng URL : http://localhost:${port}${req.path}`);
next();
}
// Mount the middleware to run for every request.
app.use(logRequestPath);
// define route
app.get('/', function (req, res) {
res.send('Welcome to learnhindituts.com');
});
app.get('/users', function (req, res) {
res.json([
{
name: "Paul",
age : 26,
profession : "Developer"
},
{
name: "John",
age : 90,
profession : "Project Manager"
}
]);
});
//listen server.
app.listen(port, function () {
console.log(`Server is running on port : ${port}`);
});इसे आप पहले local system पर run करके भी देख सकते हैं।
●●●
अब अपने project की root directory में dockerfile बनाये , जिसमे हम अपनी image की configuration define करेंगे।
# define parent image for project. FROM node:latest # specify working directory. WORKDIR /app # where we putting project. COPY . . # run nom install to install our project dependiecs. RUN npm install # make a port , by whch we will access our project. EXPOSE 8081 # Now run files CMD ["node","index.js"]
●●●
इसके बाद project की image बनाने के लिए नीचे दी गयी commands को run करे ।
docker build -t node1Image . # here dot is the path of your dockerfile , right now we are in root directory of the project.
image से related नीचे कुछ और commands दी गयी हैं।
# see all images. docker images # remove image. docker image rm imageName # remove image when it's container is running. here only image get's deleted. docker image rm imageName -f # delete all images & containers. docker system prune -a
●●●
Image create होने के बाद आप उस image को run कर सकते हैं। हालाँकि आप command line के अलावा direct desktop application से भी image run कर सकते हैं।
docker run --name node1-container -p 8081:8081 node1Image
बस ये command run करते ही आपका container successfully run हो जायगा जिसे आप localhost:port के साथ access कर सकते हैं।
●●●
I Hope ,आपको Docker के बारे में ये basic जानकारी पसंद आयी होगी।
Loading ...