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
- Docker Image.
- 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.
There are several dockerfile commands
1. FROM [:]
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
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
Example : COPY . /app
you can write COPY . .
4. ADD
We can download the files while creating a image
Syntax :
ADD
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
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"]
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, Medium, and Hashnode. Let’s keep pushing the boundaries of what’s possible in web development! 🌐💻
Until next time, keep coding, keep creating, and keep pushing forward. 💪✨
Discover more from Coursity
Subscribe to get the latest posts sent to your email.