春季启动mysql docker windows 7

Posted

技术标签:

【中文标题】春季启动mysql docker windows 7【英文标题】:springboot mysql docker windows7 【发布时间】:2018-10-28 06:51:33 【问题描述】:

操作系统-Windows 7

Docker 版本:Docker 版本 18.03.0-ce,构建 0520e24302

Springboot 版本:1.5.3.RELEASE

我从 https://github.com/TechPrimers/docker-mysql-spring-boot-example 获取代码库(这是 MacOS 中的工作示例代码。)

我的 application.properties 如下所示

# DataSource settings: set here your own configurations for the database
# connection. In this example we have "netgloo_blog" as database name and
# "root" as username and password.
spring.datasource.url = jdbc:mysql://mysql-standalone:3306/test
spring.datasource.username = testuser
spring.datasource.password = testuserpass

# Keep the connection alive if idle for a long time (needed in production)
spring.datasource.testWhileIdle = true
spring.datasource.validationQuery = SELECT 1

# Show or not log for each sql query
spring.jpa.show-sql = true

# Hibernate ddl auto (create, create-drop, update)
spring.jpa.hibernate.ddl-auto = create

# Naming strategy
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy

# Use spring.jpa.properties.* for Hibernate native properties (the prefix is
# stripped before adding them to the entity manager)

# The SQL dialect makes Hibernate generate better SQL for the chosen database
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

server.port=8086

Dockerfile 如下所示

FROM openjdk:8
ADD target/users-mysql.jar users-mysql.jar
EXPOSE 8086
ENTRYPOINT ["java", "-jar", "users-mysql.jar"]

    使用 Docker Hub (https://hub.docker.com/_/mysql/) 发布的 MySQL 镜像 运行mysql容器的命令

    docker run --name mysql-standalone -e MYSQL_ROOT_PASSWORD=rootpass -e MYSQL_DATABASE=test -e MYSQL_USER=testuser -e MYSQL_PASSWORD=testuserpass -d mysql:5.6

我看到名为 mysql-standalone 的 docker mysql 容器。

    使用 Dockerfile 创建 Docker 映像。 从 Dockerfile 的目录 -

    码头工人建造。 -t 用户-mysql

我看到 docker image created users-mysql

    运行#3中创建的Docker镜像(users-mysql)。

docker run -p 8086:8086 --name users-mysql --link mysql-standalone:mysql -d users-mysql

我看到应用程序在名为 users-mysql 的 docker 容器中运行

    我尝试访问http://localhost:8086/all/ 我收到错误:

无法访问此网站 localhost 拒绝连接。

    我尝试检查应用程序“users-mysql”和 mysql 容器“mysql-standalone”的日志 但是,我没有看到任何错误或异常。

我做错了吗?

基于一些 cmets,我想到了添加应用程序启动日志。

2018-05-17 20:52:29.921  INFO 1 --- [           main] SpringDataJpaHibernateExampleApplication : Starting SpringDataJpaHibernateExampleApplication v0.0.1-SNAPSHOT o
n b4f3a2acd0a9 with PID 1 (/users-mysql.jar started by root in /)
2018-05-17 20:52:29.937  INFO 1 --- [           main] SpringDataJpaHibernateExampleApplication : No active profile set, falling back to default profiles: default
2018-05-17 20:52:30.124  INFO 1 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConf
igEmbeddedWebApplicationContext@5b37e0d2: startup date [Thu May 17 20:52:30 UTC 2018]; root of context hierarchy
2018-05-17 20:52:34.632  INFO 1 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8086 (http)
2018-05-17 20:52:34.661  INFO 1 --- [           main] o.apache.catalina.core.StandardService   : Starting service Tomcat
2018-05-17 20:52:34.665  INFO 1 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.14
2018-05-17 20:52:34.873  INFO 1 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2018-05-17 20:52:34.873  INFO 1 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 4752 ms
2018-05-17 20:52:35.206  INFO 1 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Mapping servlet: 'dispatcherServlet' to [/]
2018-05-17 20:52:35.220  INFO 1 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2018-05-17 20:52:35.221  INFO 1 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2018-05-17 20:52:35.222  INFO 1 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2018-05-17 20:52:35.222  INFO 1 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
2018-05-17 20:52:36.727  INFO 1 --- [           main] j.LocalContainerEntityManagerFactoryBean : Building JPA container EntityManagerFactory for persistence unit 'd
efault'
2018-05-17 20:52:36.774  INFO 1 --- [           main] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [
        name: default
        ...]
2018-05-17 20:52:37.010  INFO 1 --- [           main] org.hibernate.Version                    : HHH000412: Hibernate Core 5.0.12.Final
2018-05-17 20:52:37.013  INFO 1 --- [           main] org.hibernate.cfg.Environment            : HHH000206: hibernate.properties not found
2018-05-17 20:52:37.015  INFO 1 --- [           main] org.hibernate.cfg.Environment            : HHH000021: Bytecode provider name : javassist
2018-05-17 20:52:37.121  INFO 1 --- [           main] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations 5.0.1.Final
2018-05-17 20:52:37.609  INFO 1 --- [           main] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
2018-05-17 20:52:38.541  INFO 1 --- [           main] org.hibernate.tool.hbm2ddl.SchemaExport  : HHH000227: Running hbm2ddl schema export
Hibernate: drop table if exists users
Hibernate: create table users (id integer not null, name varchar(255), salary integer, team_name varchar(255), primary key (id))
2018-05-17 20:52:38.712  INFO 1 --- [           main] org.hibernate.tool.hbm2ddl.SchemaExport  : HHH000230: Schema export complete
2018-05-17 20:52:38.761  INFO 1 --- [           main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2018-05-17 20:52:39.881  INFO 1 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.emb
edded.AnnotationConfigEmbeddedWebApplicationContext@5b37e0d2: startup date [Thu May 17 20:52:30 UTC 2018]; root of context hierarchy
2018-05-17 20:52:40.089  INFO 1 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "[/all/create],methods=[GET]" onto public java.util.List<c
om.techprimers.jpa.springdatajpahibernateexample.resource.Users> com.techprimers.jpa.springdatajpahibernateexample.resource.Resource.users()
2018-05-17 20:52:40.092  INFO 1 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "[/all/],methods=[GET]" onto public java.util.List<com.tec
hprimers.jpa.springdatajpahibernateexample.resource.Users> com.techprimers.jpa.springdatajpahibernateexample.resource.Resource.all()
2018-05-17 20:52:40.103  INFO 1 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "[/error],produces=[text/html]" onto public org.springfram
ework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.Ht
tpServletResponse)
2018-05-17 20:52:40.103  INFO 1 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "[/error]" onto public org.springframework.http.ResponseEn
tity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2018-05-17 20:52:40.180  INFO 1 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.sprin
gframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-05-17 20:52:40.181  INFO 1 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframewo
rk.web.servlet.resource.ResourceHttpRequestHandler]
2018-05-17 20:52:40.364  INFO 1 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.s
pringframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-05-17 20:52:41.108  INFO 1 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2018-05-17 20:52:41.230  INFO 1 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8086 (http)
2018-05-17 20:52:41.252  INFO 1 --- [           main] SpringDataJpaHibernateExampleApplication : Started SpringDataJpaHibernateExampleApplication in 12.225 seconds
(JVM running for 13.208)

【问题讨论】:

您是否尝试等待数据库完全初始化? 这是一种称为弹性的情况,这意味着您的应用应该处理数据库尚未访问的问题...您的健康状况也应该处理这个问题... @trust512 等到数据库完全初始化是什么意思?如果 DB 未初始化,那么我们应该在应用程序启动时看到错误。在应用程序日志中,我看到它正在创建 db 表,并且没有其他与此相关的错误。 @khmarbaise 如何确保 DB 尚不可访问?查看应用程序启动日志,数据库正在初始化。我已添加应用程序日志以供参考。 这可能是 Dockerfile 的问题吗(我的意思是我需要在某些 Linux 中运行它,例如 FROM ubuntu:latest) 【参考方案1】:

答案很简单。虽然一切都发生在windows上,但底层操作系统是Linux,所以我们必须将localhost替换为VM的linux机器的ip地址

所以不要打:http://localhost:8086/all/ 我不得不打:http://192.168.99.100:8086/all/

【讨论】:

以上是关于春季启动mysql docker windows 7的主要内容,如果未能解决你的问题,请参考以下文章

春季启动 - MySQL 设置不起作用

Windows10 Docker Mysql容器启动

春季启动MongoDB配置

docker windows 启动失败

windows下安装Docker Toolbox,启动docker失败.求助

春季启动Tomcat终止