[英]How to specify environment variables in application.properties file so my application can use Docker container MySQL not local MySQL in Spring?
i'm new to Dockers,i want my spring application to access MySQL db from container but i don't know how to add environment variables in docker-compose file and application.properties file so my application can use MySQL db containing in Docker MySQL image.but make sure this is spring application not spring boot i'm new to Dockers,i want my spring application to access MySQL db from container but i don't know how to add environment variables in docker-compose file and application.properties file so my application can use MySQL db containing in Docker MySQL image.但请确保这是 spring 应用程序不是 spring 启动
below is the docker-compose file i tried to write下面是我试图写的 docker-compose 文件
version: '3'
services:
mysql-standalone:
image: 'mysql:5.7'
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_USER=root
- MYSQL_PASSWORD=root
- MYSQL_DATABASE=CTH
ports:
- "3307:3306"
cth-docker-container:
image: cth-docker-container
ports:
- "8082:8082"
environment:
CTH_MYSQL_DATABASENAME: CTH
CTH_MYSQL_SERVERNAME: mysql-standalone
CTH_MYSQL_USERNAME: root
CTH_MYSQL_PASSWORD: root
CTH_CHAT_MYSQL_DATABASENAME: CTH_CHAT
CTH_CHAT_MYSQL_SERVERNAME: mysql-standalone
CTH_CHAT_MYSQL_USERNAME: root
CTH_CHAT_MYSQL_PASSWORD: root
build:
context: "./"
dockerfile: "Dockerfile"
depends_on:
- mysql-standalone
and application.properties file which has db properties和具有 db 属性的 application.properties 文件
dbuser.local=root
dbpassword.local=root
dbdatabaseName.local=CTH
dbserverName.local=localhost
dbportNumber.local=3306
i don't know how to create environment variables so my application knows to use use MySQL db present inside MySQL-standalone image我不知道如何创建环境变量,所以我的应用程序知道使用 MySQL 独立映像中存在的 MySQL db
With docker-compose you create a container.使用 docker-compose 创建一个容器。
Inside your container there are:在您的容器内有:
if you want to connect to database from your Spring application you have to change your application.properties like this:如果您想从 Spring 应用程序连接到数据库,您必须像这样更改您的 application.properties:
(you can find properties in this site https://spring.io/guides/gs/accessing-data-mysql/ under "Create the application.properties File" section) (您可以在此站点https://spring.io/guides/gs/accessing-data-mysql/在“创建 application.properties 文件”部分下找到属性)
Finally, I found how to create environment variables in the application.properties file which can consume data from the docker-compose file and can able to give values when my application is running locally.最后,我发现了如何在 application.properties 文件中创建环境变量,它可以使用 docker-compose 文件中的数据,并且可以在我的应用程序在本地运行时给出值。
key.name=${value_consuming_from_docke_compose_file:i_can_give_value_when_application_is running_locally}
this is the way but anyone gets another way stuck in a different scenario please find the answer and post here.这是方式,但任何人都会以另一种方式陷入不同的情况,请在此处找到答案并发布。
Thanks!谢谢!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.