简体   繁体   English

如何使用 docker 容器内我本地机器上运行的 MySQL 服务器用于 DropWizard 应用程序?

[英]How to use the MySQL server running on my local machine inside the docker container for a DropWizard application?

I'm new to Docker and am currently struggling with containerizing my dropwizard application.我是 Docker 的新手,目前正在努力将我的 dropwizard 应用程序容器化。 Each time I build the container, run it, and check the logs, I get the MySQL connection failure error which makes sense as the container runs on a virtual machine and for it the localhost URL means nothing.每次我构建容器、运行它并检查日志时,我都会收到 MySQL 连接失败错误,这是有意义的,因为容器在虚拟机上运行,本地主机 URL 对它来说毫无意义。 I was wondering what can I do to make my MySQL accessible inside my docker container.我想知道我该怎么做才能使我的 MySQL 在我的 docker 容器中可访问。 Thanks.谢谢。 This is how my config.yml file looks like rn.这就是我的 config.yml 文件的样子。

  driverClass: com.mysql.cj.jdbc.Driver
  # the username
  user: root

  # the password
  password:

  # the JDBC URL
  url: jdbc:mysql://localhost:3306/locations?useLegacyDatetimeCode=false&serverTimezone=UTC

  # any properties specific to your JDBC driver:
  properties:
    charSet: UTF-8

  # the maximum amount of time to wait on an empty pool before throwing an exception
  maxWaitForConnection: 1s

  # the SQL query to run when validating a connection's liveness
  validationQuery: "/* MyService Health Check */ SELECT 1"

  # the timeout before a connection validation queries fail
  validationQueryTimeout: 3s

  # the minimum number of connections to keep open
  minSize: 8

  # the maximum number of connections to keep open
  maxSize: 32

  # whether or not idle connections should be validated
  checkConnectionWhileIdle: false

  # the amount of time to sleep between runs of the idle connection validation, abandoned cleaner and idle pool resizing
  evictionInterval: 10s

  # the minimum amount of time an connection must sit idle in the pool before it is eligible for eviction
  minIdleTime: 1 minute

  # Logging settings.
#logging:
#  level: INFO
#  loggers:
#    io.dropwizard: DEBUG
#    org.eclipse.jetty.servlets: DEBUG
#    org.hibernate.SQL: ALL
#    com.udemy.LocationsApplication:
#      level: ALL,
#      additive: false
#      appenders:
#        - type: conso
#          logFormat: "%red(CDR) [%magenta(%date)] [%thread] [%cyan(%logger{0})]: %message%n"
#  appenders:
#    - type: console
#      logFormat: "%highlight(%-5level) [%magenta(%date)] [%thread] [%cyan(%logger{0})]: %message%n" ```

Assuming you have a mysql container exposing the port 3306 and your dropwizard container.假设你有一个 mysql 容器暴露端口3306和你的 dropwizard 容器。

You can create a.network for them你可以为他们创建一个.network

docker network create <network_name>

And assign it to the dockers并将其分配给码头工人

docker run .... --network <network_name>

This should make that bot dockers see each other这应该让机器人码头工人看到对方

You can see the.networks you have using docker.network ls if you use docker-compose it will generate the.networks automatically.如果您使用docker-compose ,您可以使用docker.network ls查看您拥有的.networks,它将自动生成.networks。

You can also use the host .network which also makes them able to connect to ports in your machine (the virtual machine if you are running docker in one of those)您还可以使用host .network,这也使它们能够连接到您机器中的端口(如果您在其中一个中运行 docker,则为虚拟机)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从 docker 容器内运行的应用程序连接到本地运行的数据库 - Connect to database running in local from application running inside docker container 如何从另一台机器连接到在 docker 容器内运行的服务器套接字? - How can I connect to server socket running inside a docker container from another machine? 如何将我的本地 java 应用程序连接到在服务器上的容器中运行的 cassandra - How do I connect my local java application to cassandra running in a container on a server 如何:运行 java jar 的 Docker 容器连接到主机 mysql 服务器? - How to: Docker container running java jar connecting to host mysql server? 无法从本地计算机连接到在服务器上运行的mysql docker(错误10061)-Workbench - Cannot connect from local machine to mysql docker running on server (error 10061) - Workbench 如何向在 docker 容器中运行的服务器发送 HTTP 请求? - How to send HTTP requests to my server running in a docker container? 应用服务器已经在docker容器中运行,无法使用postgresql dirver.Connection denied - The application server has been running in docker container could not use postgresql dirver.Connection refused 无法连接到docker容器内运行的嵌入式服务器 - Can't connect to embedded undertow server running inside docker container 在本地机器上运行的Web应用程序 - web application running in local machine 将 SQL 开发人员与在 EC2 中的 Docker 容器上运行的 MYSQL 服务器连接 - Connect SQL developer with MYSQL server running on Docker container in EC2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM