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
Related
When I run the docker compose, it returns the error
Caused by: java.net.ConnectException: Connection refused (Connection
refused)
I don't have much experience with docker.. what am i doing wrong?
My docker compose:
version: '3.5'
services:
mysql-service:
image: mysql:5.7
networks:
- phonebook-network
environment:
- MYSQL_ROOT_PASSWORD=admin
- MYSQL_USER=admin
- MYSQL_PASSWORD=admin
- MYSQL_DATABASE=PhoneBook
restart: on-failure
phonebook-service:
build:
context: ./
args:
JAR_FILE: ./Phonebook-0.0.1-SNAPSHOT.jar
ports:
- "8080:8080"
environment:
- DB_HOST=jdbc:mysql://mysql-service:3306/PhoneBook?useTimezone=true&serverTimezone=UTC&useSSL=false
- DB_USERNAME:admin
- DB_PASSWORD:admin
networks:
- phonebook-network
depends_on:
- mysql-service
restart: on-failure
networks:
phonebook-network:
driver: bridge
My dockerfile:
FROM openjdk:11-jdk-slim
VOLUME /phoneBook
ARG JAR_FILE
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-jar","/app.jar"]
First, ensure the ports are exposed in MySQL as well:
services:
mysql-service:
image: mysql:5.7
networks:
- phonebook-network
ports:
- "3306:3306"
environment:
- MYSQL_ROOT_PASSWORD=admin
- MYSQL_USER=admin
- MYSQL_PASSWORD=admin
- MYSQL_DATABASE=PhoneBook
restart: on-failure
the "depends_on" config dont resolve the problem about the order that the containers start?
Yes, the depends_on ensures that the MySQL container will start before the Java one, but it might happen that MySQL itself takes a long time to bootstrap, so the container is ready but the process inside is not.
If the problem still persists, this can be a possible solution: create a docker_entrypoint.sh file at the root of your project. This also assumes the container has netcat installed.
#!/usr/bin/env bash
echo "Waiting for MySQL..."
# I'm assuming you're running it in a default port (3306)
until nc -vz "mysql-service" 3306; do
>&2 echo "Waiting for MySQL to be available..."
sleep 0.1
done
echo "MySQL started"
java -jar /app.jar
then modify your Dockerfile:
FROM openjdk:11-jdk-slim
VOLUME /phoneBook
ARG JAR_FILE
RUN apt-get update --fix-missing \
&& apt-get install -y --no-install-recommends netcat \
&& rm -rf /var/lib/apt/lists/*
COPY ${JAR_FILE} app.jar
COPY ./docker_entrypoint.sh docker_entrypoint.sh
RUN chmod 100 docker_entrypoint.sh
ENTRYPOINT [ "./docker_entrypoint.sh" ]
I don't have a way to test this right now, but let me know if this works.
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
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.
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
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