Docker build machine for Maven/Java

Here’s a quick-ish way to create a Docker container which downloads and compiles the SimpleDropWizardEcho service I wrote for testing.

But why would you do this? As discussed in my previous post, this creates a clean environment with no state held, so the fatjar is built cleanly, with no cached M2 artifacts which might cause you hidden problems. And if you split up the dockerfile into two parts – one to build an image which contains a JDK and Maven, and then a second dockerfile based on that image which downloads and compiles the code, then you’d have a much quicker method than using a new virtual machine.

Anyway – to do this…

wget https://raw.githubusercontent.com/devsoup/DockerJavaMavenBuildBox/v1.0.1/build.sh
./build.sh

This downloads my simple shellscript which runs creates the dockerfile, then calls Docker to build a container from it.

The dockerfile downloads the OpenJDK 1.7.0, and Maven 3.1.1, and then downloads my project and calls mvn package on it. When it’s finished, if you run the command to interactively connect to the box, then you’ll find the fat jar in /var/tmpbuild/SimpleDropWizardEcho-develop/target.

The build script contents are

#/bin/bash
 
# Remove old instances
docker rm `docker ps --no-trunc -a -q`
docker rmi devsoup/centos-jdk7
 
# Create the dockerfile we'll use in a moment to build a new Docker container
cat > build.dockerfile << EOF
# Set the base image
FROM centos
 
# Install OpenJDK
RUN yum -y install java-1.7.0-openjdk-devel
RUN echo export JAVA_HOME=/usr/lib/jvm/jre-1.7.0-openjdk.x86_64 > /etc/profile.d/java.sh
 
# Install Maven
RUN yum -y install wget
RUN wget http://mirror.cc.columbia.edu/pub/software/apache/maven/maven-3/3.1.1/binaries/apache-maven-3.1.1-bin.tar.gz
RUN yum -y install tar
RUN tar xzf apache-maven-3.1.1-bin.tar.gz -C /usr/local
WORKDIR /usr/local
RUN ln -s apache-maven-3.1.1 maven
WORKDIR /
RUN echo export M2_HOME=/usr/local/maven > /etc/profile.d/maven.sh
RUN echo export PATH=\${M2_HOME}/bin:\${PATH} >> /etc/profile.d/maven.sh
RUN echo source /etc/profile > ~/.profile
 
# Download the code to build
RUN yum -y install unzip
WORKDIR /var
RUN mkdir tmpbuild
WORKDIR /var/tmpbuild
RUN wget https://github.com/devsoup/SimpleDropWizardEcho/archive/develop.zip
RUN unzip develop.zip
 
# Build it
WORKDIR /var/tmpbuild/SimpleDropWizardEcho-develop
RUN /bin/bash -l mvn package
 
EOF
 
# Build the container
docker build -t devsoup/centos-jdk7 - < build.dockerfile
 
echo Try 'docker run -i -t devsoup/centos-jdk7 /bin/bash -l'

# Remove old instances
docker rm `docker ps –no-trunc -a -q`
docker rmi devsoup/centos-jdk7

# Create the dockerfile we'll use in a moment to build a new Docker container
cat > build.dockerfile << EOF
# Set the base image
FROM centos

# Install OpenJDK
RUN yum -y install java-1.7.0-openjdk-devel
RUN echo export JAVA_HOME=/usr/lib/jvm/jre-1.7.0-openjdk.x86_64 > /etc/profile.d/java.sh

# Install Maven
RUN yum -y install wget
RUN wget http://mirror.cc.columbia.edu/pub/software/apache/maven/maven-3/3.1.1/binaries/apache-maven-3.1.1-bin.tar.gz
RUN yum -y install tar
RUN tar xzf apache-maven-3.1.1-bin.tar.gz -C /usr/local
WORKDIR /usr/local
RUN ln -s apache-maven-3.1.1 maven
WORKDIR /
RUN echo export M2_HOME=/usr/local/maven > /etc/profile.d/maven.sh
RUN echo export PATH=\${M2_HOME}/bin:\${PATH} >> /etc/profile.d/maven.sh
RUN echo source /etc/profile > ~/.profile

# Download the code to build
RUN yum -y install unzip
WORKDIR /var
RUN mkdir tmpbuild
WORKDIR /var/tmpbuild
RUN wget https://github.com/devsoup/SimpleDropWizardEcho/archive/develop.zip
RUN unzip develop.zip

# Build it
WORKDIR /var/tmpbuild/SimpleDropWizardEcho-develop
RUN /bin/bash -l mvn package

EOF

# Build the container
docker build -t devsoup/centos-jdk7 – < build.dockerfile

echo Try 'docker run -i -t devsoup/centos-jdk7 /bin/bash -l'

and once built, we run the echo’d final command to interactively inspect our compilation.

mac:~ devsoup$  docker run -i -t devsoup/centos-jdk7 /bin/bash -l
bash-4.1# ls -al /var/tmpbuild/SimpleDropWizardEcho-develop/target/
total 9568
drwxr-xr-x 4 root root    4096 Jun  1 10:51 .
drwxr-xr-x 4 root root    4096 Jun  1 10:51 ..
drwxr-xr-x 3 root root    4096 Jun  1 10:50 classes
-rw-r--r-- 1 root root    1659 Jun  1 10:51 dependency-reduced-pom.xml
drwxr-xr-x 2 root root    4096 Jun  1 10:50 maven-archiver
-rw-r--r-- 1 root root    5906 Jun  1 10:50 original-SimpleDropWizardEcho-1.0-SNAPSHOT.jar
-rw-r--r-- 1 root root 9765101 Jun  1 10:51 SimpleDropWizardEcho-1.0-SNAPSHOT.jar
bash-4.1#

Running it requires port-forwarding to be set up first – so I’ll cover that in another post.

This is my personal blog - all views are my own.

Tagged with: , , , ,