Build a Docker Image and Run a Docker Container with docker maven plugin fabric8.io
Introduction
In this tutorial, we are going to
- Build a docker image
- Link it to running DB container (Mongo)
- Start the container
- Stop the container
Please note that Docker and Jenkins were both running on Centos (Linux) as separate processes
- For below steps, I have used Spring Boot Rest service project which is connected to MongoDB. See below link for reference.
- Using io.fabric8 plugin, under the plugin section add the io.fabric8 plugin123456<plugin><groupId>io.fabric8</groupId><artifactId>docker-maven-plugin</artifactId><version>0.20.0</version>....
- We need to first build the image from Dockerfile, this plugin supports all the command which are in Dockerfile but I think making docker image from Docker file is much easier and simpler. So I telling the maven plugin in below step to look for Dockerfile in root( project base directory)1234567<name>springboot-mongo-dockerimage:${project.version}</name><alias>springboot-mongo-dockerimage</alias><build><dockerFileDir>${project.basedir}</dockerFileDir></build>
- Next step is to specify what docker container needs to do when it runs.
- Since we want to make sure that mongo DB container should be running before this image starts running. We add a tag called <dependsOn>.
- If the MongoDB is started we would like to link this container to Mongo so both can talk to each other, we specify that by adding a tag called <link>12345678<dependsOn><container>mongo</container></dependsOn><links><link>mongo</link></links>
- Next, we will specify on which port we would like to run this container and which port it should map to localhost machine12345<ports><port>9876:8080</port></ports>
- We enable all the logging using <log> tag, so we could see any errors or debug if some problem arises.
- Next, we will add execution phases, we will cover these steps
- Remove an existing image, if it exists
- Build the Image
- Deploy the image12345678910111213<executions><execution><id>start</id><phase>pre-integration-test</phase><goals><goal>stop</goal><goal>build</goal><goal>start</goal></goals></execution>
- Now run this pom.xml using mvn clean install and check your docker containers so see if it running.
Here is the complete build section from the plugin, hope this help.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>io.fabric8</groupId> <artifactId>docker-maven-plugin</artifactId> <version>0.20.0</version> <configuration> <!--<dockerHost>tcp://REMOTE_IP:2375</dockerHost>--> <images> <image> <name>springboot-mongo-dockerimage:${project.version}</name> <alias>springboot-mongo-dockerimage</alias> <build> <dockerFileDir>${project.basedir}</dockerFileDir> </build> <run> <namingStrategy>alias</namingStrategy> <dependsOn> <container>mongo</container> </dependsOn> <links> <link>mongo</link> </links> <ports> <port>9876:8080</port> </ports> <log> <prefix>TC</prefix> <date>default</date> <color>cyan</color> </log> </run> </image> </images> </configuration> <executions> <execution> <id>start</id> <phase>pre-integration-test</phase> <goals> <goal>stop</goal> <goal>build</goal> <goal>start</goal> </goals> </execution> <!-- Uncommment this execution phase if you wan to remove container after testing -> <!--<execution> <id>clean image</id> <phase>post-integration-test</phase> <goals> <goal>stop</goal> </goals> </execution> --> </executions> </plugin> </plugins> </build> |
If you get an error : io.fabric8:docker-maven-plugin:0.20.0:build failed: A tar file cannot include itself
OMG… finally. Thank you very much for this solution!
Glad that it worked for you !!
Hi,
Great posts! Thank you.
I cannot run it because I get dockerHost errors. Please help.
When building this on Windows 10 Home, I get an error: No given, no DOCKER_HOST environment variable, no read/writable ‘/var/run/docker.sock’ or ‘//./pipe/docker_engine’ and no external provider like Docker machine configured.
I tried different dockerHosts, like unix:///var/run/docker.sock or localhost, but then I get errors like Execution default-cli of goal io.fabric8:docker-maven-plugin:0.21.0:build failed: An API incompatibility was encountered while executing io.
fabric8:docker-maven-plugin:0.21.0:build: java.lang.UnsatisfiedLinkError: unknown
[ERROR] —————————————————–
[ERROR] realm = plugin>io.fabric8:docker-maven-plugin:0.21.0
[ERROR] strategy = org.codehaus.plexus.classworlds.strategy.SelfFirstStrategy
Etc.
I really hope you can help.
Regards, J
I have implemented above in MAC-OS, so I am not fully aware of windows. But can you please use either or 2 below and see if that solves the problem.
Hi I am also facing the same problem. I am using Docker Toolbox, tried creating entry as suggested above, but no luck. Getting below error:
[ERROR] Failed to execute goal io.fabric8:docker-maven-plugin:0.20.0:stop (start) on project spring-boot-docker: Cannot create docker access object: Cannot extract API version from server : Connect to 192.168.99.100:2375 [/192.168.99.100] failed: Connection refused: connect -> [Help 1]
Any help ?