Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Automated Building and Deployment of Docker Microservices
# author:
# stop on any error
set -e
# app is the first parameter
echo "app: $app"
NOW=$(date +"%Y%m%d-%H%M%S")
mkdir -p "~/build/$app-$NOW"
cd "~/build/$app-$NOW"
# it should be no password login. use ssh keys if necessary.
git clone --depth=1 "ssh://git@myserver/$app.git"
cd "$app"
# build app
mvn -DskipTests=true package
mvn docker:build
# docker image is ready
# stop current container
docker stop $app || true
# remove current container
docker rm -v $app || true
# remove current image
docker rmi localhost:5000/$app:current || true
# tag latest image as current
docker tag localhost:5000/$app:latest localhost:5000/$app:current
# run the container
docker run -P -d --name $app -e VIRTUAL_HOST=$app.$(hostname -f) localhost:5000/$app:latest
# OR, specialized scripts:
#if [ "$app" = "myapp" ]; then
#docker run -P -d --name $app --link mysql -e VIRTUAL_HOST=$app.$(hostname -f) localhost:5000/$app:latest
#elif [ "$app" = "myapp2" ]; then
#docker run -P -d --name $app --link mongo -e VIRTUAL_HOST=$app.$(hostname -f) localhost:5000/$app:latest
# clean up
cd ~
rm -rf ~/build/$app-$NOW
FROM frolvlad/alpine-oraclejdk8:slim
ADD myapp.war app.jar
RUN sh -c 'touch /app.jar'
ENTRYPOINT ["java","","-jar","/app.jar"]
// node.js script to start build process
// author:
const http = require('http');
const url = require('url');
const sys = require('util');
const exec = require('child_process').exec;
const spawn = require('child_process').spawn;
function handleRequest(request, response){
response.setHeader('Transfer-Encoding', 'chunked');
console.log('Received request. Url: ' + request.url);
var query = url.parse(request.url, true).query;
var app =;
if(app == undefined){
response.writeHead(400, 'Bad request');
response.end("No 'app' parameter");
console.log('App: '+app);
var child = spawn('/home/division/', [app]);
child.stdout.on('data', function (data) {
child.stderr.on('data', function (data) {
child.on('close', function (code) {
response.end('Finished. Exit code: '+code);
console.log('child process exited with code ' + code);
var server = http.createServer(handleRequest);
server.listen(PORT, function(){
console.log("Server listening on: http://localhost:%s", PORT);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment