0

I have to write 3 simple microservices, using spring boot, and dockerize them. Since I have just started reading about dockers I have encountered several issues.

To build docker images I am using this maven plugin:

<plugin>
    <groupId>com.spotify</groupId>
    <artifactId>docker-maven-plugin</artifactId>
    <version>1.2.0</version>
    <configuration>
        <imageName>example</imageName>
        <baseImage>java</baseImage>
        <entryPoint>["java", "-jar", "/${project.build.finalName}.jar"]</entryPoint>
        <resources>
            <resource>
                <targetPath>/</targetPath>
                <directory>${project.build.directory}</directory>
                <include>${project.build.finalName}.jar</include>
            </resource>
        </resources>
        <imageTags>
            <imageTag>latest</imageTag>
        </imageTags>
    </configuration>
</plugin>

Requirements

  • Docker containers have to run without ROOT user privilages.
  • Building maven script should build docker containers.
  • Database also needs to be dockerized (all microservices use one database)

My issues

I have test service .jar and everything works just fine (it connects to dockerized postgres) but when I run docker image (built with sudo mvn docker:build) of test service .jar I can't connect to postgres.

I have found that localhost in data source might be the issue:

spring.datasource.url=jdbc:postgresql://localhost:5432/dockertest

But I don't know what should I put in place of localhost.


I would appreciate any tips on how to easily create and run those docker images (meeting above requirements)

1 Answer 1

1

Docker wont see your localhost. You have to create a network, and join with all the services (see docker network cli command) Then use the app's docker name to refere it in the network (if name is abc-service then use spring.datasource.url=jdbc:postgresql://abc-service/dockertest).

Or you can use docker compose, which will auto create the network for your services.

example docker-compose.yml:

version: '3' services: redis-server: image: 'redis' eureka-server: build: ./eureka-server ports: - "8010:8010" book-service: build: ./book-service build: ./writer-service

then connect like this in proper services' application.yml:

redis: host: redis-server port: 6379

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.