FewV minutes of What is docker

» What is Docker?

  1. Docker is a technology that allows you to package and run software applications in a consistent and isolated environment. It’s like a container for your applications.

  2. Imagine you have a special box called a “docker container.” Inside this container, you can put all the necessary things your application needs to run, such as the code, libraries, and dependencies. It’s like a self-contained package.

  3. Docker helps you keep everything organized and ensures that your application runs the same way on any computer or server that has Docker installed. It eliminates the problem of “it works on my machine” because it provides a consistent environment for your application to run.

  4. With Docker, you can easily share your application with others, knowing that they will have the same environment to run it. It also makes it simple, to deploy your application to different servers or cloud platforms because the container can be easily moved around.

» 3 things that you should know

  1. Dockerfile

  2. Docker Image

  3. Docker Image


  1. Dockerfile:

A. You use a special file called a Dockerfile to create these containers. A Dockerfile is essentially a recipe or a blueprint for building a Docker image.

B. It contains a set of instructions that specify the base image, adds files and dependencies, configure settings, and define the commands to be executed within the container.

  1. Docker Image:

A. An Docker image is a lightweight, standalone, and executable package that is created from a Dockerfile. It’s like a snapshot of a specific application or service, along with its environment.

B. The image includes all the necessary components, such as the code, runtime environment, libraries, dependencies, and configuration files. Think of it as a template or blueprint for creating Docker containers.

  1. Container

A. A container is an instance of a Docker image. It’s like a running, isolated environment that encapsulates the application and its dependencies.

B. Containers provide a lightweight and consistent runtime environment, ensuring that your application runs the same way across different systems. Containers can be started, stopped, moved, and deleted, allowing for easy management and deployment of your applications.

» Differences between Dockerfile, Docker image, and Docker container

  1. A Dockerfile is a set of instructions used to build a Docker image.

  2. A Docker image is packaged software that includes everything needed to run an application.

  3. A Docker container is a running instance of a Docker image, providing an isolated and lightweight runtime environment.

» Containerizing Node.js applications with Docker!

  1. Step 1: Install Docker

The first thing you need to do is install Docker on your computer. Docker provides versions for Windows, macOS, and Linux, so choose the one that matches your operating system. Head over to the Docker website, download the installer, and follow the installation instructions.

  1. Step 2: Open VS Code

In VS Code, create a file app.js and in this file console log “Hello Docker!”

console.log('Hello Docker!')

Now create a Dockerfile, which is like a recipe that tells Docker how to build your container. Create a new file in your Node.js application’s root directory and name it Dockerfile. Open the file and let’s start adding some instructions.

In the Dockerfile, the first line typically specifies the base image to use. You can think of the base image as the starting point for your container. For a Node.js application, a good base image is “node:latest” because it includes Node.js pre-installed. So, add the following line to your Dockerfile:

FROM node:latest  
// Use an official Node.js runtime as the base image

To keep things organized, let’s set the working directory inside the container. This is the directory where your application’s code will be copied. Add the following line to your Dockerfile:

WORKDIR /app 
// Set the working directory to /app

Now, let’s copy your Node.js application files into the container. Add the following line to your Dockerfile: We are going to copy all the files in the current directory into the app directory into that image

COPY . /app 
//Copy the current directory contents into the container at /app

Finally, we need to specify the command that Docker should run when the container starts. Add the following line to your Dockerfile:

CMD ["node", "app.js"] 
// Run app.js when the container launches

With the Dockerfile ready, it’s time to build your Docker image. Open your terminal or command prompt, navigate to your application’s root directory (where the Dockerfile is located), and run the following command:

docker build -t hellodocker .

Docker will read the Dockerfile, pull the necessary dependencies, and build the image for you. This might take a few moments.

Now let’s run this command

docker image ls

This command lists all the Docker images stored on your local machine. It provides valuable information such as the image ID, repository name, tag, and size. The output will give you a clear overview of the Docker images you have available.

The image ID uniquely identifies each image. The repository name denotes the name of the image, often in the format of “repository:tag” where the tag represents a specific version or label for the image. The size column shows the disk space occupied by each image.

Now that you have built this image, now this image can run on any computer that has running docker.

Run this command

docker run your_file_name

Now look we see the message on the terminal which says:

Hello Docker!

That’s it! You’ve successfully containerized your Node.js application using Docker. Now you can share your container with others, deploy it to different servers, or take advantage of the scalability and portability Docker offers.


I hope this cleared - Docker, Docker File, Docker Image and Container. Thank You and Happy coding in containers!😊