Error when trying to run the docker compose - java

When I run the command docker-compose -f docker-compose.yml up my container starts normally.
In IntelliJ it appears the button to execute the container when the file docker-compose.yml is opened, When I try to upload the container directly through the * .yml file I get the error below:
Failed to deploy 'Compose: docker-compose': Sorry but parent: com.intellij.execution.impl.ConsoleViewImpl[,0,0,1188x368,invalid,layout=java.awt.BorderLayout,alignmentX=0.0,alignmentY=0.0,border=,flags=9,maximumSize=,minimumSize=,preferredSize=] has already been disposed (see the cause for stacktrace) so the child: com.intellij.util.Alarm#7566093f will never be disposed.
My docker-compose.yml file:
version: 3.4
services:
api.logistics-service:
container_name: logistics-service
build: ./docker
ports:
- "8080:8080"

I had the same problem. A wrong version in docker-compose.yaml caused the error on first startup.
After fixing this it, I was not able to start any docker-compose-services anymore.
Looks like an IntelliJ bug.
In this situation just restart IntelliJ.

Related

Spring/Hibernate app in docker doesn't remove columns

I am new to docker and it is easy to get confused about some things. Here is my question. I am working on my Spring Boot app and was creating entities for DB. I found out that when I remove a column from the entity after rebuilding the container (docker-compose up --build) this column isn't removed. But when I add a new column after rebuilding a container new column is created.
After that, I tried to remove all unused images and containers by running docker system prune. And surprisingly after once again running docker-compose up --build column was removed from db.
Is this expected or it can be changed somehow?
I'm gonna add my docker files. Maybe the problem is somewhere there.
Dockerfile:
FROM maven:3.5-jdk-11 AS build
COPY src /usr/src/app/src
COPY pom.xml /usr/src/app
RUN mvn -f /usr/src/app/pom.xml clean package -DskipTests
FROM adoptopenjdk:11-jre-hotspot
ARG JAR_FILE=*.jar
COPY --from=build /usr/src/app/target/restaurant-microservices.jar /usr/app/restaurant-microservices.jar
ENTRYPOINT ["java","-jar","/usr/app/restaurant-microservices.jar"]
docker-compose.yml:
version: '3'
services:
db:
image: 'postgres:13'
container_name: db
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=password
- POSTGRES_DB=restaurant
ports:
- "5432:5432"
app:
build: .
container_name: app
ports:
- "8080:8080"
depends_on:
- db
environment:
- SPRING_DATASOURCE_URL=jdbc:postgresql://db:5432/restaurant
- SPRING_DATASOURCE_USERNAME=postgres
- SPRING_DATASOURCE_PASSWORD=password
- SPRING_JPA_HIBERNATE_DDL_AUTO=update
Upd: I tried the same thing with controllers and it creates and removes
controllers without any issues
I found the problem. It is all about this line of code in docker-compose.yml(and application.properties)
SPRING_JPA_HIBERNATE_DDL_AUTO=update
The update operation for example, will attempt to add new columns, constraints, etc. but will never remove a column or constraint that may have existed previously but no longer does as part of the object model from a prior run (found here How does spring.jpa.hibernate.ddl-auto property exactly work in Spring?) So when I changed update to create-drop (not sure if it is optimal option) it started working as expected
oh, I also didn't know that. thank you for your problem solving.
It is something weird, because 'update' means something changed.

How to set up file docker-compose with mysql and tomcat in IDE Intelij?

I am working on a personal project using Jsp / servlet so it needs tomcat to run. I created a compose docker file for mysql and I want to have tomcat inside the file and how to set it up in the IDE instead of downloading it from tomcat.apache.org and setting it up into the Intelij IDE. Can you guys help me? Thank you very much!
version: '3'
services:
db_bookstore:
image: mysql:5.7
ports:
- 3210:3306
environment:
- MYSQL_ROOT_PASSWORD=654321
- MYSQL_DATABASE=bookstore
I assume you want to have tomcat service in your Docker, right?
version: '3'
services:
db_bookstore:
...
tomcat:
image: tomcat:9.0.12
ports:
- "80:8080"

facing connection refused error while trying to run test cases from one container which is hitting the zalenium container

When running my selenium testcases on Zalenium grid locally and it runs fine. I have created image of my project using the docker file below. i have written the docker compose file to start the zalenium grid . But when trying to run image of my project to create container and run test cases inside my container it is giving connection refused error.
Dockerfile of my project to create image is below
FROM openjdk:11.0.1-debian
VOLUME /tmp
ADD web_runnable.jar app.jar
ADD cacerts cacerts
ADD config.properties config
EXPOSE 8000
ENV USERNAME xyz
ENV PASSWORD xyz
ENV GRID true
ENTRYPOINT sh -c 'java -jar -Dconfig=config -DuserName=$USERNAME -Dpassword=$PASSWORD -Dgrid=$GRID -Djavax.net.ssl.trustStorePassword=changeit -Djavax.net.ssl.trustStore=cacerts /app.jar com.tsys.driverscript.DriverScript.class'
Docker compose file to start zalenium is below
version: '3.5'
services:
zalenium:
image: dosel/zalenium
container_name: zalenium_container
networks:
- main
restart: always
ports:
- "4444:4444"
command: ["start", "--desiredContainers", "1", "--maxDockerSeleniumContainers", "1","--sauceLabsEnabled","false","--screenWidth", "1280", "--screenHeight", "720" , --timeZone , "Asia/Kolkata", "--seleniumImageName","elgalu/selenium:latest"]
volumes:
- /var/run/docker.sock:/var/run/docker.sock
- /Users/nikitatorane/videos:/home/seluser/videos
privileged: true
selenium:
image: elgalu/selenium:latest
container_name: selenium_container
networks:
- main
restart: always
networks:
main:
name: main
I have setup my huburl as below :
NODE = "http://zalenium:4444/wd/hub";
When I'm trying to run image using with setting the same network on which zalenium is running then also i am getting connection refused error being all containers in same network.
Command to build image of my project is below :
docker build -t xyz:1.2
command to run my project image is below :
docker run -it --net=main xyz:1.2
error while running image is below :*
Caused by: java.net.ConnectException: Connection refused (Connection refused)
at java.base/java.net.PlainSocketImpl.socketConnect(Native Method)
at java.base/java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:399)
at java.base/java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:242)
at java.base/java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:224)
at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:403)
at java.base/java.net.Socket.connect(Socket.java:591)
at org.apache.http.conn.socket.PlainConnectionSocketFactory.connectSocket(PlainConnectionSocketFactory.java:74)
at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:141)
... 52 more
I even tried to link two containers and then also getting same error as above. Can anyone go through this and let me know where I am going wrong ?

Docker disable restart on-failure

The following situation:
I have a Spring Boot Application
which runs in a Docker swarm
but fails to start because it was not properly configured (a property is missing).
It seems to me that the docker swarm always tries to restart the container, but always fails because of the missing property.
The restart makes no sense because docker will never be able to start the application unless I fix the missing property.
So currently the swarm ends in an endless loop.
Regarding this problem I already read:
The docker documentation: https://docs.docker.com/config/containers/start-containers-automatically/
and several StackOverflow posts: https://stackoverflow.com/search?q=Docker+restart
My "setup":
The dockerfile:
ARG nexus_docker_registry=mynexus.com:10099
FROM ${nexus_docker_registry}/openjdk:8-jdk-alpine
ADD myjar.jar myjar.jar
ENV JAVA_OPTS=""
ENTRYPOINT [ "java", "-jar", "/myjar.jar" ]
my YML-file to create the docker service:
---
- hosts: docker_manager
become: false
vars:
servicename: 'myservice'
imageurl: "mynexus.com:10099/myjar:{{version}}"
extraoptions:
- "--with-registry-auth"
- "--detach=true"
- "--log-driver gelf"
- "--log-opt 'gelf-address=udp://{{ groups['logstash'][0] }}:10001'"
- "--hostname 'myhost.com'"
- "--mount 'type=bind,source=/etc/localtime,destination=/etc/localtime:ro'"
- "--mount 'type=volume,source=mykeys,destination=/mykeys'"
- "--env 'spring.profiles.active=docker'"
- "--publish 8000:6666"
tasks:
- name: Include vault
include_vars: "myvault.yml"
- name: "delete service '{{ servicename }}'"
command: sudo docker service rm "{{ servicename }}"
args:
warn: false
ignore_errors: true
run_once: true
- name: "create service {{ servicename }}"
command: sudo docker service create {{ extraoptions | join( ' ' ) }} --name "{{ servicename }}" "{{ imageurl }}"
args:
warn: false
run_once: true
What I want to achieve is:
If the spring boot application is not able to start because of for example a BeanCreationException or something similar, then I don't want the docker service to restart endlessly.
If I restart the swarm etc. the docker service should restart automatically.
In the docker documentation is written:
If you manually stop a container, its restart policy is ignored until the Docker daemon restarts or the container is manually restarted. This is another attempt to prevent a restart loop.
So I guess that what I want to achieve is not possible with a restart policy.
Questions:
but maybe I can write something in my Dockerfile that I achieve my goals?
Or am I totally wrong here and misinterpret the documentation?
I am unfortunately not a docker expert and still learning to handle 'the swarm'.
There are 4 different restart policies in Docker:
no - Do not automatically restart the container. (the default)
on-failure - Restart the container if it exits due to an error, which manifests as a non-zero exit code.
always - Always restart the container if it stops
unless-stopped - Similar to always, except that when the container is stopped (manually or otherwise), it is not restarted even after Docker daemon restarts.
There is no way for docker to "detect" a type of error from an application and restart or not depending on that.
One way to achieve this is to use supervisord within your container and let that handle the restart depending of a list of exit codes that you define. But this means that your container will only restart when supervisord crashes, not when you application does and you'll have to change your code to return different exit codes on the errors that should be restarting and the ones that shouldn't.
Because it does not seem possible what I wanted to achieve, I read the documentation again (https://docs.docker.com/engine/reference/commandline/service_create/) and found the option --restart-max-attempts which will solve my problem with the endless loop.
You may want to try and implement the creation of a docker stack based on a docker-compose file.
In this scenario, as the compose v3 documentation indicates, you have full control over the service restart policy.
The next example won't allow restart:
version: "3.9"
services:
python:
image: my_user/my_repo:my_container
volumes:
- /home/python:/home
deploy:
restart_policy:
condition: none
You can adjust the restart_policy block with condition: [none | on-failure | any] and with max_attempts: [your_int]

Creating docker container for java jersey app

I've been stuck on this for a good bit now and can't find the solution anywhere. I'm writing a java rest service using jersey framework, maven as a package manager hosted on a apache tomcat.
The project works perfectly fine locally. I want to dockerize the application and really struggling. I have the tomcat container up and running and when I go to the root of my application I can see the simple hello text I have. So when I go to http://xxx:8888/npmanager/ at this point I'm seeing what I expect.
Now when I try to hit any of my endpoints i.e https://xxx:8888/npmanager/api/XXX I get a 500 error:
warnings have been detected with resource and/or provider classes:
SEVERE: Missing dependency for field: private org.glassfish.jersey.server.wadl.WadlApplicationContext org.glassfish.jersey.server.wadl.internal.WadlResource.wadlContext
Dockefile:
FROM tomcat:8.5.38
ADD ./target/npmanager.war /usr/local/tomcat/webapps/
CMD chmod +x /usr/local/tomcat/bin/catalina.sh
CMD ["catalina.sh", "run"]
docker-compose.yml
version: '3'
services:
tomcat-dev:
build: .
environment:
TOMCAT_USERNAME: root
TOMCAT_PASSWORD: root
ports:
- "8888:8080"
mysql-dev:
image: mysql:8.0.2
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: npmanager
volumes:
- /mysql-data:/var/lib/mysql
ports:
- "3308:3306"

Categories