What is Docker? - Day 3 Of #100DaysOfFullStackChallnege
Tags: docker, webdev, javascript, typescript
Published: 2024-07-11 17:27:53

This Blog is about the basics of Docker. 
What is Docker? 

So, let's start,
**## What is Docker? **
Docker is a Platform as a Service. (PaaS). The main use case of docker is to containerization of the app. 

There are two main components of Docker

1. Docker Image.
2. Docker Container.

##**Docker Image**
Docker Image is a stand-alone executable package that contains all of the components required to run a piece of software, including the code, a runtime, libraries, environment variables, and configuration files is called a Docker image.

##**Docker Container**
A Docker Container is a runtime instance of an image. Containers make development and deployment more efficient since they contain all the dependencies and parameters needed for the application it runs completely isolated from the host environment.

## **How to build a Docker Image**
To create a Docker Image you need to create a `dockerfile` file in your project root directory. Make sure you use the same name and this does not have any extension. 

![dockerfile img](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9448c5na3cgatu0gkplh.png) 

There are several dockerfile commands 

## **1. FROM <image>[:<tag>]**
Specifies the base image to use for the subsequent instructions. The base image can be any valid Docker image, including OS images like `alpine` and `Ubuntu`, or application-specific images like `node`, `python`, etc.
 
This is the command that is executed first before any other commands.

Syntax : 
FROM <ImageName>

Example : 
`FROM node:latest`. You can write like `FROM node:vesion_you_want`.

## ** 2. RUN** 
Executes a command in a new layer on top of the current image and commits the result. Commonly used for installing dependencies.

Syntax : 
RUN < Command + ARGS>

Example : 
`RUN apt-get update && apt-get install -y curl`

This command will update the package list and install curl in the image.

## **3. COPY**
This command is used to copy the file or a folder while creating a docker image.

Syntax : 
COPY <Source> <Destination>

Example : 
`COPY . /app` 

you can write 
`COPY . .`

## **4. ADD** 
We can download the files while creating a image 

Syntax : 
ADD <URL>

Example : 
`ADD https://example.com/file.tar.gz /app/`

Downloads a file from the given URL and extracts it into the /app/ directory.

## **5. WORKDIR**

Sets the working directory for any subsequent RUN, CMD, ENTRYPOINT, COPY, and ADD instructions.

Syntax : 
WORKDIR <path>

Example : 
`WORKDIR /src` 

 Changes the working directory to /src 

## **6. CMD**
The main purpose of the CMD command is to start the process inside the container and it can be overridden.

Syntax : 
CMD ["executable","param1","param2"]


Example : 
`CMD ["node", "index.js"]`


Specifies that node index.js should be run when a container is started from the image.

This is how dockerfile look like 

```
# Use an official Node.js image as the base image
FROM node:14

# Set environment variables
ENV NODE_ENV=production

# Create and change to the app directory
WORKDIR /usr/src/app

# Copy application dependency manifests to the container image
COPY package*.json ./

# Install dependencies
RUN npm install

# Copy the local code to the container image
COPY . .

# Expose the port the app runs on
EXPOSE 3000

# Define the default command to run the app
CMD ["node", "index.js"]

```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rpmaixkf4j3m0zn42v82.png)


That's all for today!

Thank you for joining me on this journey through the 100 Days Full Stack Challenge. Each day brings new insights, challenges, and opportunities for growth, and I'm thrilled to share every step with you. Your support and feedback mean the world to me, and I'm excited to continue building and learning together. 🚀

Don't forget to follow my progress on my personal website and stay connected on [Dev.to](https://dev.to/zendeaditya), [Medium](https://medium.com/@adityazende), and [Hashnode](https://hashnode.com/@adityazende01). Let's keep pushing the boundaries of what's possible in web development! 🌐💻

Until next time, keep coding, keep creating, and keep pushing forward. 💪✨