Docker container accessing external MySQL host

I have setup a spring-boot docker container in an EC2 instance(EC2_IP) and I have a MySQL hosted in a different VM. I verified that that the mysql host(MYSQL_IP) is accessible from the EC2 instance hosting the docker container.

I am passing the spring.datasource.url parameters using the docker environment variables during docker run.

The spring boot app fails complaining with an error message access denied to user db_user@EC2_IP. This is the part I am unable to understand and fix. I don’t understand why its trying to connect to the EC2_IP instead of db_user@MYSQL_IP.

I did a docker inspect and I verified that the environment variable for spring.datasoure.url is passed correctly and it is db_user@MYSQL_IP:3306.