Docker: Containerization and Deployment

Docker
In the dynamic landscape of software development and deployment, efficiency and scalability are paramount. Docker, an open-source platform, has emerged as a game-changer, providing developers with a powerful tool to streamline the process of building, shipping, and running applications. In this blog post, we'll delve into the world of Docker, exploring its key concepts, benefits, and how it is reshaping the way we approach software development.
Key Concept
Images
Images are the building blocks of Docker containers. Images are lightweight packages/read-only templates that include everything needed to run software like the application code, runtime, libraries, and system tools.
In simple term, images are the blueprint or recipe for the application
Build Images
docker build -t react-docker .Container
Docker container are runnable instance of docker images in isolation on a host system. Containers encapsulate the application and its dependencies, ensuring consistency across various environments.
In simple term container are the real product, the app
Run the Container
docker run -p 5173:5173 -v "$(pwd):/app" -v /app/node_modules react-dockerDockerfile
A script that defines the steps to create a Docker image. Developers can use Dockerfiles to specify the environment, dependencies, and configuration of their applications.
Benefits
- Portability: Docker containers can run on any system that supports Docker, providing consistency across development, testing, and production environments.
- Scalability: Docker enables easy scaling of applications by allowing the deployment of multiple containers, either on a single host or across a cluster of machines.
- Resource Efficiency: Unlike traditional virtualization, Docker containers share the host OS kernel, reducing overhead and optimizing resource utilization.
- Rapid Deployment: Docker simplifies the deployment process, allowing developers to deploy applications quickly and efficiently.
Steps to dockerize any project (here is for a fullstack nextjs project):
1.
docker init2. Setup the Dockerfile
FROM node
WORKDIR /app
COPY package*.json ./
RUN npm install --
COPY . .
EXPOSE 3000
CMD npm run dev3. Setup compose.yaml
version: '3.8'
services:
frontend:
build:
context: .
dockerfile: Dockerfile
ports:
- 3000:3000
develop:
watch:
- path: ./package.json
action: rebuild
- path: ./next.config.js
action: rebuild
- path: ./package-lock.json
action: rebuild
- path: .
target: /app
action: sync
environment:
# here we're using MongoDB atlas so we don't need to run a local instance of MongoDB
- DB_URL=mongodb+srv://sujata:rnZzJjIDr3bIDymV@cluster0.hnn88vs.mongodb.net/
# but if you want to run a local instance, you can do it this way
# db:
# image: mongo
# ports:
# - 27017:27017
# environment:
# - MONGO_INITDB_ROOT_USERNAME=sujata
# - MONGO_INITDB_ROOT_PASSWORD=rnZzJjIDr3bIDymV
# volumes:
# - tasked:/data/db
volumes:
tasked:4. docker compose up
5. Split the console then run docker compose watch
more explanation you can watch JSMastery: Lean Docker in 1 Hour | Full Docker Course for Beginners - YouTube