Cannot see Java Maven Tomcat-plugin web application in web browser - java

I want to build and run my Java Maven web app in a docker container. I tried with a following command:
docker run -it --name my_project -v "$PWD":/usr/src/my_project -w /usr/src/my_project maven:3.5.0-jdk-8 mvn clean install tomcat7:run
It correctly copies the resources, run maven clean install (successful build) and run with tomcat7-maven-plugin that is included in my pom.xml.
Everything works fine and logs are really similar to build and run locally on my windows machine:
Unfortunately on a web browser there is information "connection refused".
What could potentially cause the problem?:
- my application is windows specific and cannot run on linux?
- app is fully app and running but something wrong is with proxy configuration or port is not configured?
How can i proceed further - investigate the logs? Try to build on windows docker container?
P.S. I check IP of a container with Kitematic app for windows docker.

Possibly three issues. Once your used -w instead of -v
docker run -it --name my_project -v "$PWD":/usr/src/my_project -w /usr/src/my_project maven:3.5.0-jdk-8 mvn clean install tomcat7:run
Which I assumed was a Typo while posting. Next you didn't publish the port on your machine
docker run -p 9998:9998 -it --name my_project -v "$PWD":/usr/src/my_project -w /usr/src/my_project maven:3.5.0-jdk-8 mvn clean install tomcat7:run
This would map the port 9998 (right side) from your container to the port 9998 on your localhost.
Third and last one, your INFO log says listening on localhost:9998. This is not good. Because that means your war is listening from traffic generated inside the the container only and not from outside the container. You need to configure your war so it listens on all interfaces inside the container and bind should be 0.0.0.0:9998

Related

Unable to run with GraalVM a SpringBoot - Postgres application using Docker containers

On Windows 10, with IntelliJ Idea I built a Spring Boot application (with the help of Bootify.io).
That application connects to a Postgres Database that resides in Docker (container-postgres-1) called Bootifytwo.
With the intention of playing and practicing with GraalVM, I downloaded a Docker image from Oracle.
From the corresponding GraalVM container (container-graalvm-1) I have been able to generate the target/bootifytwo executable.
But when I try to run it, it gives me a database connection error.
I put below all the steps that I executed, and after some images.
(Note the use of a network for intercommunication between the containers; as well as the use of volumes for each of the 2 containers).
Help with resolution would be appreciated.
Copy folder C:\CODIGO\IDEA_PROJECTS\bootifytwo to C:\Volumenes-Docker\vol-graalvm-1\bootifytwo
docker network create red-postgres-graalvm-1
docker run --name contenedor-postgres-1 -p 5433:5432 --network red-postgres-graalvm-1 -v "C:\Volumenes-Docker\vol-postgres-1:/var/lib/postgresql/data" -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=password -e POSTGRES_DB=bootifytwo -d postgres:13.9-alpine3.17
docker run --name contenedor-graalvm-1 -it --network=red-postgres-graalvm-1 -v "C:\Volumenes-Docker\vol-graalvm-1:/app" container-registry.oracle.com/graalvm/community:ol8-java17-22.3.0-b1 bash
gu install native-image
cd bootifytwo
. ./mvnw native:compile -Pnative
docker start contenedor-graalvm-1
docker exec -it contenedor-graalvm-1 bash
./target/bootifytwo
The postgresql db runs in an other docker than your application. So you can't connect with localhost.
Change your setup to connect to contenedor-postgres-1

Docker for an Angular-Java app : ports not exposed properly

I am trying to dockerize a full-stack Java(Springboot)-Angular app. The Angular app is embedded in the application and makes REST calls to the Java app as GET localhost:8080/getSomeInfo. My Dockerfile is as
FROM maven:3.6.3-openjdk-8 AS build
COPY src /home/app/src
COPY pom.xml /home/app
RUN mvn -f /home/app/pom.xml clean package
FROM openjdk:8-jdk-alpine
COPY --from=build /home/app/target/*.jar /usr/local/lib/app.jar
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} /usr/local/lib/app.jar
ENTRYPOINT ["java","-jar","/usr/local/lib/app.jar"]
FROM node:12.2.0
WORKDIR /app
ENV PATH /app/node_modules/.bin:$PATH
COPY src/main/web/package.json /app/package.json
RUN npm install
RUN npm install -g #angular/cli#10.1.2
COPY src/main/web /app
CMD ng serve --host 0.0.0.0
I build it as
docker build -t springio/myapp .
I need to expose both the angular port and the java port, so I run it as
docker run -p 9898:4200 -p 8080:8080 -t springio/myapp
I am able to bring up the web page, but it cannot reach out to the Java server (can't connect to 8080). How can this be fixed please?
I have tried putting 'EXPOSE 8080' in the dockerfile, but that did not work
You should not put 2 applications in the same container. Although you can, it is considered a bad practise and it defeats the purpose of using containers.
I suggest having 2 Dockerfile: one for the back-end (java app) and one for the front-end (Angular app). Build each of them separately and start them independently.
For your specific problem I don't think the port is the problem, but is the Java app that is not even starting (it is actually not even present in the final image). Splitting the Dockerfile and starting the apps separately will fix your issue.
EXPOSE doesn't actually do anything. It is there to inform users of the Dockerfile which ports the application is listening on, so that they know which ports to map on the host.

Running test with testcontainers as part of a Dockerfile

My dockerfile looks something like this:
FROM maven:3-jdk-11-slim
COPY pom.xml .
COPY src src
RUN mvn clean install
That means that part of the build is the execution of the unit tests. Some of the unit tests use a testcontainer. Running mvn clean install on my local machine works fine, but running docker build . -t my-app doesn't because the testcontainers won't start.
(...)
15:54:38.793 [ducttape-0] DEBUG org.testcontainers.dockerclient.DockerClientProviderStrategy - Pinging docker daemon...
15:54:38.794 [ducttape-0] DEBUG com.github.dockerjava.core.command.AbstrDockerCmd - Cmd: org.testcontainers.dockerclient.transport.okhttp.OkHttpDockerCmdExecFactory$1#355cb260
15:54:39.301 [ducttape-0] DEBUG org.testcontainers.dockerclient.DockerClientProviderStrategy - Pinging docker daemon...
15:54:39.301 [ducttape-0] DEBUG com.github.dockerjava.core.command.AbstrDockerCmd - Cmd: org.testcontainers.dockerclient.transport.okhttp.OkHttpDockerCmdExecFactory$1#1c1a1359
15:54:39.469 [main] ERROR org.testcontainers.dockerclient.EnvironmentAndSystemPropertyClientProviderStrategy - ping failed with configuration Environment variables, system properties and defaults. Resolved dockerHost=unix:///var/run/docker.sock due to org.rnorth.ducttape.TimeoutException: Timeout waiting for result with exception
org.rnorth.ducttape.TimeoutException: Timeout waiting for result with exception
(...)
I've seen examples of running docker run with working testcontainers, but how do I make my docker build work?
Help is much appreciated.
For future reference: I believe this is simply not possible.
docker run allows you to mount the Docker socket (and thus access the host's Docker daemon) with -v /var/run/docker.sock:/var/run/docker.sock.
docker build doesn't support such an argument.
My workaround will be to modify my Dockerfile to
RUN mvn clean install -Dmaven.test.skip=true and run the unit tests separately.
In the docker file, you need to have docker installed to run TestContainer.
I have learned this from one of the existing projects in my workplace. hopefully, it can give you some hints to solve your problem.
RUN curl -fsSL get.docker.com | sh
RUN curl -L "https://github.com/docker/compose/releases/download/1.24.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose && \
chmod +x /usr/local/bin/docker-compose
In the docker-compose file,
volumes:
- /var/run/docker.sock:/var/run/docker.sock
- /mnt/cachedata/.m2:/root/.m2
- .:/app

How to install Tomcat container for Docker in Fedora 28?

I am new in Docker, I would like to know how to install Tomcat Container from the command line, also, what are the pre-requisites, do I need to download Java on Fedora 28 first? Or Tomcat already contains a JVM ?
The tomcat/8.5/jre8/Dockerfile image definition starts with
FROM openjdk:8-jre
So it already includes a JDK.
All you need to do is run the default Tomcat server (CMD ["catalina.sh", "run"]):
$ docker run -it --rm tomcat:8.0
You can test it by visiting http://container-ip:8080
See more at hub.docker.com/_/tomcat/.
All you need is to install docker first.
The OP adds:
To be able to connect I had to run below command to get the container ID with
docker ps
docker inspect <containerid> | grep "IPAddress"

Move file from Docker container to Host system in Jenkins

I have configured Jenkins in Docker container. I am able to take a build. After a build I want to move WAR file into my Tomcat server which is running in host system. I have added copy command in post build task. Jenkins is not able to move the WAR to host system , since it is running in container.
How to move WAR file from container to Host system ?
Host path : /home/test/tomcat/webapps
Jenkins container path: /var/jenkins/workspace/dev/welcome/target/welcome.war
I would create a volumne when starting jenkins with docker and then copy the war file there with a normal shell jenkins command. I usually do
docker run -d -v /var/run/docker.sock:/var/run/docker.sock -p 8080:8080 -p 50000:50000 -v /home/docker:/var/jenkins_home --net="host" --env JAVA_OPS="-Xms1024m -Xmx1024m" --privileged=true axltxl/jenkins-dood
The -v option is to create a volumne, which is a shared folder for container and host. In my case I use that for having the jenkins configuration outside docker container.

Categories