I cannot get my spring-boot server to run against my database using docker.
If I start up my mysql database (called shape-shop-db-container) and intialize the database like so :
docker run -d -p 3306:3306 --name=shape-shop-db-container --env="MYSQL_ROOT_PASSWORD=root" --env="MYSQL_PASSWORD=root" --env="MYSQL_DATABASE=shapeshop" mysql
docker exec -i shape-shop-db-container mysql -uroot -proot shapeshop < SCHEMA.sql
docker exec -i shape-shop-db-container mysql -uroot -proot shapeshop < TEST_DATA.sql
and then run my application server within my IDE with the following application.properties :
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/shapeshop?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC&useLegacyDatetimeCode=false
spring.jpa.database-platform=org.hibernate.dialect.MySQL8Dialect
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.platform=mysql
spring.datasource.initialization-mode=always
server.port=8080
everything works fine.
Now
.... instead of running my application server through my IDE, I instead have it running on a docker container, and I try the following :
(1) create network 'shape-shop-network'
docker network create shape-shop-network
(2) run db container as before, but this time specifying the network as well.
docker run -d -p 3306:3306 --name=shape-shop-db-container --network shape-shop-network --env="MYSQL_ROOT_PASSWORD=root" --env="MYSQL_PASSWORD=root" --env="MYSQL_DATABASE=shapeshop" mysql
docker exec -i shape-shop-db-container mysql -uroot -proot shapeshop < SCHEMA.sql
docker exec -i shape-shop-db-container mysql -uroot -proot shapeshop < TEST_DATA.sql
(3) now i build my app server and run it on a container called 'shape-shop-server'.
docker build -t shapeshop:1.0 .
docker run --name shape-shop-server -p 8080:8080 shapeshop:1.0 --name shape-shop-server --network shape-shop-network
But when I run it in the container, I get "unable to acquire JDBC Connection" error.
Caused by: org.springframework.dao.DataAccessResourceFailureException: Unable to acquire JDBC Connection; nested exception is org.hibernate.exception.JDBCConnectionException: Unable to acquire JDBC Connection
2022-11-03T11:34:10.043594500Z at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:275) ~[spring-orm-5.1.8.RELEASE.jar:5.1.8.RELEASE]
2022-11-03T11:34:10.043602700Z at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:253) ~[spring-orm-5.1.8.RELEASE.jar:5.1.8.RELEASE]
2022-11-03T11:34:10.043610900Z
Caused by: java.net.ConnectException: Connection refused (Connection refused)
at java.base/java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:na]
at java.base/java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:412) ~[na:na]
at java.base/java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:255) ~[na:na]
at java.base/java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:237) ~[na:na]
at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:na]
Why is this happening? Is something 'off' with my ports? Why would it work in my IDE but not as a container? I would assume that the settings are the same.
UPDATE:
I tried to rename "localhost" in my application.properties to shape-shop-db-conatiner. Eg :
spring.datasource.url=jdbc:mysql://shape-shop-db-container:3306/shapeshop?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC&useLegacyDatetimeCode=false
But I get "Caused by: java.net.UnknownHostException: shape-shop-db-container" , even though shape-shop-db-container is running.
CodePudding user response:
The hostname for the docker container is not the same as "name".
You should apply the --hostname parameter when starting the docker container to specificy the hostname it would be given. You can then use that to connect to it from another container in the same network.
CodePudding user response:
When you want to establish a connection with another Docker container within a network, its hostname should be used instead of its name. By default, the container’s hostname is set to the container’s ID, which can be overridden using the --hostname
flag with docker run
command.
Another option would be to use the --network-alias
flag, also with docker run
command, to specify network-scoped alias for a container, which can be used as an alternative to a hostname. Likewise, the alias can be specified with the --alias
flag when connecting to an existing network using docker network connect
command.
Consider using docker-compose
where you don't have to worry about network and hostnames.
For reference: Container Networking and Docker run reference