简体   繁体   English

如何在 application.properties 文件中指定环境变量,以便我的应用程序可以在 Z38008DCCA73AZ 中使用 Docker 容器 MySQL 不是本地 MySQL?

[英]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:在您的容器内有:

  • database= CTH数据库= CTH
  • username = root用户名 = 根
  • password = root密码=根
  • port = 3307(port expose outside):3306(port expose inside container) port = 3307(端口暴露在外面):3306(端口暴露在容器内)

if you want to connect to database from your Spring application you have to change your application.properties like this:如果您想从 Spring 应用程序连接到数据库,您必须像这样更改您的 application.properties:

  • spring.datasource.url=jdbc:mysql://localhost:3307/CTH spring.datasource.url=jdbc:mysql://localhost:3307/CTH
  • spring.datasource.username=root spring.datasource.username=root
  • spring.datasource.password=root spring.datasource.password=root
  • spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.driver-class-name=com.mysql.jdbc.Driver

(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.

相关问题 如何在 spring application.properties 中指定我的 ssl 密钥库文件的本地文件路径? - How do I specify local file path to my ssl keystore file in spring application.properties? spring-从application.properties文件内部读取环境变量 - spring - read environment variables from inside the application.properties file 使用环境变量覆盖 application.properties 中的变量的 Spring 奇怪行为 - Spring weird behavior for overriding variables in application.properties with environment variables 替换application.properties以外的Spring属性文件中的环境变量 - Replace environment variables in Spring properties file other than application.properties spring-boot application.properties中的环境变量错误 - Environment Variables in spring-boot application.properties error 将环境变量添加到 Spring 的 application.properties - Adding environment variables to Spring's application.properties Spring Boot:如何在 application.properties 中指定带有破折号的环境变量? - Spring Boot: How do you specify an environment variable that has dashes in the application.properties? 如何在Spring Boot中的application.properties中指定外部属性文件? - How to specify external properties files in application.properties in Spring Boot? 如何在Spring安全上下文配置xml文件中使用application.properties中的布尔参数? - How can I use boolean parameters from application.properties in spring security context configuration xml file? 如何更改我的application.properties文件? - How I can change my application.properties file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM