DOCKER. Error when creating an image with dockerfile - java

I need to containerize my project. I use Intellij idea and all command i write in the intellij terminal. Commands that I use:
$ mvn package
$ docker build -f Dockerfile -t week10 .
Image hello-world is working correctly.
I create executable jar and Dockerfile. When I enter the command to create image I get the following error:
error during connect: Post
http://%2F%2F.%2Fpipe%2Fdocker_engine/v1.40/build?buildargs=%7B%7D&cachefrom=%5B%5D&cgroupparent=&cpuperiod=0&cpuquota=0&cpusetcpus=&cpusetmems=&cpushares=0&do
ckerfile=Dockerfile&labels=%7B%7D&memory=0&memswap=0&networkmode=default&rm=1&session=jauqgloaecpwv5hch49bgfcak&shmsize=0&t=week10&target=&ulimits=null&version=1:
open //./pipe/docker_en gine: The system cannot find the file
specified. In the default daemon configuration on Windows, the docker
client must be run elevated to connect. This error may also indicate
that the docker daemon is not running.
Dockerfile:
FROM openjdk:8-jdk-alpine
ADD target/week10-1.0-SNAPSHOT.jar week10.jar
EXPOSE 8080
ENTRYPOINT ["java","-jar","week10.jar"]
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>week10</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.2.6.RELEASE</version>
<configuration>
<finalName>week10</finalName>
<mainClass>com.fruitshop.Application</mainClass>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<properties>
<start-class>com.fruitshop.Application</start-class>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
</project>
You can see my entire project here: Project
I don't know what i did wrong. Please indicate what the problem is and how I can solve it

The error is not due to the Dockerfile it is related to that part:
In the default daemon configuration on Windows, the docker client must
be run elevated to connect
You can do this in order to switch Docker daemon:
Open Powershell as administrator
Run following command:
cd "C:\Program Files\Docker\Docker"
./DockerCli.exe -SwitchDaemon

I resolved this error. If you have the same error, you need to enter this command in the terminal:
Docker-machine env default
and then you need to enter the sets, that you have displayed on the screen, example:
SET DOCKER_HOST=tcp://192.168.99.100:2376
SET DOCKER_CERT_PATH=<your home path>\.docker\machine\machines\default
SET DOCKER_MACHINE_NAME=default
SET COMPOSE_CONVERT_WINDOWS_PATHS=true```

Related

SonarQube - mvn sonar : sonar - NoSuchElementException

I have a project on SonarQube but when I try to update it with the follow command in the terminal it gets me the this error :
[ERROR] NoSuchElementException
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
I have searched online but apparently nobody knows the answer, so I thought I'd give it a try.
I have the following project structure:
Project name
Dal
SharedModel
WebsocketClient
WebsocketServer
So in total I have 4 modules. My main pom.xml looks like this :
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.8.RELEASE</version>
</parent>
<groupId>myproject.nl</groupId>
<artifactId>myproject</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>websocketserver</module>
<module>websocketsclient</module>
<module>sharedmodel</module>
<module>Dal</module>
</modules>
<name>PartieKo</name>
<dependencies>
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</dependency>
<!-- JUnit 5 -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.5.2</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M3</version>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Your command is wrong:
mvn sonar : sonar // WRONG
Remove the spacers between sonar and sonar and add the Sonar server address:
mvn sonar:sonar -Dsonar.host.url=https://mysonarserver:9000
The sonar server configuration can also be configured in the pom.xml file (add in the properties section)
<properties>
...
<sonar.host.url>https://mysonarserver:9000</sonar.host.url>
</properties>
Command mvn sonar scanner on windows
mvn sonar:sonar `
"-Dsonar.projectKey=xxxxx" `
"-Dsonar.host.url=http://local:9000" `
"-Dsonar.login=xxxxxxxxx"
Linux
After adding project on sonar console manualy,sonar will give you the next command.
mvn sonar:sonar \
"-Dsonar.projectKey=xxxxx" \
"-Dsonar.host.url=http://localhost:9000" \
"-Dsonar.login=xxxxxxxxx"
You can also put url/host/login in pom file or env config.

Maven not downloading jars behind proxy

Maven is unable to download jar (s) (Dependency) behind proxy.
Here is my pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.faizan.firstbootapp.group1</groupId>
<artifactId>com-faizan-firstbootapp-group-1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>FirstBootApp</name>
<description>Test app for Maven build system</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Here is my settings.xml at path C:\Users\faizanmubasher\.m2\settings.xml. I have added proxies in it.
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
https://maven.apache.org/xsd/settings-1.0.0.xsd">
<proxies>
<proxy>
<id>skm--http-proxy</id>
<active>true</active>
<protocol>http</protocol>
<host>my.proxy.ip.here</host>
<port>8080</port>
<username>faizanmubasher</username>
<password>*****</password>
</proxy>
<proxy>
<id>skm--https-proxy</id>
<active>true</active>
<protocol>https</protocol>
<host>my.proxy.ip.here</host>
<port>8080</port>
<username>faizanmubasher</username>
<password>*****</password>
</proxy>
</proxies>
</settings>
Username and password are correct.
Is it due to my office's proxy that is not allowing to download jar or is it some Maven settings that I need to configure?
I am facing this error on STS IDE.
The container 'Maven Dependencies' references non existing library 'C:\Users\faizanmubasher\.m2\repository\org\springframework\boot\spring-boot-autoconfigure\2.0.2.RELEASE\spring-boot-autoconfigure-2.0.2.RELEASE.jar' FirstBootApp Build path Build Path Problem
My Maven version is Apache Maven 3.5.3.
EDIT
The command set MAVEN_OPTS="-DhttpProxyHost=xxx.xx.xx.xx -DsocksProxyPort=8080" doesn't work as well.
Try this, if 1 does not work try 2, then 3:
1) First update the project, Alt-F5
2) Then go to your repo and blow away the directory for that dependency which should be in the directory C:\Users\faizanmubasher.m2\repository\org\springframework\boot\spring-boot-autoconfigure\2.0.2.RELEASE Then do an Alt-F5 again.
3) If this fails try a mvn clean install -U from the command line.
You can test your maven to maven central repo connectivity with the following command:
mvn archetype:generate
You should see over 2000 archetypes returned
I have encountered the same problem.
And I turned back to older versions of maven which is 3.1.0.
I was trying to use a http proxy to get reached to maven https
repository.
Here, there is a issue for newer versions in maven's jira.
https://issues.apache.org/jira/browse/PROXY-27

Buildning docker image from Dockerfile with maven - Error: "Could not find or load main class"

I'm building a docker image from a jax-rs web application built with Maven. The project builds an image from the Dockerfile, but when I'm about to run the container I get Error: Could not find or load main class.
I've been looking through the docker image with docker inspect and noticed that the Entrypoint from the Dockerfile is not copied in the right way. I have another project where building the image and running it is working, where the Dockerfile is set up in the same way, and in those case the ENTRYPOINT is copied correctly. So I think this is the problem, but I haven't found I way to solve it.
Edit:
After correcting an error in my Dockerfile, I can exclude that the copying of entrypoints to the image is the problem. However, the error remains: I now get the error "Could not find or load main class .opt.cartservicejava-swarm.jar" where before it was only "Could not find or load main class".
This is the pom.xml file
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.cartservicejava</groupId>
<artifactId>cartservicejava</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<build>
<finalName>cartservicejava</finalName>
<plugins>
<plugin>
<groupId>org.wildfly.swarm</groupId>
<artifactId>wildfly-swarm-plugin</artifactId>
<version>${version.wildfly-swarm}</version>
<executions>
<execution>
<goals>
<goal>package</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<properties>
<failOnMissingWebXml>false</failOnMissingWebXml>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<version.wildfly-swarm>2017.7.0</version.wildfly-swarm>
</properties>
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.wildfly.swarm</groupId>
<artifactId>jaxrs</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.wildfly.swarm</groupId>
<artifactId>bom-all</artifactId>
<version>${version.wildfly-swarm}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
This is the Dockerfile
FROM openjdk:jre-alpine
COPY target/cartservicejava-swarm.jar /opt/cartservicejava-swarm.jar
EXPOSE 8080
# preferIPv4Stack is needed to keep wildfly-swarm happy
ENTRYPOINT ["java", "-Djava.net.preferIPv4Stack=true", "/opt/cartservicejava-swarm.jar"]

JDBC driver class not loading for JAVA app when it connects to Postgres in Docker

I have a docker container set up locally with CentOS and Boot2Docker. The goal is to install a JAVA application that connects to Postgres. Java JDK and JRE for 1.8 are installed. Postgres is up and running. I am able to connect to it with psql and do work.
The Java application was compiled as a jar file with Intellij. Attached is the POM.xml file. All is well in intellij. However, when I try to run the app in docker from the command line with: java -jar APP_NAME.jar, I get the following error message:
"PostgreSQL 9.4.1212.jre7
Found in: jar:file:/demo_data/out/artifacts/demo_data_jar/demo_data.jar!/org/postgresql/Driver.class
The PgJDBC driver is not an executable Java program.
You must install it according to the JDBC driver installation instructions for your application / container / appserver, then use it by specifying a JDBC URL of the form
jdbc:postgresql://
or using an application specific method.
See the PgJDBC documentation: http://jdbc.postgresql.org/documentation/head/index.html
This command has had no effect."
Here is the POM:
http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
<groupId>com.align</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo_data</name>
<description>Program to create demo data for align care.</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-codec/commons-codec -->
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.9</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
The jar you created is using the main class from the PostgreSql jdbc driver (that serves only to warn people that try to run it like java -jar postgresql_jdbc.jar, that it does not make sense).
So make sure while you create your jar, that you specify the right main class.

How to deploy spring boot app project to Heroku

I have created a simple server that uses JPA repository and returns a response in Json. It includes methods post and get. It is a spring starter boot app project and everything works on my localhost (I use postman to send and receive json objects). My problem is when I try to deploy to Heroku I run into many problems. I added a jetty-runner dependency and plugin. I also created a procfile as shown below:
web: java $JAVA_OPTS -jar target/dependency/jetty-runner.jar --port $PORT target/*.war
Initially the target folder was empty and foreman start web was saying that it couldn't find my war file, so I added the following line to pom.xml:
<packaging>war</packaging>
But the project created an error, so I disabled maven nature of the project, then configured maven again and then it included pom.properties and pom.xml in the target folder. When I tried to deploy it, it said it was unable to access jetty jar file in target/dependency because there was no folder like that. So I did maven install and it installed the missing folders. Now I get the error that No transaction manager can be found, so I installed a dependency for jetty-plus and Atomikos. But now I still get the error that there is not transaction manager found and there is a java.net.bindexception.
I feel like I am really on the wrong path. I was wondering if anyone can tell me from the beginning on how to deploy a spring starter boot project to heroku. Any help would really be appreciated.
This is my pom.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.test</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>Challenge-Server1</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
<dependency>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-runner</artifactId>
<version>7.4.5.v20110725</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jta-atomikos</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<phase>package</phase>
<goals><goal>copy</goal></goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-runner</artifactId>
<version>7.4.5.v20110725</version>
<destFileName>jetty-runner.jar</destFileName>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
I found out that the deploying a spring boot application is a little different then deploying spring boot mvc project. Basically you don't need a jetty-runner dependency or plugin, you just do the following steps:
Configure a mvn project
mvn clean and install
Declare Procfile as: web: java $JAVA_OPTS -Dserver.port=$PORT -jar target/*.jar
All found from this site:
http://web.archive.org/web/20171018145733/http://nicholaspaulsmith.com/spring-boot-on-heroku/
I outlined some details in my blog
https://exampledriven.wordpress.com/2016/11/04/spring-boot-heroku-example/
The main point is to use the heroku maven plugin like this
heroku plugins:install heroku-cli-deploy
mvn clean install
# Creates an app with the specified name, without setting up a git remote
heroku create <APP-NAME> --no-remote
#deploys the jar file
heroku deploy:jar target/demo-0.0.1-SNAPSHOT.jar --app <APP-NAME>
but there are more details like how to set up a CI/CD pipeline

Categories

Resources