Thursday, April 12, 2018

Getting Started with Postgres Docker image

This command will download and run docker container
 sudo docker run --name dipen-postgres -p 5432:5432  -e POSTGRES_PASSWORD=<your-password> -d postgres  

Note: username = postgres


To run the image and expose the port 5442 of docker image to host port
 sudo docker run -p 5432:5432 postgres   

If you need to download postgresql client
 sudo apt-get install postgresql-client  

To connect the postgres data hosted in docker container with psql client
 psql -h localhost -p 5432 -U postgres  

https://docs.docker.com/samples/library/postgres/

https://zaiste.net/posts/docker_postgresql_how_to/


https://hub.docker.com/_/postgres/

https://docs.docker.com/engine/examples/postgresql_service/#connecting-from-your-host-system

Run Program in background Linux

To run the process in background use & at the end of bash command
 ./idea.sh &  

To view the jobs running in background
 jobs  
Useful links

Friday, March 16, 2018

Create React App

Install npm package manager before proceeding forward. Click Here

Step: 1 Creating react app npm package manager
 sudo npm install -g create-react-app  

  create-react-app my-app   

Navigate into my-app directory
 cd my-app  

Start your application
 npm start  

Now your application is served and you will be able to see output on your browser
 http://localhost:3000/  

Note :
If you run into error specified below
 ERROR: npm 5 is not supported yet  
Then you need to install 4.6.1 version globally with following command and follow the step one again.
 sudo npm i -g npm@4.6.1  

Useful links 
A re-introduction to JavaScript 
Reactjs

Install Nodejs and NPM Ubuntu 17

Step 1: Run the command to install LTS Release version.

 sudo apt-get install python-software-properties  
curl -sL https://deb.nodesource.com/setup_9.x | sudo -E bash -  
sudo apt-get install -y nodejs

Then verify your npm and nodejs installation.
 node -v   
 npm -v  
Useful link
  Nodejs

Thursday, March 15, 2018

Share your docker image

Before Moving forward you have to define your container with Dockerfile. If you haven't done that follow the link Define your container with Dockerfile

Now it time to share your image across multiple platform and team using a docker registries. A registry is a collection of repositories, and a repository is a collection of images—sort of like a GitHub repository, except the code is already built.

Step 1: Make account on cloud.docker.com
Docker’s public registry is free and pre-configured, but there are many public ones to choose from.
Note that the docker CLI uses Docker’s public registry by default.

Login using docker CLI

 docker login  

After your are logged in successfully

Step 2: Tag the Image
Tagging image is good way of associating a local image with a repository on a registry.
For e.g. my-repo:my-first-image this puts the image in the my-repo repository and tag it as my-first-image.

 docker tag image username/repository:tag  

 docker tag my-app lamadipen/my-repo:first  
View list of images

 docker image ls  

Step 3: Publish your  image

  sudo docker push lamadipen/my-repo:first  

Now you can find your image on Docker Hub
Docker hub screen shot

Step 4: Pull the image and run image from the remote repository any where you want.
 docker run -p 4000:80 username/repository:tag  
For example
 docker run -p 4000:80 username/repository:tag  

This command pull your image from the repository if image isn't available on local machine and run it on your .

Step 5: Check your container serving you application
You will get response from your application on the browser if you check the link in your browser.
 http://localhost:8000/  

Tuesday, March 13, 2018

Define your container with Dockerfile

If you haven't installed Docker, then install it on your machine using the link (Installing Docker on Ubuntu).

Define a container with Dockerfile

Now its time to create your own custom environment using DockerFile. DockerFile behaves exactly the same wherever it runs enabling you to establish same standard configuration on dev, test and production environment.

Step 1: Create a folder (in my case i have created my-docker-img folder)
Step 2: Create a Dockerfile inside the folder (I have created Dockerfile.txt)
Dockerfile.txt
 # Use an official Python runtime as a parent image  
 FROM python:2.7-slim  
 # Set the working directory to /app  
 WORKDIR /app  
 # Copy the current directory contents into the container at /app  
 ADD . /app  
 # Set proxy server, replace host:port with values for your servers (If your are behind proxy server)  
 #ENV http_proxy host:port  
 #ENV https_proxy host:port  
 # Install any needed packages specified in requirements.txt  
 RUN pip install --trusted-host pypi.python.org -r requirements.txt  
 # Make port 80 available to the world outside this container  
 EXPOSE 80  
 # Define environment variable  
 ENV NAME World  
 # Run app.py when the container launches  
 CMD ["python", "app.py"]  

Step 3: Create app.py and requirement.txt specified in Docker file in the same folder.
app.py file is your python application that you want to deploy

app.py

 from flask import Flask  
 from redis import Redis, RedisError  
 import os  
 import socket  
 # Connect to Redis  
 redis = Redis(host="redis", db=0, socket_connect_timeout=2, socket_timeout=2)  
 app = Flask(__name__)  
 @app.route("/")  
 def hello():  
   try:  
     visits = redis.incr("counter")  
   except RedisError:  
     visits = "<i>cannot connect to Redis, counter disabled</i>"  
   html = "<h3>Hello {name}!</h3>" \  
       "<b>Hostname:</b> {hostname}<br/>" \  
       "<b>Visits:</b> {visits}"  
   return html.format(name=os.getenv("NAME", "world"), hostname=socket.gethostname(), visits=visits)  
 if __name__ == "__main__":  
   app.run(host='0.0.0.0', port=80)  

requirements.txt
 Flask  
 Redis  

Note:
When the Dockerfile is built into an image, Dockerfile’s ADD command will add app.py into the image, and the app is served over HTTP with help the EXPOSE command which expose port 80. PIP command install python dependency libraries specified on requirements.txt file

Build the app

Now execute the build command from top of the directory. This creates a Docker image with tag using -t so it has a friendly name.
In my case directory is /Documents/my-docker-img and image name is my-app
 docker build -t my-app .  
To view the list images created on your machine
 docker image ls  

Run the app

Run the app, by mapping your machine’s port 8000 to the container’s published port 80 using -p
 docker run -p 4000:80 friendlyhello  
Then you can access your app deployed within the container using following URL
 http://localhost:8000/  
If you want to run the container in the background in detach mode
 docker run -d -p 4000:80 my-app  
To view all the running containers on your machine
 docker container ls  
If you want to stop the running container using CONTAINER ID
  sudo docker container stop c1081125c6e9   

Next Share your docker image

Useful links
https://docs.docker.com/get-started/part2/#log-in-with-your-docker-id

Installing Docker in Ubuntu

Set up repository to install dependent package for Docker installation
 sudo apt-get update  
Install packages to allow apt to use a repository over HTTPS:
  sudo apt-get install \  
   apt-transport-https \  
   ca-certificates \  
   curl \  
   software-properties-common  
Add Docker’s official GPG key:
 curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -  
To Verify the key with the fingerprint by matching the last 8 characters of the fingerprint.
 sudo apt-key fingerprint 0EBFCD88  
Set up the stable repository I have chosen stable repository and x86_64/amd64. For different version please follow the link
 sudo add-apt-repository \  
   "deb [arch=amd64] https://download.docker.com/linux/ubuntu \  
   $(lsb_release -cs) \  
   stable"  
Now to install Docker
Update the apt package index
 sudo apt-get update  
Install the latest version of Docker CE
 sudo apt-get install docker-ce  
To verify that Docker CE is installed correctly by running the hello-world image
 sudo docker run hello-world  
It will download a test image and run it in a container. When the container runs, it prints an informational message and exits. Now Docker CE is installed and running on your machine.

If you need to uninstall Docker installed
 sudo apt-get purge docker-ce  
To delete all images, containers, and volumes which is not removed automatically
 sudo rm -rf /var/lib/docker  
If you want to configure Linux as post installation Click the Link. Some Useful Commands: To see list of images downloaded image on your machine.
 docker image ls  
To get help
 docker container --help  
Next Define your own container with Docker file

Helpful links
https://docs.docker.com/get-started/#conclusion-of-part-one
https://github.com/docker/labs/tree/master/beginner/

SQL INTERVIEW QUESTIONS AND ANSWERS