I am trying to create a docker image for my java application. At startup this application needs to be given a password (currently via console).
I tried several methods of obtaining input however they have all failed. Is this a limitation of docker and if so is there a workaround?
For this snippet:
Console console = System.console();
if(console == null){
    System.out.println("console is null!!");
} else {
    System.out.println("Input password: ");
    char[] password = console.readPassword("Pass: ");
}
System.console() is returning null.
For this snippet:
    System.out.println("Creating InputStreamReader");
    InputStreamReader s = new InputStreamReader(System.in);
    System.out.println("Creating BufferedReader");
    BufferedReader r = new BufferedReader(s);
    System.out.println("Input password: ");
    String password = r.readLine();
    System.out.println("Password: "+password);
the input is automatically skipped, (resulting in the String password to be null) with the program continuing execution as if there was no input requested. (password is null)
For this snippet:
Scanner s = new Scanner(System.in);
System.out.println("Input password: ");
String password = s.next();
I get
Exception in thread "main" java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Scanner.java:907)
    at java.util.Scanner.next(Scanner.java:1416)
    at com.docker.test.DockerTest.testScanner(DockerTest.java:49)
    etc...
I am running the program from within my image using docker run test/plaintest1 
my dockerfile is as follows
FROM centos
RUN yum install -y java-1.7.0-openjdk
ADD DockerTest.jar /opt/ssm
ENTRYPOINT ["java","-jar","/opt/ssm/DockerTest.jar"]
CMD [""]
                Solved it.
By running the command using the -i and -t parameters you can be allowed to enter the password. using all 3 methods.
so basically docker run -i -t <imagename> <params>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With