docker-compose.yml for 1x MongoDB container plus 1x data volume container

I’m starting to dabble with docker-compose to link up some container goodness. If you’ve followed my other Docker related posts the past couple of days (here), then you might have noticed that I have something Raspberry Pi related cooking 🙂

Although I’ll have to redo these with a Pi compatible base images, to get MongoDB running in one container with a separate data volume container is actually pretty simple. Here’s what I ended up with. To start it up, ‘docker-compose up -d’ and you’re up and running (copy this to docker-compose.yml):

mongodata: 
 image: mongo:3.2
 volumes:
 - /data/db
 entrypoint: /bin/bash
mongo: 
 image: mongo:3.2
 volumes_from:
 - mongodata
 ports:
 - "27017:27017"

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!

Docker typical workflow (rough notes)

I’m still scratching the surface getting up to speed with Docker, but here’s some rough notes for what seems to be a typical development workflow.

Build an image (from Dockerfile):

docker build -t imagename .

Run an image (create a container):

docker run -d -p port:port imagename

 

If pushing an image to a local Registry to share to other machines:

Tag an image:

docker tag imagename ip-of-registry:port/imagename

Push image to registry:

docker push ip-of-registry:port/imagename

For my other notes and experiences in Dockerland so far, see here.

Running a local insecure Docker Registry for testing

To start and run a local Repository for sharing images between machines (from here):

docker run -d -p 5000:5000 --restart=always --name registry registry:2

To tag a local image and push it into your repository:

docker tag imagename [ip of your docker machine]:5000/imagename

And then push it into your repo:

docker push [ip of your docker machine]:5000/imagename

By default, the docker client fails when attempting to push an image to a locally running registry if you haven’t configured TLS and certificates. If you’re running a registry locally for testing, you can configure the local client to trust an insecure Registry by:

  • docker-machine ssh [name of docker machine]
  • sudo vi /var/lib/boot2docker/profile
  • Add this line to the EXTRA_ARGS var:
--insecure-registry=[ip of your docker machine]:5000

This tip from this article.

Update1: if running on Linux (no docker-machine), add the above property to /etc/default/docker and then

sudo service docker restart

Update2: if docker is running as a service using systemd (e.g. the Hypriot Raspberry Pi image), you need to edit

/etc/systemd/system/docker.service

… append the –insecure-registry option to the end of the line that starts: ExecStart (further details here)

Now trying the push again, and success!

$ docker push 192.168.99.100:5000/spring-boot-rest-alpine

The push refers to a repository [192.168.99.100:5000/spring-boot-rest-alpine]

d5a685774b12: Pushed 

726baddb2cde: Pushed 

bb81ee534db3: Pushed 

77f08abee8bf: Pushed 

latest: digest: sha256:cafa45a64cf25533a1544f37ef7ee3fac614cb2a8d8be30efe9b3c84cebabc52 size: 1159