使用Docker和docker compose与Spring Boot REST应用程序

时间:2019-02-12 10:20:38

标签: java spring spring-boot docker docker-compose

我一直在阅读有关Docker以及如何使用它运行Spring应用程序的信息。我读到在使用Docker的情况下,我们使用Docker引擎(而不是来宾OS),它是OS的非常薄的一层,并且容器可以与主机OS对话以获取那里的内核功能。这样我们就可以拥有一个非常轻巧的容器。

这很有意义。

我想要一个非常基本的设置,并将Docker用于我正在开发的Spring BootMySQL应用程序。该应用程序本身运行良好(没有Docker)。提供了项目结构,

enter image description here

首先我运行$ mvn clean package

这将在Appointment-0.0.1-SNAPSHOT.jar目录中创建/target

enter image description here

我将在阅读教程后定义Dockerfile

FROM java:8
VOLUME /tmp
ADD /target/Appointment-0.0.1-SNAPSHOT.jar Appointment.jar
RUN bash -c 'touch /Appointment.jar'
ENTRYPOINT ["java","-jar","/Appointment.jar"]

下面提供了我的docker-compose.yaml文件

version: '3'

services:
  web:
    build: .
    ports:
    - "5000:5000"
    volumes:
    - .:/code
    - logvolume01:/var/log

  appointment-mysql:
      container_name: appointment-mysql
      image: mysql/mysql-server:5.7
      environment:
        MYSQL_DATABASE: Appointment
        MYSQL_ROOT_PASSWORD: testpassword
        MYSQL_ROOT_HOST: '%'
      ports:
      - "3307:3307"
      restart: always

volumes:
  logvolume01: {}

我通常将MySQL的端口更改为3307。最后,我运行命令

$ docker-compose up -d

我收到输出消息,

appointment-mysql is up-to-date
Starting appointmentmanager_web_1 ... done

我什么都没看见。谁能提供清晰的说明,说明如何使用Docker和docker-compose运行Spring Boot应用程序?

很明显,我没有收到任何错误消息,但是通常,当我运行该应用程序时, 一个方法被调用并在数据库中加载一堆数据。

public static void main(String[] args) {

        SpringApplication.run(AppointmentApplication.class, args);
        loadAppointmentsData();
    }

,我可以使用cURL对应用程序进行各种查询。似乎缺少了一些东西。我知道学习和使用Docker太晚了,如果有人帮助我通过Spring Boot应用程序开始使用Docker,我将非常感激。

更新

我尝试使用命令docker-compose up(不带-d),看来我没有什么错误,

$ docker-compose up
appointment-mysql is up-to-date
Starting appointmentmanager_web_1 ... done
Attaching to appointment-mysql, appointmentmanager_web_1
appointment-mysql    | [Entrypoint] MySQL Docker Image 5.7.25-1.1.10
appointment-mysql    | [Entrypoint] Starting MySQL 5.7.25-1.1.10
appointment-mysql    | [Entrypoint] MySQL Docker Image 5.7.25-1.1.10
appointment-mysql    | [Entrypoint] Starting MySQL 5.7.25-1.1.10
appointment-mysql    | [Entrypoint] MySQL Docker Image 5.7.25-1.1.10
appointment-mysql    | [Entrypoint] Starting MySQL 5.7.25-1.1.10
web_1                | [INFO] Scanning for projects...
web_1                | [INFO] ------------------------------------------------------------------------
web_1                | [INFO] BUILD FAILURE
web_1                | [INFO] ------------------------------------------------------------------------
web_1                | [INFO] Total time: 0.205 s
web_1                | [INFO] Finished at: 2019-02-12T14:33:03Z
web_1                | [INFO] ------------------------------------------------------------------------
web_1                | [ERROR] No goals have been specified for this build. You must specify a valid lifecycle phase or a goal in the format <plugin-prefix>:<goal> or <plugin-group-id>:<plugin-artifact-id>[:<plugin-version>]:<goal>. Available lifecycle phases are: validate, initialize, generate-sources, process-sources, generate-resources, process-resources, compile, process-classes, generate-test-sources, process-test-sources, generate-test-resources, process-test-resources, test-compile, process-test-classes, test, prepare-package, package, pre-integration-test, integration-test, post-integration-test, verify, install, deploy, pre-clean, clean, post-clean, pre-site, site, post-site, site-deploy. -> [Help 1]
web_1                | [ERROR] 
web_1                | [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
web_1                | [ERROR] Re-run Maven using the -X switch to enable full debug logging.
web_1                | [ERROR] 
web_1                | [ERROR] For more information about the errors and possible solutions, please read the following articles:
web_1                | [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/NoGoalSpecifiedException
appointmentmanager_web_1 exited with code 1

我认为这与pom.xml文件配置有关,因此,我还在下面提供了该信息。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.appoint.manager</groupId>
    <artifactId>Appointment</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>Appointment</name>
    <description>A project for Appointment Management</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>


    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.1.2.RELEASE</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.7</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.github.javafaker/javafaker -->
        <dependency>
            <groupId>com.github.javafaker</groupId>
            <artifactId>javafaker</artifactId>
            <version>0.17.2</version>
        </dependency>


        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.15</version>
        </dependency>


        <!-- https://mvnrepository.com/artifact/com.jayway.jsonpath/json-path -->
        <dependency>
            <groupId>com.jayway.jsonpath</groupId>
            <artifactId>json-path</artifactId>
            <version>2.4.0</version>
        </dependency>


        <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.9.8</version>
        </dependency>


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.assertj/assertj-core -->
        <dependency>
            <groupId>org.assertj</groupId>
            <artifactId>assertj-core</artifactId>
            <version>3.11.1</version>
            <scope>test</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.mockito/mockito-core -->
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-core</artifactId>
            <version>2.24.0</version>
            <scope>test</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.1.4.RELEASE</version>
            <scope>test</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.hamcrest/hamcrest-library -->
        <dependency>
            <groupId>org.hamcrest</groupId>
            <artifactId>hamcrest-all</artifactId>
            <version>1.3</version>
            <scope>test</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.3.2</version>
            <scope>test</scope>
        </dependency>


        <!-- https://mvnrepository.com/artifact/org.hamcrest/hamcrest-library -->
        <dependency>
            <groupId>org.hamcrest</groupId>
            <artifactId>hamcrest-library</artifactId>
            <version>2.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>



    <build>
        <defaultGoal>install</defaultGoal>
        <!--<sourceDirectory>src</sourceDirectory>-->

        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>

        </plugins>
    </build>

</project>

所以认为错误主要在下面描述,这是No goals have been specified

No goals have been specified for this build. You must specify a valid lifecycle phase or a goal in the format <plugin-prefix>:<goal> or <plugin-group-id>:<plugin-artifact-id>[:<plugin-version>]:<goal>. Available lifecycle phases are: validate, initialize, generate-sources, process-sources, generate-resources, process-resources, compile, process-classes, generate-test-sources, process-test-sources, generate-test-resources, process-test-resources, test-compile, process-test-classes, test, prepare-package, package, pre-integration-test, integration-test, post-integration-test, verify, install, deploy, pre-clean, clean, post-clean, pre-site, site, post-site, site-deploy.

1 个答案:

答案 0 :(得分:1)

有什么问题?您的应用程序似乎正在运行,像在没有docker的情况下进行测试一样进行测试(如果它是re​​st api,请测试运行状况检查)

并查看容器的日志(如果有错误堆栈)