Where Camel meets Docker
Or how to deploy your java application inside a container.
Camel is a great tool but it requires some infrastructure to run. Using docker can easy this deployment. (See our previous blog on Camel to discover how to build the Camel based application)
The docker image is created via a text file called a docker file (named Dockerfile by default). Please find below an example of such a file.
FROM java:openjdk-8u72-jdk MAINTAINER snuids COPY ./bin ./camelbin WORKDIR ./camelbin VOLUME ["/camelbin/conf"] VOLUME ["/camelbin/logs"] ENTRYPOINT ./start.sh
In our case we want a container that starts a java program, so we start from an official java image. This image already has a java 8 installed.
We then copy the content of the local folder bin into the folder camelbin inside the image. This folder contains the previously built camel runner.
We then force the working directory to camelbin inside the container.
We want to map two folder outside the container.
- A configuration folder that has our integration configuration file
- A logs folder that has our container logs
Note that it is possible to keep these files inside the containers but it is not advised because, it prevents any easy version update on the container later.
We then call a shell script used to start the java application in an infinite loop.
#!/bin/bash while true do echo 'Starting from shell' java -Dlog4j.configuration=file:./conf/log4j.properties -jar CamelRunner.jar start file:./conf/config.xml sleep 5 done
Building and creating the container
Building the container can be done via the following command:
snuids@AMAMBookPro2 ~/Desktop/Docker/DockerFiles/CamelWorkerImage $docker build . Sending build context to Docker daemon 169.7 MB Step 1 : FROM java:openjdk-8u72-jdk ---> 6c9a006fd38a Step 2 : MAINTAINER snuids ---> Using cache ---> c79a114a631d Step 3 : COPY ./bin ./camelbin ---> Using cache ---> 471b01bd686b Step 4 : WORKDIR ./camelbin ---> Using cache ---> 1601578b4d4a Step 5 : VOLUME /camelbin/conf ---> Using cache ---> e3f0d98de46b Step 6 : VOLUME /camelbin/logs ---> Using cache ---> caa9856b3f15 Step 7 : ENTRYPOINT ./start.sh ---> Using cache ---> d03abce9583c Successfully built d03abce9583c
Note the ID after the successfully build words. This is the current ID of the image. The next step is to give a version number to this image.
docker tag d03abce9583c snuids/camelrunner:v1.0
Now it is possible to use locally the image in docker. In order to push it todockerhub, the following command is required: