[英]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.