Docker, container, dockerfile and or minikube? (part 1)

If you are like me often struggling with the decision to choose your best poison ;) in your development, I can share you my poison.

In my journey to be DevOps, I am constantly asked to adopt the coolest technology on the market. Docker is on of them. So lets begin with it, docker is really great technology to put your development into container. If you ask me, why we even want to put everything in container ? The answer is quite simple, for me it is to eliminate the debate, about this app works on my local machine ;).

Once you work with nodeJS for long time, your library will expand until you don’t know anymore, and docker can help easing your pain, if you can eliminate the first barricade for the first time.

In this story I will put a simple nodeJS project. Lets start:

  • for this project I am using docker for mac which can be dowloaded here and node version 12
  • for the sake of easiness to follow I will add the file name as commentary on first line, if you copy paste the code you can remove it ;)
  • Create your own directory and add those 2 files below there
  • on your package.json
{
"name": "docker_web_app",
"version": "1.0.0",
"description": "Node.js on Docker",
"author": "kura2ninja",
"main": "index.js",
"scripts": {
"start": "node index.js"
},
"dependencies": {
"express": "^4.16.1"
}
}
  • on your index.js
var express = require('./node_modules/express');var app = express();app.get('/', function(req, res){
res.send('Hello World !!');
});
/* istanbul ignore next */
if (!module.parent) {
app.listen(3000);
console.log('Express started on port 3000');
}
  • run npm install
  • then run npm run start
  • then you can see your localhost:3000 showing ‘Hello World !!’
  • Congratulations ! :D first baby step NodeJS project is done
  • As bonus if you want to watch file changes and tired of running npm run start every time you change your file, in this case index.js, then you can add nodemon as global dependency
  • to install nodemon npm i -g nodemon
  • instead of calling node, now we can call nodemon to watch our files
"scripts": {
"start": "node index.js",
"watch": "nodemon index.js"
},
  • now we can call npm run watchinstead of npm run start

So, that was pretty easy huh ;) Now let’s move to the second part how to container your app in docker. As mentioned above, docker is magical to isolate your app. Let’s start docker:

  • check if you install docker properly by calling docker -v
  • now create your first Dockerfile
FROM node:lts# Create app directory
WORKDIR /usr/src/app
# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
COPY package*.json ./
RUN npm install
# If you are building your code for production
# RUN npm ci --only=production
# Bundle app source
COPY . .
EXPOSE 3000
CMD [ "node", "index.js" ]
  • Yay Dockerfile is done !, Lets build it with this command docker build -t mynodeapp -f Dockerfile .
  • Hopefully all is successfully build and you can run it.
  • Let’s give it a shot docker run -p 3000:3000 mynodeapp:latest , if all works as expected you can call your app in localhost:3000

Congratulations you have build your first containerized app, and this will end first chapter of this tutorial. :D

In the next chapter we will discuss about minikube and how to create deployment file :)

Please do not hesitate to give me feedback or new idea to be discussed :D

I am on my journey into DevOps world and always curious with the new technology :)

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store