DEV Community

Fabio Oliveira Costa
Fabio Oliveira Costa

Posted on • Edited on

Introduction to container based development - Part 2/4

This is the second part of our container based development series. This post is based on the container development article created by me.

Creating our image - Pressing our own CD.

We are going create a node program its behavior is the following.

  • try to find a file and read from it;
  • output the content of the file;
  • save the content +1;
  • repeat every 1 second until 19.
  • if no content is found it will create the file and save with 0;

The code is on the repository on the firstContainer branch.

nodeCounter.js

const fs = require('fs')
const path = require('path')

const counterFile = path.resolve(__dirname, './counter.txt')

const COUNTER_LIMIT = 20
const INTERVAL = 1000
let counterData = 0
if (fs.existsSync(counterFile)) {
  const fileData = fs.readFileSync(counterFile)
  counterData = parseInt(fileData, 10)
}

const recursiveCounter = () => {
  if (counterData === COUNTER_LIMIT) {
    process.exit(0)
  }
  console.log(counterData)
  counterData++
  fs.writeFileSync(counterFile, counterData)
  setTimeout(recursiveCounter, INTERVAL)
}

recursiveCounter()

With this code on some folder we also need to configure our container, to make how our CD is going to be built.

Dockerfile (No extension)

FROM node:13-alpine3.10
RUN mkdir -p /app/ourApp/
COPY ./nodeCounter.js  /app/ourApp/
WORKDIR  /app/ourApp/
CMD node ./nodeCounter.js

The dockerfile above have some commands

  • FROM - We are using a Node image that uses the alpien linux operating system.
  • RUN - On this image we are creating a folder.
  • COPY - We are copying our node program to the folder.
  • WORKDIR - We change the working dir so all commands will run starting from this new location.
  • CMD - We say which command should run as soon as you play the CD.

To make our cd we execute the following on the terminal, it says to build a image called container_dev with the tag first using the dockerfile on the same folder I am executing the command.

docker build -t container_dev:first  .

The output will be something like:

Sending build context to Docker daemon  70.66kB
Step 1/5 : FROM node:13-alpine3.10
.......
Successfully built 50c9fae3a235
Successfully tagged container_dev:first

If we want to see our images we can type

docker images

To see our container data like Ids, names , tags and so on.

REPOSITORY                              TAG                 IMAGE ID
container_dev                           first               50c9fae3a235       
node                                    13-alpine3.10     

Now that our image has a name and a tag we can run it:

docker run container_dev:first

And you should see a count from 0 to 19 and our image stopping. Congratulations you just created your first image!
This image is saving data on a file, run it again if we persisted data it should stop immediately since we are already on the 20 but now it will start again from 0, run it twice with a little delay each count will not interfere with each other. That is part of the security of containers they are isolated.

Our next topic: Volumes!

Top comments (0)