This repository is created to practice and experiment with:
- gRPC: A high-performance, open-source universal RPC framework.
- GraphQL: A query language for APIs and a runtime for executing those queries.
- Docker: A platform to containerize applications and manage dependencies efficiently.
- Learn the basics of Docker and how to containerize Node.js applications.
- Implement and explore gRPC and GraphQL services.
- Practice deploying and running these services in Docker containers.
- Example gRPC service setup.
- Example GraphQL service setup.
- Dockerfile and Docker Compose for containerization.
- Clone the repository:
git clone https://github.com/azkaiftikhar01/grphql-grpc-docker.git cd grphql-grpc-docker
Containerization is the process of packaging software and its dependencies into a lightweight, portable container. These containers can run consistently across various environments, whether it's a developer's laptop, a testing server, or a production environment.
- Containers are isolated from the host system, ensuring consistent execution.
- They are lightweight compared to traditional virtual machines because they share the host OS kernel.
-
Images:
- A Docker image is a lightweight, immutable blueprint for creating containers.
- Images include the application code, runtime, libraries, and dependencies.
-
Containers:
- A container is a runtime instance of a Docker image.
- It runs as an isolated process on the host machine.
-
Dockerfile:
- A
Dockerfile
is a script with instructions to build a Docker image.
- A
-
Volumes:
- Docker volumes allow data persistence and sharing between containers.
-
Docker Swarm:
- A built-in orchestration tool for deploying and managing clusters of containers.
A Dockerfile
is a plain text file containing instructions to build a Docker image. Each instruction creates a new layer in the image. When a Dockerfile
is processed by the docker build
command, it generates a new Docker image.
# Step 1: Specify the base image
FROM node:14
# Step 2: Set working directory inside the container
WORKDIR /app
# Step 3: Copy package files and install dependencies
COPY package*.json ./
RUN npm install
# Step 4: Copy the rest of the application code
COPY . .
# Step 5: Expose the application port
EXPOSE 3000
# Step 6: Define the command to start the application
CMD ["node", "index.js"]
- FROM: Specifies the base image (e.g.,
node:14
). - WORKDIR: Sets the working directory inside the container.
- COPY: Copies files from the host to the container.
- RUN: Executes commands during image build (e.g., installing dependencies).
- EXPOSE: Informs Docker that the container will listen on the specified port.
- CMD: Specifies the command to run when the container starts.
-
Build an Image:
docker build -t my-app .
-t my-app
tags the image with the namemy-app
..
specifies the current directory as the build context.
-
Run a Container:
docker run -d -p 3000:3000 my-app
-d
runs the container in detached mode.-p 3000:3000
maps port 3000 on the host to port 3000 in the container.
-
View Running Containers:
docker ps
-
Stop a Container:
docker stop <container_id>
Docker Swarm allows you to deploy and manage a cluster of Docker nodes. It supports scaling, load balancing, and container orchestration.
-
Initialize a Swarm:
docker swarm init
- Initializes the current node as the swarm manager.
-
Create a
docker-compose.yml
File: Define the services and configuration for your application. Example:version: '3.8' services: web: image: my-app ports: - "3000:3000" deploy: replicas: 3 restart_policy: condition: on-failure
-
Deploy the Application:
docker stack deploy --compose-file docker-compose.yml my-app-stack
-
Verify the Services:
docker service ls
-
Scale Services:
docker service scale my-app-stack_web=5
- Scales the service to 5 replicas.
Volumes are the preferred way to persist data generated by Docker containers.
-
Named Volumes:
- Managed by Docker.
- Example:
docker volume create my-volume docker run -v my-volume:/data my-app
-
Bind Mounts:
- Maps a specific directory on the host to a container directory.
- Example:
docker run -v /path/on/host:/data my-app
In a docker-compose.yml
file:
version: '3.8'
services:
app:
image: my-app
volumes:
- my-volume:/app/data
volumes:
my-volume:
-
List all volumes:
docker volume ls
-
Inspect a specific volume:
docker volume inspect my-volume
-
Remove unused volumes:
docker volume prune
Command | Description |
---|---|
docker build -t <name> . |
Build a Docker image from a Dockerfile . |
docker run -d -p <host>:<container> |
Run a container in detached mode. |
docker ps |
List running containers. |
docker stop <container_id> |
Stop a running container. |
docker swarm init |
Initialize a Docker Swarm. |
docker stack deploy ... |
Deploy a stack to a Swarm. |
docker volume create <name> |
Create a new Docker volume. |
docker volume ls |
List all Docker volumes. |
docker volume prune |
Remove unused volumes. |
- Use
.dockerignore
to exclude unnecessary files (e.g.,.git
,node_modules
). - Always use specific tags for base images (e.g.,
node:14
instead ofnode:latest
). - Keep images small by using multi-stage builds.
- Use Docker Compose for multi-container applications.
Let me know if you'd like more detailed examples or clarifications on any section!