Introduction To Docker

Photo by Ian Taylor on Unsplash

Introduction To Docker

Introduction

Before understanding what is docker let's try to find the answer to the question "why Docker". Any software developed is for solving a particular problem statement. Let's consider a situation in which you developed an application and send your source files to your manager for production or a friend to test it out. Even though your application works fine on your machine when your friend or manager runs it on their machine or in a production server it just didn't work. There can be many reasons for this, lets say your application was build using nodejs 14 but the person who ran that has node 12 in their system or you developed the app in a Linux environment but the person who ran it might have used windows and it crashed the app. This problem of "it works on my machine" is what docker tries to solve. So what if there is a way to ship your machine to production rather than shipping your source code, this will ensure that the program works where ever it is made to run. Dockerising an application helps us to do this, in the upcoming sections we will try to see how to do it.

Installation

Installing docker is pretty straightforward, you can follow these instructions according to your operating system docs.docker.com/get-docker. It's a good thing to note that in all these installation setups docker is installed inside a Linux machine or a Linux virtual machine(Docker Desktop), that is because docker only runs operating systems with the same kernel. If you don't understand this do not worry, you will get this once you use are familiarised with docker.

Dockerfile, Docker Images, Docker Containers

Now let's understand some important terms in Docker. Let's start with Docker Images, as the name suggests it is a snapshot of the machine you need to ship to production and a docker file is a blueprint to configure how your image should be built. Once we build a docker file and create a docker image, we can run the image to create a container which is a process that runs our applications in the specified environment

Containerizing a Node Js Application

Let's try to use all the things we learnt till now and containerise a simple Nodejs Application. You can find the starter code here. Once you are ready with the starter nodejs application let's begin by creating a DockerFile in the root directory of the application.

FROM node:12

WORKDIR /app

COPY package.json ./

RUN npm install

COPY . .

ENV PORT=8080

EXPOSE 8080

CMD [ "npm", "start" ]

Now copy and paste the following contents into the Dockerfile file. Let's break down each of these statements. The FROM command specifies the base image or the starting image that we are using , it can be ubuntu, centos or something particular like node which already have node js installed inside that. next, we specified the working directory, this is where all our files should be stored. the copy command copies the package.json to the working directory then the RUN command runs 'npm install' which installs the necessary packages needed. then we copy the rest of the app to the working directory. The ENV sets an Environment variable to 'port' then we Expose the port and finally starts the application using the CMD command

Now let's build an image using the docker file we made. Use the following command to run the docker file.

docker build -t node-docker-demo:v1 .

This command builds our docker image.

docker images ps

the above command can be used to show all the docker images in our system

Now lets the run the image in a container

 docker run -p 8080:8080 -d node-docker-demo:v1

the above command starts and the container runs our application, then expose the port 8080 to the host machine. Now you can visit localhost:8080 in your browser to view our app.

Congrats on running your first containerized application, now we can stop the container
Run the following command to get the list of containers currently running in our machine

docker ps

copy the id of the container that we started and run the following command to stop it.

docker stop <container_id>

Conclusions

That was a small introduction to docker, if you are more curious checkout more commands and options in the official documentation Follow me for more content like this