Saturday, November 16, 2019

Images, Containers and Docker Registry

This tutorial shows how to create images and containers and push to hub.docker.com registry. GitHub repo is at https://github.com/nikola-bodrozic/Docker-ReactJS

Simple Build on Linux

Apache web server no Dockerfile, $(pwd) prints path to current folder

docker run -d --name apa -e TZ=UTC -p 8080:80 -v $(pwd)/html/:/var/www/html/ ubuntu/apache2

Development Build

docker build -t reactapp:1.0 .
docker run -p 3000:3000 \ 
       --env REACT_APP_BASE_URL=localhost \ 
       --name react-app reactapp:1.0
docker logs -f YOUR_CONTAINER_SHA

Prod build & push to registry

Create empty private image repo at DOCKER_HUB_USERNAME/REPO_NAME

The repo should be visible in browser at https://hub.docker.com/repository/docker/DOCKER_HUB_USERNAME/REPO_NAME

For production you need to set env variable in Dockerfile-prod not in docker run. Here's how
RUN REACT_APP_BASE_URL=example.com yarn build

build, run and push to registry

docker run -d \
           -p 80:80 \
           --name react-app-prod DOCKER_HUB_USERNAME/REPO_NAME:1.0

docker build -t DOCKER_HUB_USERNAME/REPO_NAME:1.0 \
             -f Dockerfile-prod .

docker login

docker push DOCKER_HUB_USERNAME/REPO_NAME:1.0
DOCKER_HUB_USERNAME/REPO_NAME:1.0

Saturday, June 22, 2019

Override env. variable and shell script

Override env. variable and shell script defined in Docker file in run command.

build an image

docker build -t busybox .

run container with env. variable & run CMD in Dockerfile

docker run busybox

override env. variable in Dockerfile
docker run --env ENV_TARGET=yahoo.com busybox

override CMD in Dockerfile
docker run busybox sh -c 'whoami && pwd'

Dockerfile

FROM busybox

WORKDIR /app
ENV ENV_TARGET google.com
COPY subs.sh subs.sh
RUN chmod +x subs.sh
CMD ["/app/subs.sh"]

subs.sh

#!/bin/sh

ping -c 2 ${ENV_TARGET}

axios concurrent calls

Axios can make multiple concurrent calls. In app.js calls to two end-points are started at the same time so total waiting time is 4 seconds. Server latency is 4 seconds for demo purposes.

app.js

const axios = require('axios')
 
try {
    console.log("start");
    function getUserName() {
        return axios.get('http://localhost:3000/heroes');
    }
 
    function getId() {
        return axios.get('http://localhost:3000/heroes');
    }
 
    // Performing multiple concurrent requests waiting time 4 seconds
    axios.all([getUserName(), getId()])
        .then(axios.spread(function (acct, perms) {
            const user = acct.data[0].param;
            const id = perms.data[1].param;
            console.log(`${user} ${id}`)
            const str = 'https://jsonplaceholder.typicode.com/' + user + '/' + id
            console.log(str)
            axios.get(str).then(response => console.log(response.data.name));
        }));
} catch (error) {
    console.error(error);
}

Monday, June 17, 2019

axios async await

Sometimes you need to wait for one (axios) call to finish to continue with next one. It's accomplished by using async/await. Last axios call in app.js downloads a file without waiting for getUser() to be finished.

set server delay to 4 seconds for demo purposes

{
  "name": "async-await",
  "scripts": {
    "serve-json": "json-server --delay 4000 --watch db.json",
    "start": "node app.js"
  },
  "dependencies": {
    "axios": "^0.19.0",
    "json-server": "^0.15.0"
  }
}

db.json

{
    "heroes": [
        {
            "param": "users"
        },
        {
            "param": "1"
        }
    ]
}

app.js

const axios = require('axios')
const fs = require('fs')
 
async function getUser() {
    try {
        console.log("start");
        const response = await axios.get('http://localhost:3000/heroes');
        var1 = response.data[0].param; // users
        console.log("4 sec after start ", var1);
        const response2 = await axios.get('http://localhost:3000/heroes');
        var2 = response2.data[1].param; // 1
        console.log("8 sec after start", var2);
        const response3 = await axios.get('https://jsonplaceholder.typicode.com/' + var1 + '/' + var2);
        console.log(response3.data.name);
    } catch (error) {
        console.error(error);
    }
}
 
getUser()
 
// call below doesnot wait for getUser() to be finished 
axios({
    method: 'get',
    url: 'https://jsonplaceholder.typicode.com/users/2',
    responseType: 'stream'
  })
    .then(function (response) {
      response.data.pipe(fs.createWriteStream('full-user.json'))
});






Thursday, February 14, 2019

Oracle 12c Create Table with Auto Increment and Flashback

CREATE TABLE table_name ( 
    id NUMBER(10) GENERATED BY DEFAULT AS IDENTITY ( START WITH 1 NOCACHE ) NOT NULL, 
    ctimestamp TIMESTAMP DEFAULT current_timestamp 
) NOLOGGING; 

CREATE UNIQUE INDEX table_name_pk ON table_name (id ASC ); 

ALTER TABLE table_name ADD CONSTRAINT table_name_pk PRIMARY KEY ( id ); 

SELECT TO_CHAR (SYSDATE, 'YYYY-MM-DD HH24:MI:SS') "NOW" FROM DUAL; 

CREATE TABLESPACE archivetest_tabspace DATAFILE SIZE 10M SEGMENT SPACE MANAGEMENT AUTO; 

CREATE FLASHBACK ARCHIVE archivetest TABLESPACE archivetest_tabspace QUOTA 50M RETENTION 7 DAY; 

ALTER TABLE table_name FLASHBACK ARCHIVE archivetest; 

SELECT * FROM table_name AS OF TIMESTAMP TO_TIMESTAMP ('2018-11-29 14:05:11', 'YYYY-MM-DD HH24:MI:SS');

Sunday, January 6, 2019

Docker and node_modules

Development environment for a NodeJS App


Dockefile

FROM node:16-alpine

WORKDIR /app

COPY package.json ./

RUN npm install

COPY . .

EXPOSE 3000

CMD ["npm", "start"]

.dockerignore

node_modules
npm-debug.log
.git
Dockerfile
.dockerignore

Open your browser on http://localhost:3000

docker-compose.yaml
version: "3.8"

services:
  app:
    build:
      context: .
      dockerfile: Dockerfile
    volumes:
      - './src:/app/src'
      - './public/assets:/app/public/assets'
      - '/app/node_modules'
    ports:
      - 3000:3000
    stdin_open: true
    environment:
      - CHOKIDAR_USEPOLLING=true

Friday, December 21, 2018

Mass Update and Insert in Oracle

UPDATE tab1 t1 SET col_1 = (SELECT col_1 FROM tab2 t2 WHERE t1.id=t2.id);

INSERT INTO table1 SELECT * FROM table2 WHERE ...

INSERT INTO table1 (col_1, col_2) SELECT col_1, col_2 FROM table2 WHERE ...