springboot mysql docker windows7

时间:2018-05-17 21:50:04

标签: mysql maven docker spring-boot

OS- 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"]
  1. 使用Docker Hub发布的MySQL镜像(https://hub.docker.com/_/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

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

    1. 使用Dockerfile创建Docker镜像。 从Dockerfile目录 -

      docker build。 -t users-mysql

    2. 我看到docker image创建了users-mysql

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

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

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

        1. 我尝试访问http://localhost:8086/all/ 我收到错误:
        2. 无法访问此网站 localhost拒绝连接。

          1. 我尝试检查应用程序的日志" users-mysql"和mysql容器" mysql-standalone" 但是,我没有看到任何错误或例外。
          2. 我做错了吗?

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

            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)
            

1 个答案:

答案 0 :(得分:0)

答案很简单。尽管一切都在Windows上进行,但是底层操作系统是Linux,因此我们必须用VM的linux机器的ip地址替换localhost

所以不要点击:http://localhost:8086/all/ 我必须点击:http://192.168.99.100:8086/all/