0

I am currently working on a project base on microservices using Spring Boot and Docker. I have created a microservice which will be taking care of database access, for now it's main method is just simply displaying something just to check if it works. The basic concept is that other services may send requests regarding data to DataAccessService which accesses the database and sends reply back.

I was able to load dependencies, compile the project without warnings, dockerize it without issues. Running the app in Docker container shows no problems. However, when I'm trying to access the address: localhost:8080/hello" I get the ERR_CONNECTION_REFUSED. Not really sure what could be the issue here... All kinds of help appreciated

Below are the dependencies from the pom file:

<properties>
        <java.version>11</java.version>
        <spring-cloud.version>2021.0.0-RC1</spring-cloud.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bootstrap</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <scope>runtime</scope>
</dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.2</version>
                <configuration>
                    <testFailureIgnore>true</testFailureIgnore>
                </configuration>
            </plugin>
        </plugins>
    </build>

The application properties file:


spring.cloud.config.server.bootstrap=true
spring.cloud.config.uri=http://configs:20101
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=password
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.jpa.hibernate.ddl-auto=update
eureka.client.registerWithEureka=false
eureka.client.fetchRegistry=false

The main class of the application:


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Configuration;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
@Configuration
@EnableAutoConfiguration

@SpringBootApplication
public class DataAccessServiceApplication {
       @GetMapping("/hello")
        public String index(final Model model) {
            model.addAttribute("title", "Docker + Spring Boot");
            model.addAttribute("msg", "Welcome to the docker container!");
            return "index";
        }
    public static void main(String[] args) {
        SpringApplication.run(DataAccessServiceApplication.class, args);
    }

}

When running it in Docker container I get the output: enter image description here

EDIT: Adding the Dockerfile info:

FROM openjdk:11-jdk
ADD /target/data-access-service-0.0.1-SNAPSHOT.jar .
EXPOSE 8080
CMD java -jar data-access-service-0.0.1-SNAPSHOT.jar --envname=prod
krysznys
  • 79
  • 1
  • 6
  • What docker command are you using exactly? Have you exposed port 8080 to the host? – tgdavies Nov 23 '21 at 23:55
  • For building the docker image I used `docker build -f Dockerfile.txt -t da-service . ` and for running I used ` docker run da-service ` .I copied the contents of Dockerfile into the post – krysznys Nov 24 '21 at 00:14
  • Read https://stackoverflow.com/questions/22111060/what-is-the-difference-between-expose-and-publish-in-docker (and why are you exposing 8181 when your service is on 8080, according to the output you show above?) – tgdavies Nov 24 '21 at 00:19
  • @tgdavies, as for the exposing part it was my mistake, fixed it – krysznys Nov 24 '21 at 00:27
  • Okay, I tried running it with command `docker run -p 0.0.0.0:8080:80 da-service` but it doesn't work, I used this command, basing on this page: [link](https://stackoverflow.com/questions/46184173/err-empty-response-from-docker-container/46203897), but I am left with `ERR_EMPTY_RESPONSE` when trying to access localhost:8080/hello – krysznys Nov 24 '21 at 18:44
  • 1
    You just want `-p 8080:8080` because the container port is 8080 and that's also the port you want to use on the host. – tgdavies Nov 24 '21 at 23:06
  • Okay, it works now! Thanks! – krysznys Nov 25 '21 at 14:59

0 Answers0