Betzen Ben Betzen Ben - 6 months ago 75
Bash Question

How to Login,Tag, Push an Docker Image to AWS using shell script called from java

Hello I am trying to push a docker image via shell command programming to AWS EC2 Container Service. But I am having trouble doing that and I am getting the following error messages:

error getting credentials - err: exec: "docker-credential-osxkeychain": executable file not found in $PATH, out: ``
no basic auth credentials

This error message is returned from my java class. If I start the shell script form the terminal i am having no problems only if I start it form my java class. I created an config.js file an added osxkeychain to it because i thought this might save the problem.

This is my Dockerfile:

FROM java:7
COPY . /Users/betzenben/Desktop/OGC/Projects/Getting_started/Docker/Directory
WORKDIR /Users/betzenben/Desktop/OGC/Projects/Getting_started/Docker/Directory
RUN javac Time_app.java
CMD ["java", "Time_app"]
Run : ~/Users/betzenben/Desktop/OGC/Projects/Getting_started/Docker/Directory/config.json


And this my config.json file

{
"apps": [
{
"credsStore": "osxkeychain"
}
]
}


And just in case it is needed my shell script code and my java class that calls the shell script.

#!/bin/sh

echo “test1”
getLoginKey="/usr/local/bin/"
getLoginKey+="$(/usr/local/bin/aws ecr get-login --no-include-email --region us-west-2)"
echo “test2”
echo "${getLoginKey}"

executeLoginKey="$(eval $getLoginKey)"
echo “test3”
sleep 2
echo "${executeLoginKey}"


tagImage="$(/usr/local/bin/docker tag time_app:latest .....id......dkr.ecr.us-west-2.amazonaws.com/time_a:latest)"
pushImage="$(/usr/local/bin/docker push .....id.......dkr.ecr.us-west-2.amazonaws.com/time_a:latest)"
wait
echo “test4”
sleep 5
echo "${pushImage}"
echo "Image Pushed"


Java Code:

import java.io.*;

public class Main {

public static void main(String[] args) throws InterruptedException, IOException {

Process p1 = Runtime.getRuntime().exec("chmod +x /Users/betzenben/Desktop/tag_push_image_AWS.sh");

BufferedReader stdInput1 = new BufferedReader(new InputStreamReader(p1.getInputStream()));
BufferedReader stdError1 = new BufferedReader(new InputStreamReader(p1.getErrorStream()));

System.out.println("STDOUT:\n");
String s1 = null;
while ((s1 = stdInput1.readLine()) != null) {
System.out.println(s1);
}
System.out.println("STDERR:\n");
while ((s1 = stdError1.readLine()) != null) {
System.out.println(s1);
}

Process p = Runtime.getRuntime().exec("/Users/betzenben/Desktop/tag_push_image_AWS.sh");

BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));

System.out.println("STDOUT:\n");
String s = null;
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}
System.out.println("STDERR:\n");
while ((s = stdError.readLine()) != null) {
System.out.println(s);
}
}
}

Answer Source

In your tag_push_image_AWS.sh file change

#!/bin/sh

to

#!/bin/bash

If that doesn't work then try below code in java

Runtime.getRuntime().exec(new String[]{"/bin/bash","-lc", "/Users/betzenben/Desktop/tag_push_image_AWS.sh"});
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download