Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot set port mapping via property #736

Closed
serefarikan opened this issue Mar 19, 2017 · 8 comments · Fixed by #739
Closed

Cannot set port mapping via property #736

serefarikan opened this issue Mar 19, 2017 · 8 comments · Fixed by #739

Comments

@serefarikan
Copy link

serefarikan commented Mar 19, 2017

Given the following pom, I'd expect the flyway plugin to be able to run the migrate task.

This pom is a simplified version of a pom that works but the working version cannot set postgres_user and postgres_password environment variables.

So I wanted to use the external property support to set these variables but now the port mapping does not work. When I do

mvn verify

based on the given pom file:

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

    <groupId>mytest</groupId>
    <artifactId>development</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

        <postgres.docker.name>postgres:latest</postgres.docker.name>
        <postgres.docker.env.POSTGRES_USER>postgres</postgres.docker.env.POSTGRES_USER>
        <postgres.docker.env.POSTGRES_PASSWORD>postgres</postgres.docker.env.POSTGRES_PASSWORD>
        <postgres.docker.wait.time>11000</postgres.docker.wait.time>
        <postgres.docker.ports.1>18800:5432</postgres.docker.ports.1>
    </properties>

    <build>
        <plugins>

            <!--DOCKER-->
            <plugin>
                <groupId>io.fabric8</groupId>
                <artifactId>docker-maven-plugin</artifactId>
                <version>0.18.1</version>

                <configuration>
                    <images>
                        <image>
                            <alias>simple</alias>
                            <external>
                                <type>properties</type>
                                <prefix>postgres.docker</prefix>
                            </external>
                            <run>
                                <namingStrategy>alias</namingStrategy>
                            </run>
                        </image>
                    </images>
                </configuration>

                <executions>
                    <execution>
                        <id>start</id>
                        <phase>pre-integration-test</phase>
                        <goals>
                            <goal>build</goal>
                            <goal>start</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>stop</id>
                        <phase>post-integration-test</phase>
                        <goals>
                            <goal>stop</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <!--FLYWAY -->
            <plugin>
                <groupId>org.flywaydb</groupId>
                <artifactId>flyway-maven-plugin</artifactId>
                <version>4.1.1</version>
                <configuration>
                    <url>jdbc:postgresql://127.0.0.1:18800/postgres</url>
                    <user>postgres</user>
                    <password>postgres</password>
                </configuration>
                <executions>
                    <execution>
                        <id>start</id>
                        <phase>pre-integration-test</phase>
                        <goals>
                            <goal>clean</goal>
                            <goal>migrate</goal>
                        </goals>
                    </execution>
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>org.postgresql</groupId>
                        <artifactId>postgresql</artifactId>
                        <version>42.0.0.jre7</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <!--JUNIT-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>

        <!--MOCKITO-->
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-core</artifactId>
            <version>2.7.16</version>
        </dependency>

        <!--HIBERNATE-->
        <dependency>
           <groupId>org.hibernate</groupId>
           <artifactId>hibernate-core</artifactId>
           <version>5.2.8.Final</version>
        </dependency>

        <!--POSTGRES JDBC-->
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>42.0.0.jre7</version>
        </dependency>
    </dependencies>
</project>

Flyway cannot connect to the container, producing the following exception:

[ERROR] Failed to execute goal org.flywaydb:flyway-maven-plugin:4.1.1:clean (start) on project development: org.flywaydb.core.internal.dbsupport.FlywaySqlException:
[ERROR] Unable to obtain Jdbc connection from DataSource (jdbc:postgresql://127.0.0.1:18800/postgres) for user 'postgres': Connection to 127.0.0.1:18800 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
[ERROR] ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
[ERROR] SQL State  : 08001
[ERROR] Error Code : 0
[ERROR] Message    : Connection to 127.0.0.1:18800 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections. Connection refused (Connection refused)

I can see that the waiting time property works, but I could not get port mapping to work. If I don't use external properties, then I can't set postgres user/pass and it ends up allowing any password access with postgres user which I don't like.

What am I missing here?

@rhuss
Copy link
Collaborator

rhuss commented Mar 19, 2017

IMO, both should work ;-)

  • When you say you can't set the user/password when using the XML configuraton, did you use
<run>
  <env>
    <POSTGRES_USER>postgres</POSTGRES_USER>
    <POSTGRES_PASSWORD>postgres</POSTGRES_PASSWORD>
  </env>
</run>

You could try to use properties here to for the values of this config.

  • The external port mapping in this example looks ok, so I will try to reproduce the issue.

@rhuss
Copy link
Collaborator

rhuss commented Mar 19, 2017

Just tried it with this very pom.xml:

mvn docker:start
....
docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                     NAMES
aa77691de179        postgres:latest     "docker-entrypoint..."   12 seconds ago      Up 11 seconds       0.0.0.0:18800->5432/tcp   nervous_curran

As you can see from the Docker output there is indeed the proper portmapping enabled.

Could it be that your IP address is not localhost but (if you use a VM) an external IP ?

@serefarikan
Copy link
Author

serefarikan commented Mar 19, 2017

Many thanks for responding on a Sunday Roland. I must have missed the bit about <env></env> in the documentation. It works indeed.

I found out that the problem with the pom file is the build goal . If I do mvn verify it runs the build goal and it messes something up. The log shows an image is started but that is probably a bit misleading since it is not the postgres image in the plugin config (postgres:latest)

When you do mvn docker:start things work as expected because the build goal is not run.

Where it gets really confusing is: if I run maven verify only once with the pom file above, it leaves things in a state in which mvn docker:start no longer runs correctly. Even though I do mvn docker:stop it does not clean up whatever incorrect state build goal leaves creates.

I had to delete everything in the images directory of docker to figure this out :) Not sure if the situation with build is a bug, but I'd like to know how to restore the system to a health state once I mess it up.

@rhuss
Copy link
Collaborator

rhuss commented Mar 19, 2017

@serefarikan ah, ok. Completely agreed that building image should not happen. Unfortunately the property based mode is not as flexible as using the XML based configuration. E.g. the .env. properties are both used for building images and running containers.

I will change the algorithm that as long there is no docker.from (or docker.fromExt) then no build configuration will be created and that image then ignored during docker:build.

This is also the problem with mvn verify: First an image based on busybox is created, which, when started immediately exits. This image is stored in your local Docker daemon, so to reset the state you need to do a mvn docker:remove -Ddocker.removeAll and then a mvn docker:start (without a docker:build in between).

@serefarikan
Copy link
Author

@rhuss Thanks. Just to clarify: you mean the properties at the top of the pom file when you say "property based mode", right?

Also thanks for the instructions to clean things up.

@rhuss
Copy link
Collaborator

rhuss commented Mar 19, 2017

@serefarikan yes, these properties in the <properties> section are picked up because you configured an external config handler of type 'properties' with

<external>
  <type>properties</type>
  <prefix>postgres.docker</prefix>
</external>

@serefarikan
Copy link
Author

Thanks. Btw, the documentation uses <type>props</type> which is no longer supported I think. Should I open another issue for that?

@rhuss
Copy link
Collaborator

rhuss commented Mar 19, 2017

Thanks for the heads up. Let me check, will fix it right now as I'm already preparing a PR for this issue.

rhuss added a commit to rhuss/docker-maven-plugin that referenced this issue Mar 19, 2017
Otherwise no build configuration will be created. This fixes fabric8io#736.
Also made error messages a bit more verbose.
rgbj pushed a commit to rgbj/docker-maven-plugin that referenced this issue Jun 21, 2017
Otherwise no build configuration will be created. This fixes fabric8io#736.
Also made error messages a bit more verbose.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants