Docker is cool. I’ve just started to use and play it for small projects. This part will cover the basics of building and running docker containers and images.

I started taking courses on Lynda.com which is given out free by my university and I went on a Binge Spree to do docker certifications for the same.

These are the basics you need to know about docker:
You create isolated, light-weight reproducible environments with Docker. You define OS, Packages and anything else you want inside the docker image and once you build it, it will be there ready for you. It works the same on all machines as it is completely isolated and does not differ/change depending on the OS you are running docker. This is a small Docker 101 tutorial. (Meant for me, can be used by you)

  • Docker creates stand-alone packages called containers that comprise of everything that is needed for you to run the application.
  • Each container gets its own CPU, memory and network resources and does not depend on a specific operating system or kernel. 
  • Docker IS NOT Virtual Machine. Docker works differently in how it shares/dedicates resources to containers.
  • It uses something called as Layered Files System which allows the containers to share common parts hence they consume less resources on the host system than a VM.

In simple terms, Docker containers, contain everything you need to run an application, including the source code you wrote. Containers are also isolated and secure light-weight units on your system. This makes it easy to create multiple micro-services that are written in different programming languages and that are using different versions of the same lib and even the same OS.

Steps

  • create an application
  • create a Dockerfile
  • build an image
  • create a container

We will be creating an Express Node.js project which will consist of the following files:

app.js: this is the file that spins up the REST
package.json: this is the manifest file for the project, where we also write the dependancies and declare the start script.
Dockerfile: Basically a definition file to tell docker how to “Dockerize” the application.

// These terminals are scrollable

This is the app.js file

Go to http://localhost:3000 and you should see a “Hello World” displayed there.

Creating the DockerFile

This acts as a manifest but also as a build instruction file, how to get our app up and running.
This is the DockerFile that is needed for our application.

// These terminals are scrollable

Assuming you are standing in the directory of the project, you will have to build the dockerfile by running:

“docker build -t divyendrapatil/node:latest .”

Do not forget the “.” at the end as it the location of the dockerfile (For Us, it is the same directory)

docker build -t divyendrapatil/node:latest .

We need to map the internal port of the app to an external one, on the host machine.
We do the mapping by using the flag -p by:

docker run -p 8000:3000 divyendrapatil/node

We need to keep track of the port when we start the express server inside of our app.js , to make sure this matches what we write in the Dockerfile. It shouldn’t have to be that way, it’s just static and error-prone.

By doing this, we git rid of static values and rely on variables.

// These terminals are scrollable

Managing the container: