问题描述
我是Docker的新手,目前正努力将我的dropwizard应用程序容器化。每次构建容器,运行容器并检查日志时,都会收到MySQL连接失败错误,这在容器在虚拟机上运行时是有意义的,对于localhost URL而言,它什么也没有。我想知道如何做才能使我的MySQL在docker容器中可访问。谢谢。这是我的config.yml文件看起来像rn的样子。
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" ```
解决方法
假设您有一个mysql容器,它暴露了端口3306
和dropwizard容器。
您可以为他们创建网络
docker network create <network_name>
并将其分配给码头工人
docker run .... --network <network_name>
这应该使机器人docker彼此见面
如果使用docker network ls
,则可以看到使用docker-compose
的网络,它将自动生成网络。
您还可以使用host
网络,这也使它们能够连接到计算机(如果在其中之一中运行docker时为虚拟机)中的端口。