Docker yeVatangi: Chii chinonzi Docker uye Maitiro Ekugadzira Docker Midziyo

Dai wanga uchitaurirana nezvazvino kuitika muhurongwa hwepasi mumakore mashoma apfuura, iwe ungadai wakacherekedza ruzha rukuru rwakakomberedza midziyo yeDocker neDocker. Uku kufarirwa kwe docker hakusi pasina chikonzero. Kuunzwa kweDocker kwakachinja zvakanyanya maitiro evanogadzira ekuvandudza kunyorera.

Ndiani anoda kusiiwa kumashure kana tekinoroji yakadaro ichirova nyika yepurogiramu? Saka, nhasi, isu tiri kutanga nyowani yedzidziso nhevedzano yekuti iwe udzidze mashandisiro eDocker yekuvandudza kunyorera. Kana iwe uri wekutanga mhedzisiro kuDocker, ino tutori yakateedzana ndiyo nzvimbo chaiyo yekuti iwe utange.

Muchinyorwa chekutanga chedu tutori akateedzana, isu tiri kutsvaga kuti tinzwisise kuti chii chaizvo chinonzi Docker uye nei vashanduri vachida Docker zvakanyanya. Isu tichave zvakare tichi dockerizing yakapusa Node.js application kukuziva iwe nezvakakosha pfungwa dzeDocker.

Kumirirei zvakare? Ngatitange!

Chii chinonzi Docker

Docker chishandiso chinoshandiswa kuvaka zvinoshandiswa. ndiko kugadzira, kuendesa, uye kumhanya kunyorera kuburikidza nemidziyo.

Iine mudziyo, maraibhurari ese, uye kumwe kutsamira kunodiwa kumhanyisa kunyorera, zvakarongedzwa sepakeji rimwe chete rekuendesa.



Chinangwa chikuru chekuisa chigadziro chiri kuvapatsanura kubva kune zvimwe zvinoshandiswa zvinoshanda mune imwecheteyo sisitimu. Maitiro aya anovimbisa kunyorera hakukanganise mashandiro emumwe uye kunoita kuti kugadziriswa kwekushandisa kuve nyore.

Kunyangwe midziyo inomhanya mune imwecheteyo system yakasarudzika kubva kune imwe kune imwe mukuitwa, inogovera imwechete OS kernel. Nekudaro, midziyo yakareruka kana ichienzaniswa neimwe sarudzo yekuparadzanisa kuisirwa kwekushandisa, chaiwo michina.

Chishandiso chine mudziyo chinoshanda paWindows OS yako chakavimbiswa kumhanya pasina nyaya mune mumwe mushandisi weWindows muchina kunyangwe paine shanduko yenzvimbo.

Kunyangwe midziyo yagara kwenguva refu ichishandiswa pamberi peDocker, kuunzwa kweDocker kwakakurumbira uchishandisa midziyo munharaunda yekuvandudza. Pane zvinhu zviviri zvinoshandiswa pakusimbisa chikumbiro: Dockerfile uye Docker Mufananidzo . Ngationei kuti ndezvipi.

Dockerfile

Dockerfile iri faira remavara iro rine seti yemirairo inodiwa kuvaka docker mufananidzo. Dockerfile rine ruzivo nezve iyo yepasi OS, mutauro, faira nzvimbo, uye network mapoti pakati pezvimwe zvinhu.

Docker Mufananidzo

Paunomhanyisa iyo Docker yekuvaka yekurayira ine dockerfile munzvimbo, mufananidzo we docker unogadzirwa zvichibva pane dockerfile. Ivo vanoita sematemplate ekugadzira iyo yekupedzisira docker mudziyo. Kamwe yagadzirwa, docker mifananidzo yakamira. Unogona kukumbira iyo Docker yekumhanya kuraira kuti ugadzire iyo docker mudziyo uchishandisa docker mufananidzo.

Kugadzirisa Node.js Kushanda

Mune ino dzidziso, isu tichaenda kuti dockerize iyo Node.js application. Isu tinoteedzera nhanho nhanho nzira yekusimudza iyo Docker mudziyo kumusoro uye unomhanya.

1 - Gadzira iyo Node.js application 2 - Gadzira iyo dockerfile 3 - Gadzira iyo docker mufananidzo 4 - Gadzira mudziyo wekushandisa

Usati wadzika kuti dockerizing app yedu, unofanirwa kuona kuti Docker neNode.js zvakaiswa mune yako system

  • Isa Docker pane yako system-ini handizofukidza maitiro ekuisa Docker mune ino dzidziso, asi iwe unogona kutevera Docker zvinyorwa uye kuisa Docker pane yako Windows kana Ubuntu desktop.
  • Dhawunirodha uye Isa Node.js kubva kune yepamutemo webhusaiti

Gadzira iyo Node.js Kunyorera

Enda kune dhairekitori nyowani yeprojekti kubva kumutsara-wekuraira uye unomhanya unotevera kuraira kuti ugadzire iyo package.json faira iro rine ruzivo nezve kutsamira kwekushandisa uye kutanga script.

npm init -y

Wobva waisa uye wedzera Express seyakavimbika kune yako application nekumhanya uyu murairo pane yekuraira-tambo. Tichave tichishandisa Express kugadzira iyo app.

npm install express --save

Izvi zvinowedzera kutaura sekuvimbika kune kwedu package.json faira.

Iye zvino tinogona kugadzira Node application nerubatsiro rwe Express.

Gadzira faira rakanzi app.js mune dhairekitori reprojekiti uye wedzera inotevera kodhi kune iyo faira.

const express = require('express') const app = express() app.get('/', (req, res) => {
res.send('Hello World!') }) app.listen(3000, () => {
console.log('Node server has started running') })

Iyi iri pamusoro kodhi inogadzira iyo Node server iyo inoteerera kune zvinouya zvikumbiro pachiteshi 3000. Unogona kumhanya uyu murairo pane yekuraira-mutsara kuti utange iyo Node server.

node app.js

Zvino enda kubhurawuza rako uye uteedzere iyo URL http://localhost:3000 uye iwe uchaona iwo mavara Hello World! papeji.

Isu takavaka yakapusa Node kunyorera kweprojekiti yedu. Iye zvino ngatienderere mberi nekugadzira iyo dockerfile.

Gadzira iyo Dockerfile

Mune iyo dockerfile, isu tinopa ruzivo rwunodiwa kuvaka nekumhanyisa yedu Node app iine iyo Docker nharaunda.

Izvi zvinosanganisira kudoma mutauro uye neshanduro yaro inoshandiswa mukushandisa, kumisikidza dhairekitori reprojekti sedhairekitori rinoshanda, kuteedzera mafaera ese mudhairekitori rinoshanda, kumisikidza doko renetiweki, uye kutsanangura kuti ndeipi faira inzvimbo yekupinda mukushandisa. Mune mamwe maitiro akaomarara, iwe uchafanirwa kuseta zvinopesana nharaunda uye URL yedhatabhesi mu dockerfile futi.

FROM node:latest WORKDIR /dockerTutorial COPY . . RUN npm install EXPOSE 3000 ENTRYPOINT ['node', 'app.js']
  • Kubva raira rinotora mufananidzo weOS, kumhanyisa chikumbiro chedu pane yakatarwa OS, kubva kuDocker Hub. Docker Hub inzvimbo yemifananidzo ye docker yatinogona kukwevera kunharaunda yemuno. Tiri kutora mufananidzo weUbuntu-wakaisa iyo Node.js. Uchishandisa 'yazvino' seiyo Node vhezheni inokweva chifananidzo chine yazvino Node vhezheni yakaiswa.
  • KUSHANDIRA rairo inoseta dhairekitori rinoshanda rekushandisa.
  • KOPA raira kuteedzera mafaera kubva kudhairekitori razvino (pane rairo-mutsara) kune dhairekitori rinoshanda iro rakaiswa mudanho rekutanga. Iwe unogona kudoma zita refaira kuti ukope kana kushandisa zvakapetwa zvizere kuzoteedzera mafaera ese mune dhairekitori razvino kune dhairekitori rinoshanda.
  • MHANYA raira inoisa zvese zvinoenderana nezvinodiwa kuvaka application. Izvi zvinosanganisira zvese zvinoenderana zvinotsanangurwa mu package.json faira.
  • FUMANIRA rairo inovhura chiteshi kubva kuDocker mudziyo kune yekunze nyika. Ichi chiteshi chinogamuchira zvese zvikumbiro zvatino tumira kune iyo Docker mudziyo. Chiteshi chakanyatso kumisirwa ku3000 nekuti ndiyo chiteshi cheNode application mukati meDocker mudziyo unoshandisa kuteerera zvikumbiro.
  • ENTRYPOINT inotsanangura maitiro ekutanga iko kunyorera. Docker ajoina rondedzero yatinopa kune imwechete kuraira kuti utange kunyorera. Mune ino kesi, node app.js.

Kuvaka iyo Docker Mufananidzo

Shandisa unotevera kuraira kugadzira iyo Docker mufananidzo kubva kune dockerfile.

docker build -t docker-tutorial .

Docker-tutorial izita remufananidzo weDocker. Iyo dot inoratidza nzira yefaira kune dhairekitori reprojekti, ndiko kwatiri parizvino mumutsetse-wekuraira.

Kana iyo OS chifananidzo chakatsanangurwa ne Kubva raira, node: ichangoburwa , haisi mumudziyo wako panguva ino, ichadhonzwa kubva kuDocker Hub kana iwe uchimhanyisa iwo pamusoro kuraira.

Mushure mekudhonza iwo mufananidzo, rairo yega yega mu dockerfile ichaitwa imwe neumwe.

Pakupera kwekuurayiwa, kana iwe ukaona iwo meseji kubudirira kuvakwa , iyo docker mufananidzo weiyo application yakavakwa zvinobudirira. Mhanyai uyu murairo kuti uone yakavakirwa docker mufananidzo mune yemuno chifananidzo.

docker images

Zvinobuda zvinoita kunge izvi

Kugadzira Mudziyo

Iye zvino tinogona kushandisa yakavakirwa mufananidzo kugadzira yedu Docker mudziyo. Shandisa iyo docker run raira kugadzira mudziyo.

docker run -p 8080:3000 docker-tutorial

Pano, iyo nhamba 8080 uye 3000 inoratidza yekunze neyemukati memudziyo. Chiteshi chekunze, 8080, ndicho chiteshi chatinoshandisa kubatana kuchishandiswa kuburikidza nebhurawuza redu. Yemukati chiteshi, 3000, ndicho chiteshi chedu chishandiso chinoteerera zvikumbiro zvinouya. Docker mudziyo wemamepu yakapihwa kunze chiteshi kune yemukati chiteshi.

Shanyira iyo URL http://localhost:8080 pane bhurawuza uye uone kana iwe ukawana iyo peji ne Hello World! meseji yawakawana pakushanya http://localhost:3000 pamberi. Kana hongu, ipapo mudziyo wako weDocker wakwira uye unomhanya.

Unogona kushandisa uyu kuraira kuona ese anomhanya Docker midziyo pane yako kifaa.

docker ps

Iwo wekuraira uchakupa chinobuda seichi. Tinogona kuwana CONTAINER_ID neZita remudziyo unomhanya pano.

Kuwedzera Zvinoshanduka zveMamiriro ekunze kune Chako Chishandiso

Rangarira kuti ndataura sei chishandiso nemisiyano yezvakatipoteredza inoda mimwe mirayiridzo mu dockerfile? Iko kukosha kwekushanduka kwezvakatipoteredza nharaunda dzavari kumhanya mazviri.

Cherekedza matauriro atakaita zvakajeka chiteshi chedu Node app inoteerera kune iyo server painenge ichimhanya. Maitiro aya haachinjike uye anokanganisa. Kana isu tikamhanya kunyorera kwedu munzvimbo isingavhure chiteshi 3000 cheNode server, yedu application inomira kushanda.

Kuitwa kwakakodzera kwazvo kutora nhamba yechiteshi kunze kwekushandisa. Panzvimbo iyoyo, isu tinoshandisa rinoshandura zita munzvimbo yechiteshi nhamba uye kuseta kukosha kweiyo inosiyana munzvimbo inomhanya nharaunda. Kwedu, yedu yekumhanyisa nharaunda ndiyo Docker mudziyo. Nekudaro, isu tinofanirwa kuwedzera iyo port port kune iyo dockerfile semamiriro ekusiyana.

Ngatione kuti tingaite sei izvozvo.

Kutanga, wedzera musiyano wezvakatipoteredza kune yedu dockerfile nemutengo wayo. Isu tinofanirwa kuwedzera murairo mutsva ku dockerfile kuti tiite izvi.

FROM node:latest WORKDIR /dockerTutorial COPY . . ENV PORT=3000 RUN npm install EXPOSE $PORT ENTRYPOINT ['node', 'app.js']

Tichishandisa iyo ENV yekuraira ichiteverwa neshanduko zita uye kukosha kupihwa, tinogona kuwedzera nyowani yemamiriro ekusiyana kune yedu dockerfile. Wakaona here kuti iyo EXPOSE 3000 rairo yakachinjwa sei kuti isataure zvakajeka nhamba yechiteshi? Pane kudaro, zvinoreva kune yakasikwa PORT kusiana kuti uwane iyo chaiyo port port. Nenzira iyi, kana tichifanira kushandura chiteshi nhamba, isu tinofanirwa kungochinja nzvimbo imwe mukodhi yedu, izvo zvinoita kuti application yedu ive nyore kuchengetedza.

Zvino isu tachinja iyo dockerfile, inotevera nhanho iri kushandura iyo app.js kureva kune yakasikwa nharaunda inoshanduka. Kune izvi, isu tinotsiva chiteshi nhamba 3000 inoshandiswa mukati mekuterera nzira ne process.env.PORT.

const express = require('express') const app = express() app.get('/', (req, res) => {
res.send('Hello World!') }) app.listen(process.env.PORT, () => {
console.log('Node server has started running') })

Sezvo isu takaita shanduko kumafaira edu ekushandisa uye dockerfile, tinofanirwa kuvaka chifananidzo chitsva chemudziyo mutsva. Asi kutanga, isu tinofanirwa kumisa ikozvino irikumhanya Docker mudziyo kuti uwane izvi.

Tinogona kushandisa docker stop raira kumisa mudziyo.

docker stop f10

Iko kukosha, f10, kunoshandiswa mumurairo uyu ndiwo manhamba matatu ekutanga eID yemudziyo.

Tinogona kushandisa rairo, docker kuuraya, kumisa mudziyo unomhanya.

docker kill f10

Musiyano uripo pakati pe docker kuuraya uye docker kumira ndeyekuti docker inomira inomisa mudziyo nerunako nekusunungura uchishandisa zviwanikwa nekuchengetedza nyika docker kuuraya, zvisinei, inomisa mudziyo nekukurumidza pasina kunyatso kusunungura zviwanikwa kana kuchengetedza nyika. Kune mudziyo unomhanya munzvimbo yekugadzira, kushandisa docker kumira kumisa mudziyo ndiyo sarudzo iri nani.

Mushure mekumisa mudziyo unomhanya, ita shuwa yekuchenesa zvakasara nemudziyo kubva kunzvimbo yekutambira uchishandisa unotevera kuraira.

Kumhanya neContainer muDaemon Mode

Paunoyedza kumhanya pamusoro apa mirairo yekumisa mudziyo, waizoona kuti iyo yekupedzisira tebhu yataishandisa kugadzira mudziyo haigone kushandiswa kumhanyisa mimwe mirairo kunze kwekunge tauraya mudziyo. Tinogona kuwana yekushandira yeizvi nekushandisa yakaparadzaniswa tebhu yekumhanya mitsva mirairo.

Asi pane nzira iri nani. Tinogona kumhanyisa mudziyo mune iyo daemon modhi. Iine daemon modhi, mudziyo unomhanya kumashure usingashandisi yazvino tebhu kuratidza zvinobuda.

Kutanga mudziyo mune iyo daemon modhi, iwe unofanirwa kungo wedzera yekuwedzera -d mureza kune iyo docker run raira.

docker run -d -p 8080:3000 docker-tutorial

Kumhanya neContainer muInternet Mode

Kuti umhanye mudziyo mune inofambidzana modhi, iyo mudziyo inofanira kunge yatove kumhanya. Kamwe mune inofambidzana modhi, unogona kumhanya mirairo yekuwedzera kana kubvisa mafaera mumudziyo, nyora mafaera, kana kumhanya mimwe bash mirairo yatinowanzo shandisa.

Shandisa unotevera kuraira kuti umhanye mudziyo mune yekudyidzana modhi.

docker exec -it e37 bash

Pano, e37 ndiyo ID yemidziyo. Tamba uchitenderedza nemhando yekudyidzana uchishandisa bash mirairo.

Mhedziso

Muchidzidzo chekutanga chedu Docker dzidziso dzakateedzana, iwe wakadzidza kugadzira Docker mudziyo weiyo yakapusa Node.js application. Asi pane zvimwe zvaunogona kuita neDocker nemidziyo. Mune edu ari kuuya ezvidzidzo, isu tichaona mashandiro nema dhatabhesi, mavhoriyamu, uye kushanda nemazhinji makomputa anoshandiswa nechishandiso chakavakwa nema microservices.