I had used the plugin of Fabric8 to orchestrate my images and containers.
Among them, I would only need one of the containers to be erased when I launched the maven (the app); The others containers: RabbitMQ, MySQL, ... that i import directly from the hub, I would not need to stop and erase them if they already had a container with the image running.
For now I used mvn clean package docker:stop docker:build docker:start to stop build (only the app) and start all the containers, would anyone know of any property to choose which container is stopped/deleted and which does not?
UPDATE
<plugin>
<groupId>io.fabric8</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.23.0</version>
<configuration>
<!-- <dockerHost>http://192.168.99.100:2375</dockerHost> -->
<dockerHost>http://127.0.0.1:2375</dockerHost>
<verbose>true</verbose>
<images>
<image>
<name>${docker.image.name}</name>
<alias>${docker.alias.name}</alias>
<build>
<dockerFile>Dockerfile</dockerFile>
<dockerFileDir>${project.basedir}/src/test/docker/</dockerFileDir>
<assembly>
<descriptorRef>artifact</descriptorRef>
</assembly>
<tags>
<tag>latest</tag>
<tag>${project.version}</tag>
</tags>
</build>
<run>
<namingStrategy>alias</namingStrategy>
<ports>
<port>
8888:8888
</port>
</ports>
</run>
</image>
<image>
<name>rabbit-sdk</name>
<alias>some-rabbit</alias>
<build>
<assembly>
<descriptorRef>artifact</descriptorRef>
</assembly>
<dockerFile>DockerfileRabbit</dockerFile>
<dockerFileDir>${project.basedir}/src/test/docker/</dockerFileDir>
<tags>
<tag>latest</tag>
</tags>
</build>
<run>
<namingStrategy>alias</namingStrategy>
<ports>
<port>
8181:15672
</port>
<port>
5671:5671
</port>
<port>
5672:5672
</port>
</ports>
</run>
</image>
<image>
<name>redis</name>
<alias>some-redis</alias>
<run>
<namingStrategy>alias</namingStrategy>
<ports>
<port>6379:6379</port>
</ports>
</run>
</image>
</images>
</configuration>
</plugin>
The maven plugin documentation for the docker:stop documents all the available flags for stopping container. In particular, the keepContainer system property can be useful:
keepContainer
If set to true not destroy container after they have been stopped. Default is false.
When this is used, the image containers defined in the pom will be stopped but not removed when invoking the docker:stop goal. You can the clean the app container with the docker:remove goal.
With the docker:remove you can specify explicitly which container to delete. In your case the app container:
mvn -Ddocker.filter=<app-image-name> docker:remove
Thus the complete maven command would be:
mvn clean package -Ddocker.keepContainer -Ddocker.filter=<app-image-name> docker:stop docker:remove docker:build docker:start
To completely remove a container, you’ll need to pass the option keepContainer=false to the docker:stop command. To remove a specific docker app image, you can use the removeMode property with the filter option. It will be like:
mvn -Ddocker.filter=<image-name-for-app> docker:remove
Related
I am using docker-maven-plugin to run an image from a remote repository.
The image is running using docker:start but it seems like the volumes not working.
I run the command using groovy (jenkins):
sh(script:"mvn -B -gs ${cfg} -f lib/pom.xml docker:start", returnStatus: true)
my pom.xml code is:
<plugin>
<groupId>io.fabric8</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.31.0</version>
<configuration>
<images>
<image>
<name>imageName:latest</name>
<run>
<volumes>
<bind>
<volume>/inputs:/inputs</volume>
<volume>/out:/out</volume>
<volume>/env:/env</volume>
</bind>
</volumes>
</run>
</image>
</images>
</configuration>
</plugin>
any idea what is going wrong?
Im building a docker image from maven using io.fabric8:docker-maven-plugin.
i want the container to run like :
java -jar -Dspring.profiles.active=prod /maven/myapp-1.4.3.jar
java -jar -Dspring.profiles.active=dev /maven/myapp-1.4.3.jar
so i actually want the image to pass -Dspring.profiles.active=$SPRING_PROFILES_ACTIVE and that variable will be passed in via docker run when starting a container:
docker run --env SPRING_PROFILES_ACTIVE=prod
however, it looks like the container run this command:
java -jar -Dspring.profiles.active=$SPRING_PROFILES_ACTIVE /maven/myapp-1.4.3.jar
and the $SPRING_PROFILES_ACTIVE is not replaced with the env var.
pom.xml:
<plugin>
<groupId>io.fabric8</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.31.0</version>
<extensions>true</extensions>
<configuration>
<verbose>true</verbose>
<images>
<image>
<name>${docker.registry}/${project.artifactId}</name>
<build>
<from>java:8-jdk-alpine</from>
<tags>
<tag>${project.version}</tag>
<tag>latest</tag>
</tags>
<entryPoint>
<exec>
<args>java</args>
<args>-jar</args>
<!--<args>-Dspring.profiles.active=$SPRING_PROFILES_ACTIVE</args>-->
<args>/maven/${project.artifactId}-${project.version}.jar</args>
</exec>
</entryPoint>
<assembly>
<descriptorRef>artifact</descriptorRef>
</assembly>
</build>
</image>
</images>
I am building a container using Jib's Maven plugin.
I have a file called /tmp/folder/file.json on my host machine, which I need to be able to read from inside a container.
I tried to mount /tmp/folder using the volumes feature:
<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>jib-maven-plugin</artifactId>
<version>1.0.0</version>
<configuration>
<to>
<image>myimage</image>
</to>
<container>
<volumes>
<volume>/tmp/folder</volume>
</volumes>
</container>
</configuration>
</plugin>
I think /tmp/folder is accessible from the container but file.json isn't. At least when I try this
docker exec -it my_cotainer /bin/ls /tmp/folder
Nothing comes back.
Am I using the volumes feature correctly?
If you don't specify from property the default gcr.io/distroless/java image is used. You can execute /bin/ls using this image.
"Distroless" images contain only your application and its runtime dependencies. They do not contain package managers, shells or any other programs you would expect to find in a standard Linux distribution.
Try to build from linux based image.
<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>jib-maven-plugin</artifactId>
<version>1.0.0</version>
<configuration>
<from>
<image>openjdk:8-jre-alpine</image>
</from>
<to>
<image>myimage</image>
</to>
<container>
<volumes>
<volume>/tmp/folder</volume>
</volumes>
</container>
</configuration>
</plugin>
I have a .jar that contains multiple public static void main(psvm)'s that I want to be able to call when I do docker run ... -e <class.path.from.env> on the image and pass an environment variable to specify the class path. Something like this:
<plugin>
<groupId>io.fabric8</groupId>
<artifactId>docker-maven-plugin</artifactId>
<configuration>
<images>
<image>
<name>${project.artifactId}</name>
<build>
<from>java:8-jre</from>
<tags>
<tag>${build.environment}-latest</tag>
<tag>${build.environment}-${build.number}</tag>
</tags>
<entryPoint>
<exec>
<arg>java</arg>
<arg>-Duser.timezone=UTC</arg>
<arg>-cp</arg>
<arg>/opt/${project.artifactId}-${project.version}.jar</arg>
<arg>${class.path.from.env}</arg>
</exec>
</entryPoint>
<assembly>
<basedir>/opt</basedir>
<inline>
<files>
<file>
<source>target/${project.artifactId}-${project.version}.jar</source>
</file>
</files>
</inline>
</assembly>
</build>
</image>
</images>
</configuration>
</plugin>
Although I read the whole documentation for docker-maven-plugin, I'm not sure how I can make this work. Basically where do I declare the environment variable class.path.from.env and how can I make sure it gets the one I pass through -e in docker run ...?
I think you need to declare a <run> section next to your <build> section, and add your env variable to <env>, as described here: https://dmp.fabric8.io/#misc-env
<run>
<env>
<CATALINA_OPTS>-Xmx32m</CATALINA_OPTS>
<JOLOKIA_OFF/>
</env>
I have a Spring Boot application that connects to a MongoDB instance. For MongoDB instance I use a docker, specifically I use the maven docker plugin from fabric8. I have this configuration:
<plugin>
<groupId>io.fabric8</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.21.0</version>
<configuration>
<logDate>default</logDate>
<autoPull>true</autoPull>
<images>
<image>
<name>mongo:3.0.15</name>
<run>
<ports>
<port>27018:27017</port>
</ports>
<wait>
<log>(?s)database system is ready to accept connections.*database system is ready to accept connections</log>
<time>40000</time>
</wait>
<log>
<prefix>mongo-container</prefix>
<color>yellow</color>
</log>
</run>
</image>
</images>
</configuration>
</plugin>
It works perfectly if I run mvn docker:start and in other terminal mvn spring-boot:run.
What I want to know is if it's possible to create some maven configuration to run this on a single command like mvn docker:start spring-boot:run.
Thanks!