Using Docker compose

So far, we have looked at writing scripts and using Maven plugins, but we haven't looked at the option that Docker currently suggests you to follow, Docker compose.  Docker compose is a tool that lets you define a multiple container system using a YAML file. You can then use Docker compose to start and stop this system. Let's get this set up for our Selenium-Grid. First of all, we need to create a file called docker-compose.yml:

version: '2.2'
services:
selenium-hub:
image: selenium/hub:3.11.0
ports:
- 4444:4444

chrome:
image: selenium/node-chrome:3.11.0
links:
- selenium-hub:hub

firefox:
image: selenium/node-firefox:3.11.0
links:
- selenium-hub:hub

Here, you can see that we have defined the same system again, only this time it's in YAML format. We can now use Docker compose to start up our Selenium-Grid:

docker-compose up -d

No surprises here; Docker compose ran, created our grid setup, and exited again.  You can check that everything is working by navigating to http://127.0.0.1:4444/grid/console in your browser again, like you did before. You can also check that it's fully functional by running:

mvn clean verify -Dremote=true -Dbrowser=firefox -DgridURL=http://127.0.0.1:4444/wd/hub

Once you are happy it's functionally equivalent to what we did before, you can tear it down again by running the following command:

docker-compose down

You will see that Docker compose will now shut down all of your containers; it will also remove them so that everything is ready for another run later on. Now the configuration for this is much easier than the previous two solutions, but how do we plug this into our Maven project?  Luckily, the Maven plugin we are using has support for Docker compose. Let's modify the configuration in our POM file to take advantage of this. First of all, we need to create a docker directory under src/test, then we need to save our docker-compose.yml there.  Then, we will modify the plugin configuration in our POM to point at our Docker compose file:

<plugin>
<groupId>io.fabric8</groupId>
<artifactId>docker-maven-plugin</artifactId>
<configuration>
<images>
<image>
<external>
<type>compose</type>
<basedir>src/test/docker</basedir>
<composeFile>docker-compose.yml</composeFile>
</external>
</image>
</images>
</configuration>
<executions>
<execution>
<id>start</id>
<phase>pre-integration-test</phase>
<goals>
<goal>start</goal>
</goals>
</execution>
<execution>
<id>stop</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>

As you can see, this is instantly much cleaner. We have changed our 40-line configuration block into an 11-line configuration block. Functionality, it is identical to our previous Maven plugin implementation, so the same command will still work to run tests against this Selenium-Grid:

mvn clean verify

I would suggest that Docker compose is the cleanest and easiest implementation of the lot.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.221.98.71