Building Docker image
Now you have your Kubernetes running on Minikube or on AWS using kops. Lets’ see how to build your own container so that you can run container on Kubernetes using docker image. This will help you to build and run your own app on Kubernetes.
To build containers, you can use Docker Engine. Setup an Ubuntu/Bionic64, I will use Vagrant with Virtualbox.
Docker is available for Linux, MacOS, and Windows. Hence you can directly install docker on you Laptop/Desktop.
What is Dockerfile?
Dockerfile is use to Dockerizing a simple app. You can dockerize any app you want. Below is example of dockerizing NodeJS app.
Every container you build is need a Dockerfile. Following is a example of Dockerfile dockerizing a simple nodejs app.
Dockerfine example
FROM node:4.6-alpine WORKDIR /app ADD . /app RUN npm install EXPOSE 3000 CMD npm start
Let’s understand each line in Dockerfile.
Line #1: FROM node:4.6
– You are saying that your docker image will be based on node: 4.6-alpine image. Docker will download node’s tag 4.6-alpine from its official page https://hub.docker.com/_/node
Line #2: Our work directory going to be /app
Line #3: Current directory’s all file and folders are going to be copied in /app
directory.
Line #4: Prepare this docker image you need to run some commands. Like we will run npm install
to add all dependencies libraries that this app needs. So that when we will bundle this image. It will have all the dependencies.
Line #5: It’s going to expose a port. In this case its 3000
Line #6: When the container will start itself, then it will run a command npm start
Let’s have a look into the other file of this Nodejs application.
index.js
var express = require('express'); var app = express(); app.get('/', function (req, res){ res.send('Hello TimesofCloud!'); }); Var server = app.listen(3000, function(){ var host = server.address().address; var port = server.address().port; console.log('Example app listening at http://%s:%s', host, port); });
package.json
{ "name": "nodejs-demo", "version": "0.0.1", "private": true, "script": { "start": "node index.js" }, "engines": { "node": "^4.6.1" }, "dependencies": { "express": "^4.14.0" } }
Following are the commands to build and run the above docker image.
1. To build above project, docker build can be used.
2. Docker build can be executed manually or by CI/CD software like Jenkins.
3. Change directory to the docker-demo folder –
$ cd docker-demo
4. Do list files inside the docker-demo folder.
$ ls
Dockerfile index.js package.json
5. Build the docker image using
$ docker build -t vermanotes/nodejs-express:0.0.1 .
Output:
Sending build context to Docker daemon 2.05MB Step 1/6 : FROM node:4.6-alpine 4.6-alpine: Pulling from library/node 3690ec4760f9: Pull complete 625591d51582: Pull complete Digest: sha256:caad727da7ff74c0710d5da698531347175149acf691855ebf0b7ecb13d05fb9 Status: Downloaded newer image for node:4.6-alpine ---> 6c52813cab3f Step 2/6 : WORKDIR /app ---> Running in ea52b038a9bb Removing intermediate container ea52b038a9bb ---> a59a6e10ceb3 Step 3/6 : ADD . /app ---> 070369ba7c54 Step 4/6 : RUN npm install ---> Running in c19aa231091c npm info it worked if it ends with ok npm info using npm@2.15.11 npm info using node@v4.6.2 npm info preinstall myapp@0.0.1 npm info build /app npm info linkStuff myapp@0.0.1 npm info build /app/node_modules/accepts npm info preinstall accepts@1.3.7 npm info linkStuff accepts@1.3.7 npm info install accepts@1.3.7 npm info postinstall accepts@1.3.7 npm info build /app/node_modules/array-flatten npm info preinstall array-flatten@1.1.1 npm info linkStuff array-flatten@1.1.1 npm info install array-flatten@1.1.1 npm info postinstall array-flatten@1.1.1 npm info build /app/node_modules/body-parser npm info preinstall body-parser@1.19.0 npm info linkStuff body-parser@1.19.0 npm info install body-parser@1.19.0 npm info postinstall body-parser@1.19.0 npm info build /app/node_modules/bytes npm info preinstall bytes@3.1.0 npm info linkStuff bytes@3.1.0 npm info install bytes@3.1.0 npm info postinstall bytes@3.1.0 npm info build /app/node_modules/content-disposition npm info preinstall content-disposition@0.5.3 npm info linkStuff content-disposition@0.5.3 npm info install content-disposition@0.5.3 npm info postinstall content-disposition@0.5.3 npm info build /app/node_modules/content-type npm info preinstall content-type@1.0.4 npm info linkStuff content-type@1.0.4 npm info install content-type@1.0.4 npm info postinstall content-type@1.0.4 npm info build /app/node_modules/cookie npm info preinstall cookie@0.4.0 npm info linkStuff cookie@0.4.0 npm info install cookie@0.4.0 npm info postinstall cookie@0.4.0 npm info build /app/node_modules/cookie-signature npm info preinstall cookie-signature@1.0.6 npm info linkStuff cookie-signature@1.0.6 npm info install cookie-signature@1.0.6 npm info postinstall cookie-signature@1.0.6 npm info build /app/node_modules/debug npm info preinstall debug@2.6.9 npm info linkStuff debug@2.6.9 npm info install debug@2.6.9 npm info postinstall debug@2.6.9 npm info build /app/node_modules/depd npm info preinstall depd@1.1.2 npm info linkStuff depd@1.1.2 npm info install depd@1.1.2 npm info postinstall depd@1.1.2 npm info build /app/node_modules/destroy npm info preinstall destroy@1.0.4 npm info linkStuff destroy@1.0.4 npm info install destroy@1.0.4 npm info postinstall destroy@1.0.4 npm info build /app/node_modules/ee-first npm info preinstall ee-first@1.1.1 npm info linkStuff ee-first@1.1.1 npm info install ee-first@1.1.1 npm info postinstall ee-first@1.1.1 npm info build /app/node_modules/encodeurl npm info preinstall encodeurl@1.0.2 npm info linkStuff encodeurl@1.0.2 npm info install encodeurl@1.0.2 npm info postinstall encodeurl@1.0.2 npm info build /app/node_modules/escape-html npm info preinstall escape-html@1.0.3 npm info linkStuff escape-html@1.0.3 npm info install escape-html@1.0.3 npm info postinstall escape-html@1.0.3 npm info build /app/node_modules/etag npm info preinstall etag@1.8.1 npm info linkStuff etag@1.8.1 npm info install etag@1.8.1 npm info postinstall etag@1.8.1 npm info build /app/node_modules/finalhandler npm info preinstall finalhandler@1.1.2 npm info linkStuff finalhandler@1.1.2 npm info install finalhandler@1.1.2 npm info postinstall finalhandler@1.1.2 npm info build /app/node_modules/forwarded npm info preinstall forwarded@0.1.2 npm info linkStuff forwarded@0.1.2 npm info install forwarded@0.1.2 npm info postinstall forwarded@0.1.2 npm info build /app/node_modules/fresh npm info preinstall fresh@0.5.2 npm info linkStuff fresh@0.5.2 npm info install fresh@0.5.2 npm info postinstall fresh@0.5.2 npm info build /app/node_modules/http-errors npm info preinstall http-errors@1.7.2 npm info linkStuff http-errors@1.7.2 npm info install http-errors@1.7.2 npm info postinstall http-errors@1.7.2 npm info build /app/node_modules/iconv-lite npm info preinstall iconv-lite@0.4.24 npm info linkStuff iconv-lite@0.4.24 npm info install iconv-lite@0.4.24 npm info postinstall iconv-lite@0.4.24 npm info build /app/node_modules/inherits npm info preinstall inherits@2.0.3 npm info linkStuff inherits@2.0.3 npm info install inherits@2.0.3 npm info postinstall inherits@2.0.3 npm info build /app/node_modules/ipaddr.js npm info preinstall ipaddr.js@1.9.0 npm info linkStuff ipaddr.js@1.9.0 npm info install ipaddr.js@1.9.0 npm info postinstall ipaddr.js@1.9.0 npm info build /app/node_modules/media-typer npm info preinstall media-typer@0.3.0 npm info linkStuff media-typer@0.3.0 npm info install media-typer@0.3.0 npm info postinstall media-typer@0.3.0 npm info build /app/node_modules/merge-descriptors npm info preinstall merge-descriptors@1.0.1 npm info linkStuff merge-descriptors@1.0.1 npm info install merge-descriptors@1.0.1 npm info postinstall merge-descriptors@1.0.1 npm info build /app/node_modules/methods npm info preinstall methods@1.1.2 npm info linkStuff methods@1.1.2 npm info install methods@1.1.2 npm info postinstall methods@1.1.2 npm info build /app/node_modules/mime npm info preinstall mime@1.6.0 npm info linkStuff mime@1.6.0 npm info install mime@1.6.0 npm info postinstall mime@1.6.0 npm info build /app/node_modules/mime-db npm info preinstall mime-db@1.42.0 npm info linkStuff mime-db@1.42.0 npm info install mime-db@1.42.0 npm info postinstall mime-db@1.42.0 npm info build /app/node_modules/mime-types npm info preinstall mime-types@2.1.25 npm info linkStuff mime-types@2.1.25 npm info install mime-types@2.1.25 npm info postinstall mime-types@2.1.25 npm info build /app/node_modules/ms npm info preinstall ms@2.0.0 npm info linkStuff ms@2.0.0 npm info install ms@2.0.0 npm info postinstall ms@2.0.0 npm info build /app/node_modules/negotiator npm info preinstall negotiator@0.6.2 npm info linkStuff negotiator@0.6.2 npm info install negotiator@0.6.2 npm info postinstall negotiator@0.6.2 npm info build /app/node_modules/on-finished npm info preinstall on-finished@2.3.0 npm info linkStuff on-finished@2.3.0 npm info install on-finished@2.3.0 npm info postinstall on-finished@2.3.0 npm info build /app/node_modules/parseurl npm info preinstall parseurl@1.3.3 npm info linkStuff parseurl@1.3.3 npm info install parseurl@1.3.3 npm info postinstall parseurl@1.3.3 npm info build /app/node_modules/path-to-regexp npm info preinstall path-to-regexp@0.1.7 npm info linkStuff path-to-regexp@0.1.7 npm info install path-to-regexp@0.1.7 npm info postinstall path-to-regexp@0.1.7 npm info build /app/node_modules/proxy-addr npm info preinstall proxy-addr@2.0.5 npm info linkStuff proxy-addr@2.0.5 npm info install proxy-addr@2.0.5 npm info postinstall proxy-addr@2.0.5 npm info build /app/node_modules/qs npm info preinstall qs@6.7.0 npm info linkStuff qs@6.7.0 npm info install qs@6.7.0 npm info postinstall qs@6.7.0 npm info build /app/node_modules/range-parser npm info preinstall range-parser@1.2.1 npm info linkStuff range-parser@1.2.1 npm info install range-parser@1.2.1 npm info postinstall range-parser@1.2.1 npm info build /app/node_modules/raw-body npm info preinstall raw-body@2.4.0 npm info linkStuff raw-body@2.4.0 npm info install raw-body@2.4.0 npm info postinstall raw-body@2.4.0 npm info build /app/node_modules/safe-buffer npm info preinstall safe-buffer@5.1.2 npm info linkStuff safe-buffer@5.1.2 npm info install safe-buffer@5.1.2 npm info postinstall safe-buffer@5.1.2 npm info build /app/node_modules/safer-buffer npm info preinstall safer-buffer@2.1.2 npm info linkStuff safer-buffer@2.1.2 npm info install safer-buffer@2.1.2 npm info postinstall safer-buffer@2.1.2 npm info build /app/node_modules/send npm info preinstall send@0.17.1 npm info linkStuff send@0.17.1 npm info install send@0.17.1 npm info postinstall send@0.17.1 npm info build /app/node_modules/serve-static npm info preinstall serve-static@1.14.1 npm info linkStuff serve-static@1.14.1 npm info install serve-static@1.14.1 npm info postinstall serve-static@1.14.1 npm info build /app/node_modules/setprototypeof npm info preinstall setprototypeof@1.1.1 npm info linkStuff setprototypeof@1.1.1 npm info install setprototypeof@1.1.1 npm info postinstall setprototypeof@1.1.1 npm info build /app/node_modules/statuses npm info preinstall statuses@1.5.0 npm info linkStuff statuses@1.5.0 npm info install statuses@1.5.0 npm info postinstall statuses@1.5.0 npm info build /app/node_modules/toidentifier npm info preinstall toidentifier@1.0.0 npm info linkStuff toidentifier@1.0.0 npm info install toidentifier@1.0.0 npm info postinstall toidentifier@1.0.0 npm info build /app/node_modules/type-is npm info preinstall type-is@1.6.18 npm info linkStuff type-is@1.6.18 npm info install type-is@1.6.18 npm info postinstall type-is@1.6.18 npm info build /app/node_modules/unpipe npm info preinstall unpipe@1.0.0 npm info linkStuff unpipe@1.0.0 npm info install unpipe@1.0.0 npm info postinstall unpipe@1.0.0 npm info build /app/node_modules/utils-merge npm info preinstall utils-merge@1.0.1 npm info linkStuff utils-merge@1.0.1 npm info install utils-merge@1.0.1 npm info postinstall utils-merge@1.0.1 npm info build /app/node_modules/vary npm info preinstall vary@1.1.2 npm info linkStuff vary@1.1.2 npm info install vary@1.1.2 npm info postinstall vary@1.1.2 npm info install myapp@0.0.1 npm info postinstall myapp@0.0.1 npm info prepublish myapp@0.0.1 npm info ok Removing intermediate container c19aa231091c ---> 1de49078fd9a Step 5/6 : EXPOSE 3000 ---> Running in 1c7c59af7faa Removing intermediate container 1c7c59af7faa ---> 1acb6b40bccd Step 6/6 : CMD npm start ---> Running in 6b3675dcddf6 Removing intermediate container 6b3675dcddf6 ---> f05a8e632beb Successfully built f05a8e632beb
Verifing docker image
➜ nodejs-express git:(master) docker images | grep vermanotes/nodejs-express vermanotes/nodejs-express 0.0.1 aea87118bef0 3 minutes ago 44MB
After the docker build process you have built an image that can run the nodejs app.
541 total views, 1 views today