Change base uri Tomcat Application on Docker (docker compose) - java

I developed a Java EE application on Eclipse for a school project and try to use it in localhost through Tomcat, all using Docker.
My problem is the url, I would like that when I launch my application with Docker, I can access it at http://localhost:8080. Currently, I have to go to the link http://localhost:8080/Epitech_Dashboard/.
Would it be possible to have access to the application directly on http://localhost:8080?
Ps: I saw a similar question on slack but it does not solve my problem because she does not handle the problem with docker-compose.
Here's my Dockerfile and my docker-compose.yml
Dockerfile:
FROM java:8
EXPOSE 8080
ADD /#.war #.war
ENTRYPOINT ["java", "-jar", "#.war"]
docker-compose.yml
version: '2'
services:
web:
image: tomcat:7
environment:
JDBC_URL: jdbc:mysql://db:3306/example_db?connectTimeout=0&socketTimeout=0&autoReconnect=true
JDBC_USER: example_db_user
JDBC_PASS: example_db_pass
ports:
- '8080:8080'
volumes:
- ./Epitech_Dashboard.war:/usr/local/tomcat/webapps/Epitech_Dashboard.war
links:
- db
db:
image: mysql:latest
hostname: db
environment:
MYSQL_ROOT_PASSWORD: nimda
MYSQL_DATABASE: example_db
MYSQL_USER: example_db_user
MYSQL_PASSWORD: example_db_pass
volumes:
- ./db:/docker-entrypoint-initdb.d
Thank you in advance.

Related

Docker compose connection between java and mysql container not possible

I have an java spring / hibernate application and I cant create a connection beween the mysql and java containers. When I start just the mysql Container and run the java application manualy in CMD it works fine. But once the java application runs in a container I get the following problem:
bookAPI | com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
bookAPI |
bookAPI | The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
bookAPI | at com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:174) ~[mysql-connector-java-8.0.21.jar!/:8.0.21]
bookAPI | at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64) ~[mysql-connector-java-8.0.21.jar!/:8.0.21]
If I understand the error correctly the java container can send to the mysql container but not the other way around. I have linked the java container to the mysql container. Anyone knows how to connect these containers correctly?
My docker-compose.yml looks like this:
version: '3'
services:
db:
image: mysql:5.7
container_name: mysql
environment:
MYSQL_ROOT_PASSWORD: aaaaaa
MYSQL_DATABASE: bookAPI
MYSQL_USER: aaaaaa
MYSQL_PASSWORD: aaaaaa
ports:
- 3306:3306
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: phpmyadmin
links:
- db
environment:
PMA_HOST: db
PMA_PORT: 3306
PMA_ARBITRARY: 1
restart: always
ports:
- 8081:80
java:
container_name: bookAPI
image: openjdk:8
depends_on:
- db
volumes:
- ./:/app
working_dir: /app
command: bash -c "cd /app && java -jar bookAPI.jar"
stdin_open: true
tty: true
ports:
- 8080:8080
links:
- db
You can also directly override the applications.properties with docker-compose environment variables like this :
java:
container_name: bookAPI
image: openjdk:8
volumes:
- ./:/app
working_dir: /app
command: bash -c "cd /app && java -jar bookAPI.jar"
stdin_open: true
tty: true
environment:
spring.datasource.url="jdbc:mysql://db:3306/bookAPI""
links:
- db
ports:
- 8080:8080
I would recomment you tu use depends_on db instead of links
depends_on:
- db
Okay, I solved the problem. To connect the spring application with another docker container you need to change the application.properties url value from localhost or ip to the docker maysql container name. Thats tricky while running it localy or run maven install so the best pracice is to overwrite it while starting the application in container.
java:
container_name: bookAPI
image: openjdk:8
volumes:
- ./:/app
working_dir: /app
command: bash -c "cd /app && java -jar bookAPI.jar --spring.datasource.url="jdbc:mysql://db:3306/bookAPI""
stdin_open: true
tty: true
links:
- db
ports:
- 8080:8080

Jar not being found when running tomcat war in Docker

I've made a war to run on my tomcat server in a Docker container. The servlet runs fine in eclipse, but when I have compiled it put it in my Docker it isn't loading in the required classes properly.
Project structure and manifest file
When I try and access the servlet using my browser I get this error.
Error in browser
I went into the Docker container and checked that gurobi.jar is still in WEB-INF/lib/ and it is.
I don't know why this wouldn't be able to find the jar, as it works fine when it's in Eclipse.
docker-compose.yml
version: '3'
services:
db:
image: mysql:5.7
ports:
- '3306:3306'
environment:
MYSQL_DATABASE: 'projectdb'
MYSQL_USER: ''
MYSQL_PASSWORD: ''
MYSQL_ROOT_PASSWORD: ''
web:
build:
context: .
dockerfile: Dockerfile
command: python manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
- /var/run/docker.sock:/var/run/docker.sock
ports:
- "8000:8000"
depends_on:
- db
tomcat:
image: tomcat
ports:
- "8080:8080"
volumes:
- ./GurServ.war:/usr/local/tomcat/webapps/GurServ.war
java:
image: bitnami/java:latest
volumes:
- .:/app
- .:/code
Dockerfile
FROM python:3
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
COPY requirements.txt /code/
RUN pip install -r requirements.txt
COPY . /code/
EXPOSE 8000
CMD exec gunicorn djangoapp.wsgi:application - bind 0.0.0.0:8000 - workers 3
Project structure
...
Java Resources
src
...
WebContent
META-INF
MANIFEST.MF
WEB-INF
gurobi-javadoc.jar
gurobi.jar

Docker unable to access jar file

The docker container is not able to access the jar file, that is being accessed over the mount point /my/project/dir.
I am certain it is not a permission issue, because I changed the access rights locally, so it should be able to read/write/execute it.
This is the Dockerfile:
FROM tomcat:9-jre8
RUN apt-get update && apt-get install librrds-perl rrdtool -y
VOLUME ["/data/rrdtool", "/my/project/dir"]
ENTRYPOINT [ "java","-jar","/my/project/dir/build/libs/spring-project-0.1.0.jar" ]
And this is the docker-compose.yml file:
version: '2'
services:
db:
container_name: db1
image: mysql:8
restart: always
environment:
MYSQL_ROOT_PASSWORD: password123
MYSQL_USER: user123
MYSQL_PASSWORD: pasw
MYSQL_DATABASE: mydb
expose:
- "3307"
db2:
container_name: db2
image: mysql:8
restart: always
environment:
MYSQL_ROOT_PASSWORD: password123
MYSQL_USER: user123
MYSQL_PASSWORD: pasw
MYSQL_DATABASE: mydb2
expose:
- "3308"
spring:
container_name: spring-boot-project
build:
context: ./
dockerfile: Dockerfile
links:
- db:db1
- db2:db2
depends_on:
- db
- db2
expose:
- "8081"
ports:
- "8081:8081"
restart: always
This is the output from docker-compose logs spring:
Error: Unable to access jarfile /my/project/dir/build/libs/spring-project-0.1.0.jar
I don't see you copying the jar into the container anywhere. You should try moving a VOLUME declaration from Dockerfile to the compose file into the spring service like this:
volumes:
- /my/project/dir:/app
And then inside Dockerfile you should point to the dir:
ENTRYPOINT [ "java","-jar","/app/build/libs/spring-project-0.1.0.jar" ]
Later on if you'd like to deploy it (for example) you should copy the project files directly into the image instead of utilizing the volumes approach. So in Dockerfile you'd then do:
COPY . /app
instead of VOLUME [..]
Putting it all together:
development:
Dockerfile:
FROM tomcat:9-jre8
RUN apt-get update && apt-get install librrds-perl rrdtool -y
ENTRYPOINT [ "java","-jar","/app/build/libs/spring-project-0.1.0.jar" ]
compose-file:
version: '2'
services:
[..]
spring:
container_name: spring-boot-project
build: .
links:
- db:db1
- db2:db2
depends_on:
- db
- db2
ports:
- "8081:8081"
restart: always
volumes:
- /my/project/dir:/app
deployment:
Dockerfile (that is placed inside project's folder, docker build requires it's build context to be in a current directory):
FROM tomcat:9-jre8
RUN apt-get update && apt-get install librrds-perl rrdtool -y
COPY . /app
ENTRYPOINT [ "java","-jar","/app/build/libs/spring-project-0.1.0.jar" ]
compose-file:
version: '2'
services:
[..]
spring:
container_name: spring-boot-project
build: .
links:
- db:db1
- db2:db2
depends_on:
- db
- db2
expose:
- "8081"
If you are using Spring-Boot Project with Maven build. Try with below
Dockerfile.
FROM maven:3.8.4-openjdk-17 as maven-builder
COPY src /app/src
COPY pom.xml /app
RUN mvn -f /app/pom.xml clean package -DskipTests
FROM openjdk:17-alpine
COPY --from=maven-builder app/target/dockube-spring-boot.jar /app-service/dockube-spring-boot.jar
WORKDIR /app-service
EXPOSE 8080
ENTRYPOINT ["java","-jar","dockube-spring-boot.jar"]
dockube-spring-boot.jar // replace with your generated jar name
Here is the Sample Code Available

Docker depends_on Order Not Working

I'm trying to a selenium hub, chrome node, and firefox node, and my code that runs the test execution script in that order. I have the nodes depending on the hub and the code depending on both hubs. However, running docker-compose --build builds the code first and tries to run without starting the selenium components. I am unsure what I am doing wrong.
docker-compose.yml
version: '3'
services:
hub:
image: selenium/hub
networks:
robottestsnw: {}
ports:
- 4444:4444
chrome:
image: selenium/node-chrome
networks:
robottestsnw: {}
depends_on:
- hub
shm_size: '2g'
environment:
SCREEN_WIDTH: 1920
SCREEN_HEIGHT: 1080
HUB_HOST: hub
firefox:
image: selenium/node-firefox
networks:
robottestsnw: {}
depends_on:
- hub
shm_size: '2g'
environment:
HUB_PORT_4444_TCP_ADDR: hub
SCREEN_WIDTH: 1920
SCREEN_HEIGHT: 1080
HUB_HOST: hub
robottests:
build: .
networks:
robottestsnw: {}
depends_on:
- chrome
- firefox
networks:
robottestsnw:
driver: bridge
Dockerfile
# Dockerfile
# Base image
FROM sgrio/java-oracle
RUN apt-get update
RUN apt-get install -y maven
# Copy test project files to the image folder
COPY . /frontend-integration-tests
# Make the folder as a working directory
WORKDIR /frontend-integration-tests
# Install the test project libraries
RUN mvn package
CMD ["java", "-cp", "target/automated-testing 0.0.1-SNAPSHOT.jar", "automated-testing.service.App"]
docker-compose up
It starts services in dependency order, about docker-compose --build up don't sure. And maybe your port, network name are defined wrong:
hub:
image: selenium/hub
networks:
- robottestsnw
ports:
- 4444:4444

Docker container for application binaries only

I'd like to have each app release stored as some kind of container and then have several "server" only containers(DB, web server). How can the cooperation between those containers work?
I can imagine to define some volume in "app" container where the app. binaries will be stored and then use this volume folder as web server's deploy directory in the "server" container.
How will the process to update the application version work? How can I "bind" multiple binaries with one "server" container?
More generally I can imagine to deploy like this
"deploy some-version-releasecontainer to serv1, serv2, serv3", maybe docker is not the proper tooling and I will need some more abstract management like Swarm, kubernate etc. But the main point is to create application binary as selfstanding, read-only entity known to the eco-system.
Maybe you need a docker-compose to make your containers interact on a virtual network (but you can still share resources and volumes). I'll post a simple example here.
The docker-compose is the following:
version: '2'
services:
myapp_service1:
image: myapp_image1:latest
networks:
mynetwork:
aliases:
- myalias1
depends_on:
- mysql
expose:
- 8080
volumes:
- /opt/myapp/logs:/jboss-as-7.1.1.Final/logs
environment:
- "JAVA_OPTS=-Xms64m -Xmx128m -XX:MaxPermSize=128m -Djava.net.preferIPv4Stack=true"
- "db=mysql.myapp"
volumes_from:
- myapp_volume
myapp_service2:
image: myapp_image2:latest
networks:
mynetwork:
aliases:
- myalias2
depends_on:
- mysql
expose:
- 8080
volumes:
- /opt/myapp/logs:/jboss-as-7.1.1.Final/logs
environment:
- "JAVA_OPTS=-Xms64m -Xmx128m -XX:MaxPermSize=128m -Djava.net.preferIPv4Stack=true"
- "db=mysql.myapp"
volumes_from:
- myapp_volume
myapp_volume:
container_name: myapp_volume
image: myapp_volume_image:latest
mysql:
image: mysql
networks:
mynetwork:
aliases:
- mysql.myapp
expose:
- 3306
ports:
- "13306:3306"
volumes:
- /opt/myapp/mysql:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: root
networks:
mynetwork:
driver: bridge
Here we have four containers. The first two are sample applications sharing the same volume. The volume is defined just below the applications and in the end we have a container which runs a simple MySql DB.
The Dockerfile for the shared volume container is that one:
FROM alpine
VOLUME /jboss-as-7.1.1.Final/mydir/config
CMD /bin/sh -c "while true; do sleep 1; done"
The docker compose is a yml file, let's say my-docker-compose.yml. To run it you have to type this in the terminal:
docker-compose -f path/to/compose/my-docker-compose.yml up -d
Of course, the images should already be built.

Categories

Resources