I have spring boot project with one page - Hello word!
JAR app perfectly works in a docker container.
But in my host computer docker does not want run app. Page not available.
docker ps say:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4b34df7e3986 quality-serv "java -jar quality-s…" 21 hours ago Up 20 hours 0.0.0.0:8080->8080/tcp quality-service_con
But netstat (admin) does not find listeners on same port
App work only in the container, not in a personal computer.
I try many dif commands:
docker run -p 8080:8080 -d...
docker run --expose 9990 -p 9990:9990 -p 8080:8080 -it
docker run -d --net=host -P
docker run -it -d -p
They did not help.
Run yaml (docker-compose) with image description did not help too...
First make sure, when creating the container, the Dockerfile was exposing port 8080 EXPOSE 8080.
Then, if your application runs on port 8080, docker run -p 8080:8080 should be the command. Don't add -d by now, so you don't run detached mode and can see the logs.
Related
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
I have a project with Spring Boot, MySQL, Liquibase. I wanted to run it through Docker, and added Dockerfile:
FROM openjdk:8
VOLUME /tmp
ADD target/{name_of_project}.jar {name_of_project}.jar
RUN bash -c 'touch /{name_of_project}.jar'
EXPOSE 8080
ENTRYPOINT ["java","-jar","{name_of_project}.jar"]
Building the app like this: docker build -t {name_of_project} .
Then I added app to https://hub.docker.com/
In server (CentOS 7) I run mysql like this:
docker run -d -p 3306:3306 --name {name_of_mysql} -e MYSQL_ROOT_PASSWORD={pwd} -e MYSQL_DATABASE={db_name} mysql
Created network and connected it to mysql:
docker network create {network_name}
docker network connect {network_name} {name_of_mysql}
And finally run the app:
docker run -d -p 80:8080 --name {name_of_project} --net {network_name} -e MYSQL_HOST={name_of_mysql} -e MYSQL_ROOT={root} -e MYSQL_PASSWORD={pwd} -e MYSQL_PORT=3307 {app_name}
It works. However in 1-2 days it stops. I checked endpoints which are not connected with mysql, it works. Also cannot connect to mysql from Intellij Idea:
Saw the logs of mysql. Something like this:
Perhaps there are some kind of ways to avoid this situation or I did something wrong? For example, should I add docker-compose instead of Dockerfile?
There are so many questions like this. However there are no answers. I decided to write step by step, maybe after it, our society will help me. I hope so...
I am trying to dockerize a spring boot web app with mongodb backend.
I run mongo and map it to host with following command:
docker run -p27017:27017 --name my-mongodb-container -d mongo:latest
I have built a jar with spring boot code and am able to run it successfully. It inserts and prints some data.
Now I dockerize this jar with the following Dockerfile
FROM adoptopenjdk/openjdk11
EXPOSE 8080
COPY target/*.jar app.jar
ENTRYPOINT ["java","-jar","app.jar"]
I run the docker container as:
docker run -p 4444:8080 --name mydoctest --link my-mongodb-container doc40
The instance comes up, tries to connect to mongo an fails. But the app get loaded as I have another url that functions properly. However, it just returns hard coded data.
The error that i see in the console
2021-09-23 17:13:02.725 INFO 1 --- [ main] org.mongodb.driver.cluster : Cluster created with settings {
hosts=[localhost:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms'}
2021-09-23 17:13:02.824 INFO 1 --- [localhost:27017] org.mongodb.driver.cluster : Exception in monitor thread whi
le connecting to server localhost:27017
com.mongodb.MongoSocketOpenException: Exception opening socket
at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:70) ~[mongodb-driver-core-4.2.3.jar!/:na]
at com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:143) ~[mongodb-driver-cor
e-4.2.3.jar!/:na]
Any inputs are much appriciated
The problem begins with your connectionString to mongoDb and the architecture of containers
You are trying to connect to localhost:27017 from the java container. In the java container, Mongo is not running. Instead is running in another container. You have to change your connection string to point to my-mongodb-container:27017
I'll recommend using docker networks instead of --link since it is deprecated
https://docs.docker.com/network/links/
I'll give you a quick example
docker network create -d brigde app-network
docker run -p27017:27017 --name my-mongodb-container -d mongo:latest
docker network connect app-network my-mongodb-container
docker run -p 4444:8080 --name mydoctest doc40
docker network connect app-network mydoctest
(I have not tested it, let me know if there is any mistake)
Depends on the Mongo driver you are using to you have to set 2 different variables to change your connection string as stated in Spring Boot and how to configure connection details to MongoDB?
spring.data.mongodb.uri=mongodb://user:secret#mongo1.example.com:12345
spring.data.mongodb.host=127.0.0.1
So you can create the container respectively using
docker run -p 4444:8080 --name mydoctest -e SPRING_DATA_MONGODB_URI=mongodb://my-mongodb-container:27017 doc40
or
docker run -p 4444:8080 --name mydoctest -e SPRING_DATA_MONGODB_HOST=my-mongodb-container doc40
Finally got it to work.
The only change I did was to connect the instance to preferred n/w on startup.
docker run -p27017:27017 --name my-mongodb-container --network=app-network -d mongo:latest
docker run -p 4444:8080 --name mydoctest --network=app-network -e SPRING_DATA_MONGODB_URI=mongodb://my-mongodb-container:27017/test doc40
I was trying to run the container and then add it to a n/w.
What's the IP address of your Docker host machine?
Your app tries to connect to Mongo using localhost. Depending on what Docker installation you are using (taking the problem into account I assume Docker Toolbox), localhost won't work. You might try the IP address of Docker host instead of localhost. Most of the times, the IP is 192.168.99.100 but could be something else as well.
You can find the address by executing docker-machine ip using Docker command line.
Please note: I know this questions is very similar to this one however you'll note that the solution in that case was to EXPOSE the port, which I am already doing. Hence although this questions sounds similar, I think its simply a different problem altogether with similar symptoms as the other question.
Docker Version 17.12.0-ce-mac49 (21995) here. I am experimenting with Docker for the first time and have built my first Docker image. My Dockerfile is:
FROM openjdk:8
RUN mkdir /opt/myapp
ADD build/libs/myapp.jar /opt/myapp
ADD application.yml /opt/myapp
ADD logback.groovy /opt/myapp
WORKDIR /opt/myapp
EXPOSE 9200
ENTRYPOINT java -Dspring.config=. -jar myapp.jar
I build it via:
docker build -t myapp .
Everything succeeds. I then tag it as if I'm going to push it to Quay:
docker tag <imageId> quay.io/myregistry/myapp:0.1.0-SNAPSHOT
However before I publish to Quay I want to run it locally to make sure it works:
docker run -it -p 9200:9200 -d --env-file /Users/myuser/myapp-local.env --name myapp myapp
When I run this I get an indication that the container is running, and I can even see it for a few seconds via docker ps:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f3fa8f7a4288 myapp "/bin/sh -c 'java -D…" Less than a second ago Up 7 seconds 0.0.0.0:9200->9200/tcp myapp
However after a few seconds it stops running and disappears from docker ps altogether:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
Furthermore I'm not able to SSH into the container:
docker exec -it f3fa8f7a4288 bash
Error: No such container: f3fa8f7a4288
...or see any logs/console output.
When I run myapp.jar outside of Docker (as a typical Spring Boot app, it starts up and runs beautifully without exceptions). How can I troubleshoot what is going on?
The docker logs command will show you the output a container is generating when you run it detached (with -d). This is likely to include the error message.
docker logs --tail 50 --follow --timestamps container
You can run the image in the foreground without the -d to see the output like when you run myapp.jar outside of Docker.
docker run my/image
So in this specific case:
docker run -it -p 9200:9200 --env-file /Users/myuser/myapp-local.env --name myapp myapp
If I am not mistaken, the issue you are experiencing is because you are using the shell form of ENTRYPOINT. Change it to use the exec version, as follows:
ENTRYPOINT ["java", "-Dspring.config=.", "-jar", "myapp.jar"]
The shell form will launch Java as a separate process just like a shell command. This causes PID 1 to return making Docker believe the container is finished. Using the exec form, the Java process replaces PID 1 and the container will continue running.
Is it possible to copy an existing WebSphere profile and run it on WebSphere in Docker?
I am doing some research on containerization, virtualization, etc. and am currently working with Docker. Getting WebSphere up and running on Docker is simple enough:
docker run --name wasserver -h wasserver -p 9043:9043 -p 9443:9443 -d ibmcom/websphere-traditional:install
What I'd like to do is use a profile from another WebSphere instance and run that on the Docker WebSphere. I have tried to do the following in an attempt to mount a directory that contains the profile in question, and to run same:
docker run -v /opt/WebSphere/WAS8_5/:/WASDIR --name myprofileserver -h myprofileserver -p 9043:9043 -p 9443:9443 -d ibmcom/websphere-traditional:install -e PROFILE_NAME=/WASDIR/profiles/myprofile1
The end result of this command is that the container is created, but does not run:
docker: Error response from daemon: oci runtime error: exec: "-e": executable file not found in $PATH
Perhaps there is a switch, setup, or other configuration I am missing here?
The last argument to docker run is the command you want to run inside the container (or the name of the image if you're running the default entrypoint / cmd). You just need to move your environment variable definition back in the command like this:
docker run -v /opt/WebSphere/WAS8_5/:/WASDIR --name myprofileserver -h myprofileserver -p 9043:9043 -p 9443:9443 -d -e PROFILE_NAME=/WASDIR/profiles/myprofile1 ibmcom/websphere-traditional:install