Problem: 2 services, both running in separate docker containers
Now I create a custom docker network my-custom-network
with driver: bridge
and connect both services to it.
Intention:
Also I set spring.jpa.hibernate.ddl-auto=update
so that when the api service is started and it connects with the DB first time it automatically creates the table. But this doesn't happen.
What happens is this:
HostNotFoundException: mysqldb not known
. This is probably because mysql container is running on my custom network but while building the image docker doesn't know which network to use (I am not sure of this. So it fails.Workaround that I did
spring.jpa.hibernate.ddl-auto
to none
so that while building the api image, docker does not try to contact the mysql service.my-custom-network
and the container is up successfully.spring.jpa.hibernate.ddl-auto
is set to none
.What I want
I want some way that tables etc are created automatically and the docker image also builds successfully. But I don't know how to do it and hence this question.
Building an image and running a container are 2 very different things.
At build time (when you build an image based on a Dockerfile for example) there is no network setup to be done because there is nothing running. Only Docker building an image. So when you build your java image, only create the jar and that's it.
For running the containers I suggest using docker-compose. It makes your life a lot easier. Your java container should use something like a wait-for.sh
script to wait for the database to become available. There are tons of examples on internet for such scripts. If you can't find such a script then for now just manually start the database container and wait for it to finish starting and then start the other container.
The main point is: the java application creates the data model at runtime (when the container starts) and not at build time (when the image is created).