Skip to main content

Command Palette

Search for a command to run...

Getting Started with Docker🐳: The Easy Way to Package and Share Apps

Updated
•4 min read
Getting Started with Docker🐳: The Easy Way to Package and Share Apps
P

Hi, I am Prem Shinde a Full Stack Web developer and Technical Content Writer

Welcome to the world of Docker! Have you ever faced a problem where your application is running fine on your system but not on your friend’s system? If yes, then here is the solution called Docker.

What is Docker?

Docker is an Open Source platform that enables developers to automate the deployment, scaling, and management of applications in a lightweight, portable container. It packagesšŸ“¦ the application in a unit called an image which includes code, runtime, libraries, and dependencies needed for an application to run, ensuring consistency across different environments. This image can be created, handled, and destroyed with the commands.

Why Docker?

Imagine you’re a chef who has mastered a recipe. You want to serve this recipe to friends in different kitchens, but each kitchen has different appliances and ingredients. Docker is like a magic container that ensures your recipe works the same way no matter where it’s cooked.

Docker solves the problem of ā€œit works on my machineā€ by ensuring that applications run the same way everywhere. It provides a consistent environment across the development, testing, and production stages.

Docker creates isolated environments that include everything needed to run an application, regardless of the system. This means you can easily move and run Docker containers on any compatible system, whether it’s a developer’s laptop, a testing server, or a cloud platform, without making any changes.

With Docker, you can package the entire application in just seconds.ā±ļøāœØ

How Docker Works: Key Concepts

1. Docker Images

Docker Image is a lightweight, standalone executable package that includes everything needed to run the application, including the code, libraries, and runtime. It is like a snapshot of an application that outlines the necessary components of an application. Docker image is created using a Dockerfile, and these images can be versioned, shared, and stored in repositories called Docker Hub.

2. Docker Containers

A container is an isolated environment where the application operates, meaning it runs independently, and containers share the host OS kernel, making them more efficient than traditional virtual machines.

3. Images Vs Containers

Image is when all the codebase, dependencies, and libraries are packaged, and the container is running instance of a Docker image. A container is an environment where the application operates.

4. Dockerfile

A Dockerfile is a script containing instructions on how to build a Docker image. There are some common commands (e.g., FROM, RUN, COPY, CMD) that are used in the Dockerfile.

Here is an example of a simple Dockerfile for building an image of an Express.js API,

FROM node:20

WORKDIR /usr/src/app

COPY . .

RUN npm install

EXPOSE 3000

CMD ["node", "index.js"]

In the above Dockerfile, the lines serve the following purposes:

  1. Base Image: The first line specifies the base image as node:20, which includes Node.js version 20. This base image provides the essential environment for running the React application.

  2. Working Directory: The second line sets the working directory to /app. This is where the application code will reside within the container.

  3. Copying Code: The line COPY . . copies all files from the local directory (where the Dockerfile is located) into the working directory /app in the container. This ensures that the application code is available for installation and execution.

  4. Installing Dependencies: The command RUN npm install is executed to install the application’s dependencies as defined in the package.json file. This is crucial for ensuring that the application has all the required packages to run properly.

  5. Port Mapping: The line EXPOSE 3000 indicates that the application will listen on port 3000. This is essential for port mapping, allowing the host machine to communicate with the containerized application.

  6. Running the Application: Finally, the command CMD ["node", "index.js"] is executed to start the application. This line tells Docker to run the index.js file, which typically contains the main entry point for the React application.

5. Docker Hub

Docker Hub is a cloud-based registry service that allows users to store, share, and access Docker images. Users can choose to store their images in public repositories, which are accessible to everyone, or in private repositories, which restrict access to specific users or teams. This flexibility makes Docker Hub an essential tool for developers collaborating on projects, as it facilitates easy sharing and versioning of container images.

Docker Architecture

Docker Architecture includes three main components, and here’s a brief explanation of these components,

1. Docker CLI (Command-Line Interface): The Docker CLI is the primary tool for interacting with Docker. It is a way to interact with a docker daemon which helps in creating images.

2. Docker Engine: The engine manages the complete lifecycle of containers, from creation to execution.

3. Docker Registry: Registries facilitate sharing images across organizations and teams, making it easy to deploy applications consistently across different environments.

Wrap Up

🌟 In This Article
We’ve explored the fundamental concepts of Docker that are essential for a solid understanding. Next, we'll dive into how to work with images and containers in greater detail.

Stay tuned for the upcoming insights!

S

Great to kick-start with docker! Very much helpful to those finding a quick understandingšŸ‘šŸ‘

1
P

Thankyou Soham!!

R

Great blog! You’ve explained Docker concepts really clearly

1
P

Thankyou Rajwardhan!!

More from this blog

Prem Shinde

12 posts