Docker Basic Instructions

Docker Image – Read only layer
Docker Container – Read/Write layer

1. Run docker bash in interactive mode for latest ubuntu image

docker run -i -t ubuntu:latest /bin/bash

2. Run docker bash in server mode for latest ubuntu image

docker run -d -t ubuntu:latest /bin/bash

3. Check running containers

docker ps

Output:

CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS              PORTS                  NAMES
327eb315fd8d        ubuntu:latest       "/bin/bash"         About a minute ago   Up About a minute                          sharp_yonath
2e27eb9e09b6        ubuntu:latest       "/bin/bash"         7 minutes ago        Up 7 minutes                               quizzical_jang

4. Check docker server status

/etc/init.d/docker status
service docker status

5. Start|Stop|Restart docker server

/etc/init.d/docker start|stop|restart
service docker start|stop|restart

6. Docker remove image

docker rmi ubuntu:latest

7. Docker start and stop container

docker start mycontainer
docker stop mycontainer

8. Docker remove container

docker rm mycontainer

9. Docker remove all exiting containers

docker rm $(docker ps -q -f status=exited)

10. Copy docker log file out of container for debugging purposes

docker cp 04421c5455b9:/var/log/mysql/error.log /home/imran/myimages

Git tag

Add new tag:

git tag -a 1.1.0 -m "Version 1.1.0";
git push --tags

List tags:

git tag

Delete local tag:

git tag -d 1.0

Delete remote tag:

git push origin :refs/tags/1.0

Laravel install particular version

composer create-project laravel/laravel myproject 5.1

Php double exclamation operator (!!) or double not operator

The right ! will result in a boolean, regardless of the operand. Then the left ! will be applied to that boolean. It means if $row has a truthy value, it will return true, otherwise false, converting to a boolean value.
It is equalent of casting to boolean as it convert anything to boolean.

return (bool)$row;

Another more human, maybe simpler, way to ‘read’ the not not:
The first ‘!’ does 2 things: ‘convert’ the value to boolean, then output its opposite. So it will give true if the value is a ‘falsy’ one.
The second ‘!’ is just to output the opposite of the first.

Java keeping config values in property file

1. File Simple.java

import java.io.FileInputStream;
import java.util.Properties;
import java.io.IOException;
import java.io.FileNotFoundException;
 
class Simple {
 
	public static void main(String[] args) {
 
	try {
 
	Properties config = new Properties();
 
	String configFile =  "config.properties";
	FileInputStream input = new FileInputStream("config.properties");
 
	config.load(input);	
 
    	String valueStr = config.getProperty("textlocal_key");
	System.out.println(valueStr);
 
	} catch (FileNotFoundException e) {
		e.printStackTrace();
	} catch (IOException e) {
		e.printStackTrace();
	}
 
   	}
}

2. Compile and run

javac Simple.java;
java Simple

Laravel new console command

1. Create

php artisan make:command MyApp --command=myapp:perform

2. Edit

vi app/Console/Commands/MyApp.php
....
    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        echo 'My App';
    }

3. Run

php artisan myapp:perform

Java Convert Integer to String

String.valueOf(Integer);

Simple JSON

$data['message'] = 'Simple Message';
$data['status'] = 'success';
 
header('Content-Type: application/json');
echo json_encode($data);

Abstract class – is something between a regular class and a pure interface. The purpose of this is to provide a kind of template to inherit from and to force the inheriting class to implement the abstract methods.

Interface – is a special case of abstract classes where ALL methods are abstract.

Grep show next or previous line when for matched value

Show next line after matched line

grep -A1 searchpattern filename

Show next 2 lines after matched line

grep -A2 searchpattern filename

Show previous line after matched line

grep -B1 searchpattern filename