2

I am using the docker-maven-plugin plugin from fabric8 to setup two containers:

  • Postgres
  • tomcat8

Both containers can be set up separately fine. I can connect from outside (from the host) to both of them. I am doing this as following:

<plugin>
    <groupId>io.fabric8</groupId>
    <artifactId>docker-maven-plugin</artifactId>
    <version>0.22.1</version>
    <configuration>
        <autoCreateCustomNetworks>true</autoCreateCustomNetworks>
        <images>
            <image>
                <alias>database</alias>
                <name>postgres:9</name>
                <run>
                    <network>
                        <name>network</name>
                        <alias>database</alias>
                    </network>
                    <ports>
                        <port>db-port:5432</port>
                    </ports>
                    <wait>
                        <log>ready to accept connections</log>
                    </wait>
                </run>
            </image>
            <image>
                <alias>container</alias>
                <name>inovatrend/tomcat8-java8</name>
                <run>
                    <network>
                        <name>network</name>
                        <alias>tomcat</alias>
                    </network>
                    <dependsOn>
                        <container>database</container>
                    </dependsOn>
                    <ports>
                        <port>tomcat-port:8080</port>
                    </ports>
                    <wait>
                        <http>
                            <url>http://localhost:${tomcat-port}</url>
                        </http>
                    </wait>
                </run>
            </image>
        </images>
    </configuration>
</plugin>

I am having troubles to configure that the tomcat8 container is allowed to connect to the Postgres container.

As you can see, I am creating a custom network in each image and the tomcat container depends on the database container.

<network>
    <name>network</name>
    <alias>database</alias>
</network>

and

<network>
    <name>network</name>
    <alias>tomcat</alias>
</network>
<dependsOn>
    <container>database</container>
</dependsOn>

But I am unable to establish a JDBC connection to localhost:5432 in the tomcat container.

Is this configuration correct? Which IP: PORT should the tomcat8 use to connect to the database? Ideally, this IP: PORT should not be fixed, so multiple maven instances can be executed concurrently without interfering (useful for simultaneous builds, such as Jenkins).

Dupinder Singh
  • 7,175
  • 6
  • 37
  • 61

1 Answers1

0

I ran into the same issue. I actually ended up with the very same configuration of the docker-maven-plugin as you did following the documentation and also didn't know what would be the URL to get from one container to another.

The missing piece was understanding how Docker networking work. Following this tutorial brought home the message.

In short. To access the database from the tomcat container use database:5432.

When containers are on the same network (e. g. custom bridge network in this case) they can resolve each other using their hostnames - e. g. database. The containers expose ports - in this case database port is randomly-assigned:5432. Now within the Docker network the ports on the machines themselves work - so 5432. From the outside, for instance from the host, it is the randomly-assigned port.

Ondrej Burkert
  • 6,617
  • 2
  • 32
  • 27