Apache Karaf & Docker: Vanilla distribution & personalized

Apache Karaf & Docker: Vanilla distribution & personalized

We continue the series of articles on Apache Karaf™ 4.2.1 & Docker . And this week, we are focusing on Vanilla distribution as well as personalized distribution.

Adaptation of the article:" Apache Karaf & Docker» of blog Nanthrax written by Jean-baptiste Onofré (Technical Advisor)

yupiik-article-karaf-docker

Docker image using the Vanilla Karaf distribution

The first use of build.sh is to create a Docker image using an official distribution ("Vanilla") available on Apache mirrors. Example: Creating an image using Apache Karaf 4.2.1 can be done as follows:

assembly/docker/build.sh --from-release --karaf-version 4.2.1 --image-name karaf<br/>Downloading apache-karaf-4.2.1.tar.gz from http://mirrors.standaloneinstaller.com/apache/karaf/4.2.1/
Sending build context to Docker daemon  22.06MB
Step 1/10 : FROM java:8-jre-alpine
8-jre-alpine: Pulling from library/java
709515475419: Pull complete
38a1c0aaa6fd: Pull complete
cd134db5e982: Pull complete
Digest: sha256:6a8cbe4335d1a5711a52912b684e30d6dbfab681a6733440ff7241b05a5deefd
Status: Downloaded newer image for java:8-jre-alpine
 ---> fdc893b19a14
Step 2/10 : ENV KARAF_INSTALL_PATH=/opt
 ---> Running in dadbe0fbb5d7
Removing intermediate container dadbe0fbb5d7
 ---> 4d14737dd37d
Step 3/10 : ENV KARAF_HOME $KARAF_INSTALL_PATH/apache-karaf
 ---> Running in b146df1354a1
Removing intermediate container b146df1354a1
 ---> 27f504e74175
Step 4/10 : ENV PATH $PATH:$KARAF_HOME/bin
 ---> Running in f7e262e47d31
Removing intermediate container f7e262e47d31
 ---> aeb7757dee5c
Step 5/10 : ARG karaf_dist=NOT_SET
 ---> Running in d3d0ac0b66d8
Removing intermediate container d3d0ac0b66d8
 ---> 1f9456e1edfd
Step 6/10 : ADD $karaf_dist $KARAF_INSTALL_PATH
 ---> 828362c5401a
Step 7/10 : RUN set -x &amp;&amp;   ln -s $KARAF_INSTALL_PATH/apache-karaf* $KARAF_HOME
 ---> Running in f59a3e5139cd
ln -s /opt/apache-karaf-4.2.1 /opt/apache-karaf

Removing intermediate container f59a3e5139cd
---> b05d4af0df6b
Step 8/10 : COPY docker-entrypoint.sh /
---> 003ed84df94f
Step 9/10 : EXPOSE 8101 1099 44444 8181
---> Running in 35bceaa38fe9
Removing intermediate container 35bceaa38fe9
---> 439a85a3d6f0
Step 10/10 : ENTRYPOINT ["/docker-entrypoint.sh"]
---> Running in fa51274dbbf3
Removing intermediate container fa51274dbbf3
---> e595c47809e5
Successfully built e595c47809e5
Successfully tagged karaf:latest

It can be seen that build.sh has automatically downloaded the distribution of version 4.2.1 from an Apache mirror, as well as the Alpine Java image of the Docker database. Anise, we can see the image in our Docker:

docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
karaf              latest              e595c47809e5        4 minutes ago       133MB
java                8-jre-alpine        fdc893b19a14        18 months ago       108MB

We can operate a Docker container using our image KarafWe can operate a Docker container using our image docker run "classic"

docker run -p 1099:1099 -p 8101:8101 -t --name mycontainer karaf
Starting Apache Karaf
karaf: Ignoring predefined value for KARAF_HOME
Apache Karaf (4.2.1)
Hit '' for a list of available commands
and '[cmd] --help' for help on a specific command.
Hit '' or type 'system:shutdown' or 'logout' to shutdown Karaf.
karaf@root()>

We can see the following container is displayed:

docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                                                                 NAMES
477b3cec3b76        karaf             "/docker-entrypoint.…"   3 minutes ago       Up 3 minutes        0.0.0.0:1099->1099/tcp, 8181/tcp, 0.0.0.0:8101->8101/tcp, 44444/tcp   mycontainer

When you use docker runyou should remember to expose the network ports using the -p. When you expose 8101you can connect to the Karaf including the Docker container that uses ssh or bin/client Karaf.

An alternative to the docker run (or docker start) is to use docker-compose. Apache Karaf now provides docker-compose.yml to simplify the start and stop of Apache Karaf in docker.

To start Apache Karaf Docker image, just do:

docker-compose run karaf
Starting Apache Karaf
karaf: Ignoring predefined value for KARAF_HOME...

The behavior is exactly the same as when the docker was running, but the port connection is configured in Karaf docker-compose.yml.

You can also start in "daemon" mode (without the console shell) using:

docker-compose up
Creating docker_karaf_1 …
Creating docker_karaf_1 … done
Attaching to docker_karaf_1
karaf_1  | Starting Apache Karaf

You can stop an existing container by using $> docker-compose kill

Image Docker with a personalized Karaf distribution

You have created your own custom Karaf distribution and you want to create a Docker image with the same distribution? With the script build.shyou simply need to provide the archive of your custom distribution.

 ./build.sh --from-local-dist --archive /home/jbonofre/my-karaf.tar.gz --image-name my-karaf

Using karaf dist: /home/jbonofre/my-karaf-1.0.0.tar.gz

Sending build context to Docker daemon  22.11MB
Step 1/10 : FROM java:8-jre-alpine
 ---> fdc893b19a14
Step 2/10 : ENV KARAF_INSTALL_PATH=/opt
 ---> Running in 620e32d745a9
Removing intermediate container 620e32d745a9
 ---> f773a2c25001
Step 3/10 : ENV KARAF_HOME $KARAF_INSTALL_PATH/apache-karaf
 ---> Running in 2445bbea610d
Removing intermediate container 2445bbea610d
 ---> 1974f31ada89
Step 4/10 : ENV PATH $PATH:$KARAF_HOME/bin
 ---> Running in 20b5bc409c29
Removing intermediate container 20b5bc409c29
 ---> 3e73907f5b14
Step 5/10 : ARG karaf_dist=NOT_SET
 ---> Running in e7603a1c7117
Removing intermediate container e7603a1c7117
 ---> 5de9504336cc
Step 6/10 : ADD $karaf_dist $KARAF_INSTALL_PATH
 ---> 1eba7c16ffd0
Step 7/10 : RUN set -x &&   ln -s $KARAF_INSTALL_PATH/apache-karaf $KARAF_HOME
 ---> Running in d3c685bd2bcd
ln -s /opt/apache-karaf-4.2.2-SNAPSHOT /opt/apache-karaf
Removing intermediate container d3c685bd2bcd
---> 1e9306813e59
Step 8/10 : COPY docker-entrypoint.sh /
---> e28f024ad48e
Step 9/10 : EXPOSE 8101 1099 44444 8181
---> Running in 960627ec7597
Removing intermediate container 960627ec7597
---> 9a3d6017498d
Step 10/10 : ENTRYPOINT ["/docker-entrypoint.sh"]
---> Running in 9d7fc90c19c8
Removing intermediate container 9d7fc90c19c8
---> 98ee7d5ef992
Successfully built 98ee7d5ef992
Successfully tagged my-karaf:latest

We now have a Karaf Docker image with our personalized distribution:

docker images
REPOSITORY          TAG                 IMAGE ID            CREATED              SIZE
my-karaf            latest              98ee7d5ef992        About a minute ago   133MB
java                8-jre-alpine        fdc893b19a14        18 months ago        108MB

We are in the same starting configuration: start a Docker container using  docker run or docker-compose

Discover soon the latest update on Apache Karaf 4.2.1 updates: images.

Blocked in your roadmaps?

Would you like to train your teams?

en_GBEnglish (UK)
fr_FRFrançais en_GBEnglish (UK)