Thursday, 25 July 2024

Java JWT example

 

package com.codewalla.hrms.authentication.controller;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jws;
import io.jsonwebtoken.SignatureAlgorithm;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.security.Keys;

import java.security.Key;
import java.security.SecureRandom;
import java.util.Base64;
import java.util.Date;

public class JWTExample {
public static void main(String[] args) {
int keyLengthBytes = 64;
SecureRandom secureRandom = new SecureRandom();
byte[] keyBytes = new byte[keyLengthBytes];
secureRandom.nextBytes(keyBytes);

String secretKey = Base64.getUrlEncoder().withoutPadding().encodeToString(keyBytes);

String jws = Jwts.builder()
.setSubject("user123")
.setIssuer("example.com")
.setIssuedAt(new Date())
.setExpiration(new Date(System.currentTimeMillis() + 3600000)) // 1 hour expiration
.signWith(SignatureAlgorithm.HS256,secretKey)
.compact();

System.out.println("Generated JWT: " + jws);

try {
Jws<Claims> claimsJws = Jwts.parser()
.setSigningKey(secretKey)
.parseClaimsJws(jws);
Claims claims = claimsJws.getBody();
System.out.println("Subject: " + claims.getSubject());
System.out.println("Issuer: " + claims.getIssuer());
System.out.println("Expiration: " + claims.getExpiration());
} catch (Exception e) {
System.out.println("Invalid JWT: " + e.getMessage());
}
}
}

communication between two docker container

 

To enable communication between two Docker containers, you can use Docker networks. Docker networks allow containers to discover and communicate with each other. Here are the steps to set up communication between two Docker containers:

1. Create a Docker Network

First, create a custom Docker network. This will allow your containers to communicate with each other using their container names as hostnames.

sh
docker network create my_network
 

2. Run Containers on the Same Network

Run your containers and attach them to the network you just created.

For example, if you have two containers named container1 and container2:

sh
docker run -d --name container1 --network my_network my_image1
docker run -d --name container2 --network my_network my_image2

3. Verify Network Connectivity

You can verify that the containers are on the same network by running:

docker network inspect my_network
This command will show you details about the network, including the containers attached to it.

 

4. Test Communication

To test the communication between the containers, you can use the docker exec command to enter one container and ping the other.

For example, from container1, you can ping container2:

docker exec -it container1 ping container2
 

 

version: '3'
services:
container1:
image: my_image1
networks:
- my_network

container2:
image: my_image2
networks:
- my_network

networks:
my_network:
driver: bridge
 

 

 

Friday, 12 July 2024

python docker deployment

Run comand

 

 FROM python:3.9-slim
WORKDIR /app
COPY ai-quiz-service/requirements.txt .
COPY config/dev.env /app/.env
RUN pip install --no-cache-dir -r requirements.txt
COPY app/ app/
ENV PYTHONPATH=/app
EXPOSE 8088
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8088"]

 

docker build --build-arg ENV_FILE=app/config/dev.env -t myfastapi:dev .


# Use an official Python runtime as a parent image
FROM python:3.10

# Set the working directory in the container
WORKDIR /app

# Copy the current directory contents into the container at /app
COPY . /app

# Install any needed packages specified in requirements.txt
RUN pip install --no-cache-dir -r requirements.txt

# Define build argument for the environment file
ARG ENV_FILE

# Copy the environment file from the build context to the container
COPY ${ENV_FILE} /app/.env

# Make port 80 available to the world outside this container
EXPOSE 8088

# Ensure the PYTHONPATH includes the /app directory
ENV PYTHONPATH=/app

# Run app.py when the container launches
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8088"]