Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Automated Building and Deployment of Docker Microservices
[http://sebworks.com](http://sebworks.com)
#!/bin/bash
# author: seb@sebworks.com
# stop on any error
set -e
# app is the first parameter
app="$1"
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
#fi
# clean up
cd ~
rm -rf ~/build/$app-$NOW
FROM frolvlad/alpine-oraclejdk8:slim
VOLUME /tmp
EXPOSE 8080
ADD myapp.war app.jar
RUN sh -c 'touch /app.jar'
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
// node.js script to start build process
// author: seb@sebworks.com
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 = query.app;
if(app == undefined){
response.writeHead(400, 'Bad request');
response.end("No 'app' parameter");
return;
}
console.log('App: '+app);
var child = spawn('/home/division/build.sh', [app]);
child.stdout.on('data', function (data) {
process.stdout.write(data.toString());
response.write(data);
});
child.stderr.on('data', function (data) {
process.stdout.write(data.toString());
response.write(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);
});
...
<build>
<finalName>myapp</finalName>
<plugins>
...
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.2.3</version>
<configuration>
<imageName>localhost:5000/${project.artifactId}</imageName>
<dockerDirectory>src/main/docker</dockerDirectory>
<imageTags>
<imageTag>latest</imageTag>
</imageTags>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.${project.packaging}</include>
</resource>
</resources>
</configuration>
</plugin>
...
</plugins>
</build>
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.