Building an Android project from the command line with Docker - java

I inherited Android Java-code in my company, without Gradle-files etc, and I want to be able to compile this on my dev-server (I program from a ChromeOS machine, hence a CLI SSH connection to a server where I do dev stuff). Now I found some Docker images like this one (which doesn't even have a working command line example) but I haven't managed to create an APK yet. What am I missing and how would you do this?

You have three steps to do:
Migrate your project to gradle.
It isn't too difficult since there are plenty of gradle project out there and you can try to follow them or just read "Migrating to Gradle" article.
Build project with gradle on local machine.
If you migrated properly you can build your project in terminal like:
./gradlew assembleDebug
but it might be also assembleDevDebug or assembleProdRelease which depends on your buildType and flavor in gradle. Check which assembles are available by running:
./gradlew tasks
Build project using Docker.
Based on image you linked:
docker run -t -i ksoichiro/android -v /path/to/project:/workspace -w workspace /bin/sh -c "./gradlew assembleDebug"

Related

Running application with java + maven + tomcat (embedded) the port is not found

I've got no previous experience with java. Trying to run project from github - https://github.com/ESPD/ESPD-Service. The Service uses Maven for building multi-module Maven project. I'm using mvn clean package in order to generate the project.war file. Then type java -Dspring.profiles.active=prod -Xms768m -Xmx768m -jar project.war and the app is running in a Tomcat container with port 8080 and it shows on the cmd console that the app is running perfectly but the problem comes when i try to access the localhost:8080. It says Not Found and I've been looking all over the internet to find a solution but without success. Will appreciate a lot if someone gives me a hand on that one.
Java version should be JDK 1.8,
Maven version must be no greater than 3.5.0
Then add 2 libraries to ESPD-Service\espd-web\target\espd-web\WEB-INF\lib folder: mail-1.4.7.jar and activation-1.1.1.jar cuz they are currently missing.
After that run mvn install in order to install all the jars files and then run the java -Dspring.profiles.active=prod -Xms768m -Xmx768m -jar espd-web.war as it's written on the docs. And also when the app is running on localhost:8080 make sure you add localhost:8080/espd

Why is OpenJDK 8 not pulling proper JavaFX libraries when using Docker?

I have a java program that I wrote. The main things include OpenJDK8, Maven, and JavaFX. The program builds and runs on its own. I want to put it in a Docker container, but I'm having trouble getting it to build.
Here is my Dockerfile:
FROM openjdk:8-jdk
FROM maven:3.3-jdk-8-onbuild
RUN apt-get update && apt-get install -y --no-install-recommends openjfx && rm -rf /var/lib/apt/lists/*
CMD ["java","-jar","target/"CodeDemo-1.0-SNAPSHOT.jar"]
Here is what I ran to build the container:
sudo docker build -t java-maven-code-demo .
Here is the error I keep getting complaining about no javafxpackager:
Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.2.1:exec
(unpack-dependencies) on project CodeDemo: Command execution failed.
Cannot run program
"/usr/lib/jvm/java-8-openjdk-amd64/jre/../bin/javafxpackager" (in
directory "/usr/src/app"): error=2, No such file or directory -> [Help
1]
I have all the files in a CodeDemo directory. At the top level, I have src, target, Dockerfile, pom.xml. In target, I have the compiled jar.
I'm confused by the error because I thought Java 8 OpenJDK came with JavaFX. So, if I'm pulling OpenJDK, I should be gettng the things I need for JavaFX (similar question on GitHub - solution still gave the error though).
Can anyone point me in the direction of what I could be doing wrong? Is there something else I should be doing to get the proper libraries?
You have multiple FROM lines. Based from your Dockerfile, only the maven base image will be used. Maybe try installing openjdk through another RUN statement before installing openjfx?
I also don't see any COPY statement in your Dockerfile. I initially assumed CodeDemo-1.0-SNAPSHOT.jar exists by default on the maven image but I just tried building the image and it doesn't exist. If that's the jar file from your Java program, don't forget to add it through a COPY statement in your Dockerfile.

Docker agent in Jenkinsfile correct syntax and command not found error in the output log

I checked the other posts but they are not what my issue represents.
I'm doing the Linuxacademy "Certified Jenkins Engineer" course and at the "Functional Testing" lesson, we add a docker agent with some steps in the Jenkinsfile but I am confused about the syntax used vs the syntax described in the official Jenkins Pipeline documentation Using Docker with Pipeline.
What the Jenkins tries to achieve is to run a test on a.jar file on a Jenkins node with CentOS but the test needs to be run on a Debian OS, in order to do that on the CentOS node, the Jenkinsfile has a stage with a Docker agent and a commmand that pulls the openjdk image from Dockerhub and run some command in it.
This is the syntax from the Lesson Repo:
stage("Test on Debian") {
agent {
docker 'openjdk:8u121-jre'
}
steps {
sh "wget http://brandon4231.mylabserver.com/rectangles/all/rectangle_${env.BUILD_NUMBER}.jar"
sh "java -jar rectangle_${env.BUILD_NUMBER}.jar 3 4"
Note that I simplified the file to match the work in progress, this one is the final version, but the focus is on the agent line.
My first question is that the Jenkins documentation syntax is different from the one used here but in the lesson video it runs with no issues, the correct syntax should be agent {
docker { image 'openjdk:8u121-jre' }
}
My second question is, wheter I use one or the other syntax, and also I used the openjdk:7u181-jre because the one from the lesson is not longer available, I get this error in the console log output:
If go to the node terminal and manually run
docker run openjdk:7u181-jre
it works fine, I run it as not sudo user.
Also I don't understand what the docker command does in the Jenkinsfile: Does it run the container after pulling it or just pull the container?
Any idea about what's going on?
Thanks.
1) Please install docker in the Jenkins server.
2) Please change the user-mode of Jenkins and docker same by running below command and restart both Jenkins and docker and now u will be able to access.
sudo usermod -aG docker jenkins-server-user-name
sudo systemctl restart docker
sudo systemctl restart jenkins
To install Jenkins: https://www.digitalocean.com/community/tutorials/how-to-install-jenkins-on-ubuntu-16-04
To install Docker: https://www.digitalocean.com/community/tutorials/how-to-install-and-use-docker-on-ubuntu-16-04
Note: Verified on ubuntu server

Update Docker image and container from Jenkins

I am quite new to Jenkins and Docker so I am stuck with trying to make them work together. What I want is to do next steps:
Build my project war-file on Jenkins (Done)
Update Docker image and container. In my case I want to stop running container (Tomcat on it), change war-file to the newest and then run it again.
I've already deploy my application on Docker, but this app is not updated by Jenkins.
I found some plugins, like docker-build-step or docker-plugin, however there are not enough information and tutorials about it and I find it really annoying spending hours and making random suggestions.
I would appetiate any useful tutorial as more spesific as possible.
Perhaps you don't need any of these plugins. You can implement it using command api of docker.
You need to install docker on the jenkins host machine.
Then in your Jenkins build config mvn plugin(or execute shell) to build target:
clean package assembly:assembly -Dmaven.test.skip=true
execute shell something like:
docker build --net=host -t reg.docker.xxx.com/xxx/xxx:latest ./
docker login --username=xxx --password=xxx reg.docker.xxx.com
docker push reg.docker.xxx.com/xxx/xxx:latest
execute shell bellow:
docker -H tcp://swarm.xxx.com:<port> --tlsverify --tlscacert=./ca.pem --tlscert=./cert.pem --tlskey=./key.pem run -v /home/admin/rc.local:/etc/rc.local:ro reg.docker.xxx.com/xxx/xxx:latest
------Edit-------
use docker upgrade if updating existing containers.

Docker and Java IDE integration

I'll start by saying that I'm not a Java developer and also I'm not a Docker expert.
In order to minimize the gap between frontend and backend (in this specific case, Java) developers I started to put some docker images in place with java and maven and after the build I start a docker container with a volume pointing to the java project (and frontend developers don't have to worry about dependencies or how to run backend services).
Already here I have a question.
I've seen other people building an image with the actual code inside instead of attaching it later, so what's the best case (if there's one)?
I've done this way since I can reuse that image for "every" project and avoid building different images.
For starting/stopping/restarting docker containers I created a script that does all of that, so I can make some changes to the code, bring it down and up again.
It kinda works, and what I mean is, I'm well aware this is not a normal workflow of a Java developer to do that kind of stuff from a console.
So now, to the most important question, how do you integrate docker with a Java IDE?
I know that you can create custom build/run commands but I also read that things like logs are not displayed on the IDE's.
Can someone explain me how are you using Docker + Java IDE's?
Note: Maven is also used for compiling java code, like mvn clean install (if this helps)
I do not use Docker with a Java IDE. I use the IDE (Eclipse) to write and test the code, with Maven to manage the build. Then I have a Dockerfile like this:
FROM java:8
RUN apt-get update || apt-get update
RUN apt-get install -y maven
# Maven installs Java 7, which set itself as the default...
RUN update-alternatives --remove java /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java
CMD java -jar target/main.jar
# Pull down dependencies here to allow Docker to cache more
ADD pom.xml /opt/app/pom.xml
WORKDIR /opt/app
RUN mvn dependency:go-offline -X
# I use the maven-shade-plugin to build a single jar
ADD src /opt/app/src
RUN mvn package
If you build all your images on one machine, then Docker will cache intelligently and you don't need to do anything more. If you want to run across more machines, or you just want to make it explicit, you could do something like this:
base/Dockerfile:
FROM java:8
RUN apt-get update || apt-get update
RUN apt-get install -y maven
RUN update-alternatives --remove java /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java
CMD java -jar target/main.jar
$ docker build -t yourorg/java-base:8 ./base/
myapp/Dockerfile:
FROM yourorg/java-base:8
ADD pom.xml /opt/app/pom.xml
WORKDIR /opt/app
RUN mvn dependency:go-offline -X
ADD src /opt/app/src
RUN mvn package
You don't get as big an effect from Docker with Java, because JARs are already pretty portable and well-contained. I suppose it makes it easy to run different Java versions side-by-side. I use it because it allows me to run applications in different languages without needing to know what's inside the container. I have some in Java, some in Python, some in JavaScript, some in Erlang, but they all get started as docker run -d <flags> myorg/myimage:someversion.

Categories

Resources