-
Notifications
You must be signed in to change notification settings - Fork 5.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[BUG] Docker compose does not interpret the .env inside the Spring Boot project. #12095
Comments
If I don't see a solution, I'll have to migrate to Kubernetes. What I think will be better than docker-compose.yml |
Up to you to migrate to k8s and see if it is "better" (you'll discover a whole new universe of random issues :P) About your issue, you should access your database using service name |
db_mysql = service name My understanding is that it is the name of the container, I tried both. Neither works. This isn't about discovering a new world. If I have to pay to do some tests... I'm not happy about it. Having to go looking for hosts with free Kubernetes... instead of things working normally... What do you want me to say? |
It seems that the solution is this:
In the docker console it appears:
Could someone explain to me why? Is there some incompatibility with Docker and jdbc metadata? I think it would be better without putting the
I think it would be more appropriate without being tied to any container, rather you would be tied to the domain. ㅤ |
this is not |
My understanding is that when you define a service in Docker Compose, Docker creates a default network for your application and each container joins this network with the service name. Therefore, other containers can access this service using its service name / container as hostname. https://docs.docker.com/compose/networking/ Docker networkingEach container can now look up the service name web or db and get back the appropriate container's IP address. For example, web's application code could connect to the URL postgres://db:5432 and start using the Postgres database._It is important to note the distinction between HOST_PORT and CONTAINER_PORT. In the above example, for db, the HOST_PORT is 8001 and the container port is 5432 (postgres default). Networked service-to-service communication uses the CONTAINER_PORT. When HOST_PORT is defined, the service is accessible outside the swarm as well. _Within the web container, your connection string to db would look like postgres://db:5432, and from the host machine, the connection string would look like postgres://{DOCKER_IP}:8001 for example postgres://localhost:8001 if your container is running locally. In Docker Compose, I have defined the service as db_mysql, and within that service, you specify that the container will be called c_db_mysql. For inter-container communication, you would use the service name (db_mysql), but in my case, since I have specified a container name (c_db_mysql), I can use that name as well. Both ways are correct. If you want an approximation, we can say that it is much better to use the service name. ㅤ This last way, without being tied to an image, seems less monolithic to me. By the way, do you know anything about what I told you about jdbc metadata and Docker that variables arrive empty and because of that having to disable metadata? |
Sorry, I don't get what you're asking for. Your application should access database relying on service name |
You don't understand? I just read it now. I'll explain it simply. Method A - If you run spring boot locally (the start button) everything works fine. If you don't understand this, I don't know what else to say. Water is a transparent, drinkable fluid. I couldn't explain it any more. I switched from docker compose to Kubernetes. |
Please stay cool here, I'm here to help you not to receive lessons.
How do you tell docker to use those .env files? Are those copied inside container by your Dockerfile instructions? In you example compose.yaml there's no reference to any .env file that would help me understand your issue |
|
Your description also marche alcohol which isn't strictly the same. Explaining things is hard, please relax and try again |
In spring boot, the .env files are usually placed in the resources folder. There is no need to tell it where the resources come from, you just have to add: That is, Spring Boot is smart enough to go and look up the properties on its own. You don't need to specify anything in the dockerfile. That works fine if deployed locally with VSC, Intellij, Eclipse... A - Running it normally - IDE: B - Running it as a container from an image.
I said "water" not "alcohol". |
please double check this Also might be usefull to compare your application with https://medium.com/thefreshwrites/the-way-of-dockerize-a-spring-boot-and-mysql-application-with-docker-compose-de2fc03c6a42, especially use of |
Sorry. I don't understand anything you said. 1 - I don't have to include any file in docker. Can you imagine having to put the .java or .class? I can't. Spring boot is smart enough to go and fetch the .env files. Nothing needs to be included. Normally you can pass the .env from dockerfile or from the orchestrator itself, whether it's Kubernetes or Docker Compose. I don't need it since I pass them individually per project. I have 20 images (microservices) and it's estimated that there will be more than 100, and if I have to put the .env of each project in a "docker compose" this is going to be too tedious. I don't use docker compose, so if I just start the image in a container the same error occurs. As I see that this will continue to be the same, remember that for the next people who have the problem, tell them to put: Remember this is a needle in a haystack. If you want to close the post, you can close it. But remember the solution at least. |
Seems to demonstrate a packaging issue or something wrong in runtime configuration. Your application container should be able to fully access the db container, the "network" setby composé is just a bridge network that doesn't bring any type of constraints About the workaround you're using, this is obviously not the desired state. I can't see any reason you get such an issue. |
I'm just passing by here to quickly note down this step of docker and kubernetes as a reminder. I'll also quickly answer your question.
With the property that I already showed in my first post. Reminder:
Note: You will always see a correct "deprecation" message.
|
I can't see any reason you get this behavior. Could you setup a minimal spring boot application to demonstrate this issue ? |
Closing as reporter didn't provided a reproducible scenario so we can investigate |
Description
When everything finally works fine locally, what I do is generate the jars and run docker compose.
After compiling the spring boot .jars and doing
Spring Boot and Docker seem to for some reason not correctly render the spring boot .env and the Spring Boot variables do not arrive correctly.
This is my mysql image in my docker-compose, for mysql and a microservice called profile.
The .env in my docker-compose-dev.yml is working fine, it's the Spring Boot one that's not working.
Let's go to the Spring Boot profile project.
application.properties:
Spring Boot .env int /resources/
This works correctly if I run the project without being launched as an image:
But if I launch it from the image with
I get this error from console docker container c_db_mysql:
java.lang.NullPointerException: Cannot invoke "org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(java.sql.SQLException, String)" because the return value of "org .hibernate.resource.transaction.backend.jdbc.internal.JdbcIsolationDelegate.sqlExceptionHelper()" is null
It seems that somehow docker is not correctly interpreting the .env that works locally. Why?
1 - If I run the image in docker and run the standalone project with: SPRING_DATASOURCE_URL=jdbc:mysql://localhost:3306/.... WORKS
2 - If I run the entire docker-compose-dev.yml, the .env variables don't seem to be read by docker.
Steps To Reproduce
1 - Run jars
2 - docker compose -f docker-compose-dev.yml up --build -d
3 - Error trying to access the database, variables arrive as null.
Compose Version
Docker Environment
Anything else?
No response
The text was updated successfully, but these errors were encountered: