Showing posts with label build. Show all posts
Showing posts with label build. Show all posts

Monday, 29 March 2021

Javax.json: Build JsonObject from string

Step 1: Create JsonReader instance from json string.

JsonReader reader = Json.createReader(new StringReader(jsonString));

 

Step 2: Get JsonObject using readObject method.

JsonObject jsonObject = reader.readObject();

 

Find the below working application.

 

jsonToJsonObjectDemo.java

package com.sample.app;

import java.io.StringReader;

import javax.json.Json;
import javax.json.JsonObject;
import javax.json.JsonReader;

public class jsonToJsonObjectDemo {
	
	public static void main(String[] args) {
		String jsonString = "\n" + 
				"{\n" + 
				"    \"firstName\": \"Ram\",\n" + 
				"    \"lastName\": \"Gurram\",\n" + 
				"    \"id\": 32,\n" + 
				"    \"male\": false,\n" + 
				"    \"dateOfBirth\": \"10/01/1985\"\n" + 
				"}\n" + 
				"";
		
		JsonReader reader = Json.createReader(new StringReader(jsonString));
		
		JsonObject jsonObject = reader.readObject();
		
		System.out.println("firstName : " + jsonObject.getString("firstName"));
		System.out.println("lastName : " + jsonObject.getString("lastName"));
		System.out.println("id : " + jsonObject.getInt("id"));
		System.out.println("male : " + jsonObject.getBoolean("male"));
		System.out.println("dateOfBirth : " + jsonObject.getString("dateOfBirth"));
	}

}

 

Output

firstName : Ram
lastName : Gurram
id : 32
male : false
dateOfBirth : 10/01/1985

 

 

 

  

Previous                                                    Next                                                    Home

Saturday, 9 May 2020

Docker: build an image with numpy library

In this post, I am going to explain how to build an image with numpy library.

Step 1: Create Dockerfile with below content.

Dockerfile
FROM python:3

RUN pip3 install numpy

Step 2: Navigate to the folder where Dockerfile is located and execute below command to build the image from Dockerfile.
docker build -t my_numpi_image .
$docker build -t my_numpi_image .
Sending build context to Docker daemon   5.12kB
Step 1/2 : FROM python:3
 ---> 4f7cd4269fa9
Step 2/2 : RUN pip3 install numpy
 ---> Running in fc7102543fc6
Collecting numpy
  Downloading numpy-1.18.4-cp38-cp38-manylinux1_x86_64.whl (20.7 MB)
Installing collected packages: numpy
Successfully installed numpy-1.18.4
Removing intermediate container fc7102543fc6
 ---> b8cec2553f11
Successfully built b8cec2553f11
Successfully tagged my_numpi_image:latest

Execute the command ‘docker image ls’ to list all the docker images.
$docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
my_numpi_image      latest              b8cec2553f11        43 seconds ago      1.05GB

Step 3: Create hello.py file.
hello.py

from numpy import random

x = random.randint(100)

print(x)

Step 4: Execute below command to run the container and execute hello.py file.

docker run --rm -v $(pwd):/src my_numpi_image python3 /src/hello.py

--rm: Remove this container once the command is executed
-v $(pwd):/src: Mount current directory to /src folder.
my_numpi_image: Image to be loaded in container.
python3 /src/hello.py: Command to be executed.

$docker run --rm -v $(pwd):/src my_numpi_image python3 /src/hello.py
85
$
$docker run --rm -v $(pwd):/src my_numpi_image python3 /src/hello.py
46
$
$
$docker run --rm -v $(pwd):/src my_numpi_image python3 /src/hello.py
79



Previous                                                    Next                                                    Home

Docker: Python: Flask: Build an image using Docker file

Step 1: Create a directory ‘flask_hello_world’.

Step 2: Navigate to flask_hello_world and create Dockerfile.
$mkdir flask_hello_world
$
$cd flask_hello_world/
$
$touch Dockerfile
$ls
Dockerfile

Step 3: Update Dockerfile with below content.

Dockerfile
FROM ubuntu

RUN apt-get update

RUN apt-get install -y python python-setuptools python-dev build-essential python-pip python-mysqldb

RUN pip install flask

COPY app.py /opt/app.py

ENTRYPOINT FLASK_APP=/opt/app.py flask run --host=0.0.0.0

Step 4: Create a file app.py in the folder where Dockerfile is located.

app.py
import os
from flask import Flask
app = Flask(__name__)

@app.route("/")
def main():
    return "Hello World"

@app.route('/welcome')
def welcome():
    return 'Welcome to Docker Container'

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=8080)

$ls
Dockerfile   app.py

Step 5: Execute the command ‘docker build -t flask_hello_world .’ from the directory where Dockerfile is located.
$docker build -t flask_hello_world .
Sending build context to Docker daemon  3.072kB
Step 1/6 : FROM ubuntu
 ---> 94e814e2efa8
Step 2/6 : RUN apt-get update
 ---> Using cache
 ---> 93102f1bf79f
Step 3/6 : RUN apt-get install -y python python-setuptools python-dev build-essential python-pip python-mysqldb
 ---> Using cache
 ---> c04fb08e8e87
Step 4/6 : RUN pip install flask
 ---> Running in a57ed8d799a6
Collecting flask
  Downloading https://files.pythonhosted.org/packages/9b/93/628509b8d5dc749656a9641f4caf13540e2cdec85276964ff8f43bbb1d3b/Flask-1.1.1-py2.py3-none-any.whl (94kB)
Collecting click>=5.1 (from flask)
  Downloading https://files.pythonhosted.org/packages/fa/37/45185cb5abbc30d7257104c434fe0b07e5a195a6847506c074527aa599ec/Click-7.0-py2.py3-none-any.whl (81kB)
Collecting Werkzeug>=0.15 (from flask)
  Downloading https://files.pythonhosted.org/packages/d1/ab/d3bed6b92042622d24decc7aadc8877badf18aeca1571045840ad4956d3f/Werkzeug-0.15.5-py2.py3-none-any.whl (328kB)
Collecting itsdangerous>=0.24 (from flask)
  Downloading https://files.pythonhosted.org/packages/76/ae/44b03b253d6fade317f32c24d100b3b35c2239807046a4c953c7b89fa49e/itsdangerous-1.1.0-py2.py3-none-any.whl
Collecting Jinja2>=2.10.1 (from flask)
  Downloading https://files.pythonhosted.org/packages/1d/e7/fd8b501e7a6dfe492a433deb7b9d833d39ca74916fa8bc63dd1a4947a671/Jinja2-2.10.1-py2.py3-none-any.whl (124kB)
Collecting MarkupSafe>=0.23 (from Jinja2>=2.10.1->flask)
  Downloading https://files.pythonhosted.org/packages/fb/40/f3adb7cf24a8012813c5edb20329eb22d5d8e2a0ecf73d21d6b85865da11/MarkupSafe-1.1.1-cp27-cp27mu-manylinux1_x86_64.whl
Installing collected packages: click, Werkzeug, itsdangerous, MarkupSafe, Jinja2, flask
Successfully installed Jinja2-2.10.1 MarkupSafe-1.1.1 Werkzeug-0.15.5 click-7.0 flask-1.1.1 itsdangerous-1.1.0
Removing intermediate container a57ed8d799a6
 ---> 080f962d64bf
Step 5/6 : COPY app.py /opt/app.py
 ---> 217efc0c9c63
Step 6/6 : ENTRYPOINT FLASK_APP=/opt/app.py flask run --host=0.0.0.0
 ---> Running in 272c2c703d4a
Removing intermediate container 272c2c703d4a
 ---> a04372b9c2d0
Successfully built a04372b9c2d0
Successfully tagged flask_hello_world:latest

Step 6: Execute the command ‘docker images’ to see list of images.
$docker images
REPOSITORY                      TAG                 IMAGE ID            CREATED             SIZE
flask_hello_world               latest              a04372b9c2d0        55 seconds ago      467MB
mysql                           latest              62a9f311b99c        2 weeks ago         445MB
jboss/wildfly                   latest              4e440230e01d        2 months ago        748MB

You can see that ‘flask_hello_world’ image is available.

Step 7: Run the container by executing the below command.
docker run -p 1234:5000 flask_hello_world
$docker run -p 1234:5000 flask_hello_world
 * Serving Flask app "/opt/app.py"
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
172.17.0.1 - - [30/Aug/2019 21:47:34] "GET / HTTP/1.1" 200 -
172.17.0.1 - - [30/Aug/2019 21:47:34] "GET /favicon.ico HTTP/1.1" 404 –

Open the url ‘http://localhost:1234/’ in browser, you can see below kind of screen.

Open the url ‘http://localhost:1234/welcome’ in browser, you can see below kind of screen.





Previous                                                    Next                                                    Home