Building a multi-container Spring Boot and MongoDB webapp with Docker 1.12.x – part 2

In the first part of this article, I showed how I split the frontend, backend and database all into their own containers, and how each could be individually scaled using docker-compose.

If you’re already familiar with docker-compose and using haproxy for load balancing against container, you might have noticed there’s a limitation in my approach, as both the backend REST service was exposing it’s port 8080 externally, and so I don’t think HTTP requests from the frontend browser in the app were ever passing through the haproxy to be load balanced, only the requests to load the front end on port 80 were being load balanced.

I looked into how I could configure haproxy with multiple backends, listening on different ports, but eventually came to the conclusion that adding two different haproxy containers, one load balancing for port 80 and one for port 8080 was easy to do.

I’m not sure if this is the best way to approach this, but it certainly. works. Leave me a comment if you have any suggestions.

Here’s my final docker-compose.yml:

 

version: '2'
  
services:
    mongodata:
        image: mongo:3.2
        volumes:
        - /data/db
        entrypoint: /bin/bash
    mongo:
        image: mongo:3.2
        depends_on: 
            - mongodata
        volumes_from:
            - mongodata
        ports:
            - "27017"
    addressbook:
        image: addressbook
        depends_on: 
            - mongo
        environment:
            - MONGODB_DB_NAME=addressbook
        ports:
            - "8080"
        links:
            - mongo
    web:
        image: docker-web-angularjs
        ports:
            - "80"
    lb-web:
        image: dockercloud/haproxy
        depends_on: 
            - web
        environment:
            - STATS_PORT=1936
            - STATS_AUTH="admin:your-password"
        links:
            - web
        volumes:
            - /var/run/docker.sock:/var/run/docker.sock
        ports:
            - 80:80
            - 1936:1936
    lb-addressbook:
        image: dockercloud/haproxy
        depends_on: 
            - addressbook
        environment:
            - STATS_PORT=1937
            - STATS_AUTH="admin:your-password"
        links:
            - addressbook
        volumes:
            - /var/run/docker.sock:/var/run/docker.sock
        ports:
            - 8080:80
            - 1937:1937

Spring Boot REST app in a Docker Container on the Raspberry Pi

Docker on the Raspberry Pi? Sure, with help from Hypriot who provide an iso image ready to go, getting docker up and running on the Pi is as simple as burning their image to an sd card and booting it up. Ok, so that part is simple.

Building an image to run on the Pi is not as straightforward as it sounds. Well, it’s no more complicated than building an image from a Dockerfile, but given that the Pi is ARM based and not x86, you have to build an image that is built/compiled with executables/libraries for ARM.

This makes complete sense when you think about it, you’re not going to get an image containing x86 executables and libraries to run on a Pi, but it’s rather sad that it isn’t possible to build an image for example from my Mac and and I can run on my Mac, or Linux, or on the Pi. Well, you can build the image on any architecture, but you can’t run an ARM compatible image on the Mac (or docker-machine/boot2docker).

Ok, so that in mind, there are a number of ARM compatible images on Docker Hub already – Hypriot also have base images ready to go. So to run Spring Boot in a container on the Pi, I need an ARM image with Java. Luckily, here’s one ready to go.

For a quick test, I took my simple Sprig Boot REST app I already have on GitHub, and threw together a Dockerfile using Alpine first to check it would run locally:

FROM java:openjdk-8-alpine

ADD SpringBootREST-0.0.1-SNAPSHOT.jar /opt/SpringBootREST-0.0.1-SNAPSHOT.jar

EXPOSE 8080

ENTRYPOINT ["java", "-jar", "/opt/SpringBootREST-0.0.1-SNAPSHOT.jar"]

This runs as expected in docker-machine on my Mac.

To push to the Pi, the only change is to replace the base image with an ARM compatible image with Java, so this looks like:

FROM hypriot/rpi-java

ADD SpringBootREST-0.0.1-SNAPSHOT.jar /opt/SpringBootREST-0.0.1-SNAPSHOT.jar

EXPOSE 8080

ENTRYPOINT ["java", "-jar", "/opt/SpringBootREST-0.0.1-SNAPSHOT.jar"]

To get it to my Pi, I set up a local Registry to push the image to, and then pulled it from the Pi. So tag the image to the registry:

docker tag spring-boot-rest-rpi registry-ip:5000/spring-boot-rest-rpi

…push to the registry:

docker push registry-ip:5000/spring-boot-rest-rpi

and then pull from the Pi:

docker pull registry-ip:5000/spring-boot-rest-rpi

And then fire it up!

docker run -it -p 8080:8080 registry-ip:5000/spring-boot-rest-rpi

Spring Boot in a Docker Container on the Pi!

Game development for Android (attempt #3)

A while back (9 years it seems, see here) I had the urge to try and create a game in Java after having spent many years of Java development for business, but I’d never developed a game. It turned out OK (final version here), and along the way I abstracted a simple 2d game engine (here).

Inspired by one of the game development panels at Silicon Valley Comic Con last weekend, I’ve picked up a copy of  Android Game Development by Example by John Horton, and now off I go 😉 I’ll report back in a couple of weeks…

Installing Arch Linux as a VirtualBox Guest

Arch Linux is probably the first Linux distro I’ve come across that does not have a graphical installer. It boots from the iso and drops you straight into a shell.

Ok. Once you’ve realized this then the install instructions make more sense.

To install in VirtualBox I created an 8GB disk. Once booted from the iso, at the shell I used fdisk to partition 2 partitions, one 6GB for / and one 2 GB for /home, following the steps from this post.

In summary, the steps were:

  • p – create primary
  • 1 – 1st partition
  • enter for start position default
  • +6G for end point 6GB from start
  • p next primary
  • 2 – 2nd partition
  • enter for default start
  • enter for end at end of available space

p shows the created partitions, which ended up looking like this:

w to write the partitions and exit.

Next format the two partitions:

mkfs.ext4 /dev/sda1

mkfs.ext4 /dev/sda2

Mount and start the install!

At this point, pick up from the remainder of the instructions in the install guide and beginners guide.

When attempting to install grub, I got these errors:

Per this post (and here), looks like my repo databases needed to be created/updated? I ran

pacman -Syu

and this looks like it fixed my pacman database issue, but now at this point it looked like I’d ran out of space on /, but going back through the install docs, I didn’t do the

arch-chroot /mnt /bin/bash

step so looks like I was installing to the / on my iso live boot? Anyway, did arch-chroot,
and now re-running the command to install grub now worked.
Next steps:

grub-install --recheck /dev/sda
grub-mkconfig -o /boot/grub/grub.cfg

Remaining steps:

  • Set root’s password: passwd
  • Exit chroot
  • Unount drives:
    umount -R /mnt
  • reboot

Remove the iso in VirtualBox, and restart – whoah, Arch is installed! Now time to install X and a window manager! Next steps depending on what you intend to use Arch for are covered in the general recommendations guide.

Create a user:

useradd -m -G wheel -s /bin/bash username

Use password username to set password.

Network config

Network setup guide is awesome!

Add name servers to /etc/resolv.conf (e.g. for Google nameservers)

nameserver 8.8.8.8
nameserver 8.8.4.4

Start and enable dhcp services to start at boot:

systemctl start systemd-networkd.service
systemctl enable systemd-networkd.service
systemctl start systemd-resolved.service
systemctl enable systemd-resolved.service

Check adapters: ip link – get name of VirtualBox adapter, will be something like enp0s3

Edit /etc/systemd/network/wired.conf, add:

[Match]
Name=enp0s3
[Network]
DHCP=ipv4

Start and enable dhcpcd.service:

systemctl start dhcpcd@enp0s3.service
systemctl enable dhcpcd@enp0s3.service

… where enp0s3 is your VirtualBox network interface.

At this point you should have network connectivity – check by pinging www.google.com