Containerization is a technique that allows developers to package applications and their dependencies together into isolated units called containers. Docker is the most popular containerization platform that makes it easy to build, ship, and run containers efficiently across different environments.
Docker is an open-source platform designed to automate the deployment, scaling, and management of applications using container technology. It packages applications and all their dependencies into a container image that can be run reliably on any environment that supports Docker.
Docker images are read-only templates that contain the application code, runtime, libraries, and environment settings.
Containers are runtime instances of Docker images. They are isolated, lightweight, and share the host OS kernel.
A Dockerfile is a script with instructions to build a Docker image.
Docker Hub is a cloud-based registry where Docker users can share and manage container images.
sudo apt update
sudo apt install docker.io
sudo systemctl start docker
sudo systemctl enable docker
Download Docker Desktop from the official Docker website and install it using the GUI installer.
An example Dockerfile for a simple Node.js application:
# Use an official Node.js runtime as a base image
FROM node:18
# Set working directory
WORKDIR /app
# Copy package files and install dependencies
COPY package*.json ./
RUN npm install
# Copy source files
COPY . .
# Expose port
EXPOSE 3000
# Start the app
CMD ["node", "index.js"]
docker build -t my-node-app .
docker run -d -p 3000:3000 my-node-app
docker ps
docker stop <container_id>
docker rm <container_id>
docker rmi <image_id>
Docker Compose allows you to manage multi-container applications using a YAML file called docker-compose.yml.
version: "3"
services:
app:
build: .
ports:
- "3000:3000"
volumes:
- .:/app
depends_on:
- mongo
mongo:
image: mongo
ports:
- "27017:27017"
docker-compose up
Volumes persist data even after the container is removed.
# Create a volume
docker volume create myvolume
# Run a container with a volume
docker run -v myvolume:/data busybox
docker network ls
docker network create mynetwork
docker run -d --network=mynetwork --name container1 myimage
docker run -e DB_HOST=localhost -e DB_PORT=27017 my-node-appdocker images # List images
docker ps -a # List containers
docker exec -it <id> bash # Enter a running container
docker logs <id> # View logs
docker system prune # Clean up unused data
Docker revolutionizes how applications are developed, tested, and deployed. It brings consistency across environments and enables scalable, reliable deployment pipelines. With containerization, you can simplify dependency management, isolate services, and streamline DevOps practices. Mastering Docker is crucial for modern software development, especially in cloud-native and microservices architectures.
A function passed as an argument and executed later.
Runs multiple instances to utilize multi-core systems.
Reusable blocks of code, exported and imported using require() or import.
nextTick() executes before setImmediate() in the event loop.
Starts a server and listens on specified port.
Node Package Manager β installs, manages, and shares JavaScript packages.
A minimal and flexible web application framework for Node.js.
A stream handles reading or writing data continuously.
It processes asynchronous callbacks and non-blocking I/O operations efficiently.
Node.js is a JavaScript runtime built on Chrome's V8 engine for server-side scripting.
An object representing the eventual completion or failure of an asynchronous operation.
require is CommonJS; import is ES6 syntax (requires transpilation or newer versions).
Use module.exports or exports.functionName.
Variables stored outside the code for configuration, accessed using process.env.
MongoDB, often used with Mongoose for schema management.
Describes project details and manages dependencies and scripts.
Synchronous blocks execution; asynchronous runs in background without blocking.
Allows or restricts resources shared between different origins.
Use try-catch, error events, or middleware for error handling.
Provides file system-related operations like read, write, delete.
Using event-driven architecture and non-blocking I/O.
Functions in Express that execute during request-response cycle.
A set of routes or endpoints to interact with server logic or databases.
Yes, it's single-threaded but handles concurrency using the event loop and asynchronous callbacks.
Middleware to parse incoming request bodies, like JSON or form data.
Copyrights © 2024 letsupdateskills All rights reserved