Saturday, July 11, 2015

Where are docker images stored locally ?

http://blog.thoward37.me/articles/where-are-docker-images-stored/

Local Storage on the Docker Host

So far I've been explaining the intricacies of remote storage, and how that relates to the confusing vocabulary, but running docker images shows you only what is local to your machine.
Where is this stuff? The first place to look is in /var/lib/docker/.

Open up the file repositories to find a JSON list of the repositories on your host:
$ sudo cat /var/lib/docker/repositories | python -mjson.tool
{
    "Repositories": {
        "ubuntu": {
            "12.04": "8dbd9e392a964056420e5d58ca5cc376ef18e2de93b5cc90e868a1bbc8318c1c",
            "12.10": "b750fe79269d2ec9a3c593ef05b4332b1d1a02a62b4accb2c21d589ff2f5f2dc",
            "latest": "8dbd9e392a964056420e5d58ca5cc376ef18e2de93b5cc90e868a1bbc8318c1c",
            "precise": "8dbd9e392a964056420e5d58ca5cc376ef18e2de93b5cc90e868a1bbc8318c1c",
            "quantal": "b750fe79269d2ec9a3c593ef05b4332b1d1a02a62b4accb2c21d589ff2f5f2dc"
        }
    }
}
 
Hey, that matches the output from docker images!
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              12.04               8dbd9e392a96        8 months ago        131.3 MB (virtual 131.3 MB)
ubuntu              latest              8dbd9e392a96        8 months ago        131.3 MB (virtual 131.3 MB)
ubuntu              precise             8dbd9e392a96        8 months ago        131.3 MB (virtual 131.3 MB)
ubuntu              12.10               b750fe79269d        8 months ago        24.65 kB (virtual 179.7 MB)
ubuntu              quantal             b750fe79269d        8 months ago        24.65 kB (virtual 179.7 MB)

How to enter a Docker

https://blog.codecentric.de/en/2014/07/enter-docker-container/

nsenter
The nsenter tool is part of the util-linux package since version 2.23. It provides access to the namespace of another process. nsenter requires root privileges to work properly. Unfortunately, util-linux is still at version 2.20 in Ubuntu 14.04.

To install the latest version (2.24) proceed as follows:
cd /tmp
curl https://www.kernel.org/pub/linux/utils/util-linux/v2.24/util-linux-2.24.tar.gz | tar -zxf-
cd util-linux-2.24
./configure --without-ncurses
make nsenter
cp nsenter /usr/local/bin
 
In order to connect to a container, you have to find out the PID of the first process in the container.
docker inspect --format "{{ .State.Pid }}" <container-id>
With that PID you can connect to the container:
nsenter --target $PID --mount --uts --ipc --net --pid

Docker nsenter

https://blog.docker.com/tag/nsenter/

Introducing nsenter

nsenter is a small tool allowing to enter into namespaces. Technically, it can enter existingnamespaces, or spawn a process into a new set of namespaces. “What are those namespaces you’re blabbering about?” They are one of the essential constituants of containers. The short version is: with nsenter, you can get a shell into an existing container, even if that container doesn’t run SSH or any kind of special-purpose daemon.

Where do I get nsenter?

Check jpetazzo/nsenter on GitHub. The short version is that if you run:
docker run -v /usr/local/bin:/target jpetazzo/nsenter
This will install nsenter in /usr/local/bin and you will be able to use it immediately. nsenter might also be available in your distro (in the util-linux package).

How do I use it?

First, figure out the PID of the container you want to enter:
PID=$(docker inspect --format {{.State.Pid}} )
Then enter the container:
nsenter --target $PID --mount --uts --ipc --net --pid
You will get a shell inside the container. That’s it. If you want to run a specific script or program in an automated manner, add it as argument tonsenter. It works a bit like chroot, except that it works with containers instead of plain directories.



Reference:

Backup data, check logs, restart service ... use volume
https://docs.docker.com/userguide/dockervolumes/


Saturday, May 2, 2015

Java String

http://javaconceptoftheday.com/tutorial-examples-strings-in-java/

Time taken by String class : 429 ms
Time taken by StringBuffer class : 2 ms
Time taken by StringBuilder class : 0 ms

Therefore, when you are performing lots of string concatenation in your application, it is better to use StringBuffer class (if you need thread safety) or StringBuilder class (If you don’t need thread safety).
Time taken by String class : 429 ms
Time taken by StringBuffer class : 2 ms
Time taken by StringBuilder class : 0 ms
Therefore, when you are performing lots of string concatenation in your application, it is better to use StringBuffer class (if you need thread safety) or StringBuilder class (If you don’t need thread safety).
- See more at: http://javaconceptoftheday.com/stringbuffer-stringbuilder-string-class/#sthash.jQ0l5vUm.dpuf
Time taken by String class : 429 ms
Time taken by StringBuffer class : 2 ms
Time taken by StringBuilder class : 0 ms
Therefore, when you are performing lots of string concatenation in your application, it is better to use StringBuffer class (if you need thread safety) or StringBuilder class (If you don’t need thread safety).
- See more at: http://javaconceptoftheday.com/stringbuffer-stringbuilder-string-class/#sthash.jQ0l5vUm.dpuf
Time taken by String class : 429 ms
Time taken by StringBuffer class : 2 ms
Time taken by StringBuilder class : 0 ms
Therefore, when you are performing lots of string concatenation in your application, it is better to use StringBuffer class (if you need thread safety) or StringBuilder class (If you don’t need thread safety).
- See more at: http://javaconceptoftheday.com/stringbuffer-stringbuilder-string-class/#sthash.jQ0l5vUm.dpuf

Saturday, April 11, 2015

Expect for Java


https://github.com/dacr/jassh

#!/bin/sh
exec java -jar jassh.jar "$0" "$@"
!#
jassh.SSH.once("localhost", "test", "testtest") { ssh =>
  print(sh.execute("""echo "Hello World from `hostname`" """))
}

#!/bin/sh
exec java -jar jassh.jar "$0" "$@"
!#
jassh.SSH.shell("localhost", "test", "testtest") { sh =>
  import sh._
  println(s"initial directory is ${pwd}")
  cd("/tmp")
  println(s"now it is ${pwd}")
} 


https://github.com/ronniedong/Expect-for-Java
try {
    JSch jsch = new JSch();
    Session session = jsch.getSession(USER, HOST);
    session.setPassword(PASSWD);
    session.setConfig("StrictHostKeyChecking", "no");
    session.connect(60 * 1000);
    Channel channel = session.openChannel("shell");
    Expect expect = new Expect(channel.getInputStream(),
            channel.getOutputStream());
    channel.connect();
    expect.expect("$");
    System.out.println(expect.before + expect.match);
    expect.send("ls\n");
    expect.expect("$");
    System.out.println(expect.before + expect.match);
    expect.send("exit\n");
    expect.expectEOF();
    System.out.println(expect.before);
    expect.close();
    session.disconnect();
} catch (JSchException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
} 
 
http://www.jcraft.com/jsch/