Arefe
Arefe

Reputation: 12427

Use Docker and docker compose with the Spring Boot REST app

I've been reading about the Docker and how to use it to run the Spring app. I read that in case of Docker, we use the Docker engine ( instead of guest OS) which is a very thin layer of the OS, and the container can talk down into the host OS in order to get to the kernel functionality there. And that allows us to have a very lightweight container.

This kind of make sense.

I would like to have a very basic setup and use the Docker for the Spring Boot and MySQL app I'm developing. The app runs itself fine (without Docker). The project structure is provided,

enter image description here

First I run the $ mvn clean package

This creates the Appointment-0.0.1-SNAPSHOT.jar in the /target directory.

enter image description here

I define the Dockerfile afterward reading a tutorial,

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"]

My docker-compose.yaml file provided below,

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: {}

I intionally chnage the port for the MySQL to the 3307. Finally, I run the command,

$ docker-compose up -d

I get the output message,

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

I see nothing happens afterward. Would anyone please provide a clear instruction about how do I run a Spring boot app with the Docker and docker-compose?

Clearly, I don't get any error message, but, normally when I run the app, a method is called and load a bunch of data in the database.

public static void main(String[] args) {

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

and I can use the cURL to do various queries for the app. It seems something is missing. I know its late to learn and use the Docker, I really appreciate if someone helps me to get started using the Docker with the Spring Boot app.

UPDATE

I try the command docker-compose up (without -d) and it seems I have few errors,

$ 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

I assumed this is related to the pom.xml file configuration, so, I also provided that below.

<?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>

So think the error is mainly described below and this is 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.

Upvotes: 0

Views: 596

Answers (1)

sam3131
sam3131

Reputation: 397

What is the probleme? your application seem to be running, test it like you use to do without docker (test the healthcheck if it's a rest api)

and see logs of your container if you have an error stack

Upvotes: 1

Related Questions