docker+java
TRANSCRIPT
Docker + Java
Elek Márton@anzix
2015 MarchDPC Consulting
Docker+Java
What is docker● Lightweight virtualization
– Container● Repository of virtual images
– 100 official image– ~45000 public images
● Versioning support– To upgrade a container
● Only for linux– Just a filesystem, the kernel of the host system is used– Windows out of scope
● Similar to– BSD jails, Linux LXC, Solaris zones
Docker run
Docker run
Docker run
Two main use case Start a predefined container
docker run jenkins
Start a command in a well defined envdocker run ubuntu uname -a
docker run ubuntu bash
Volumes● Every container is a one time container.
– All of the files will be deleted together with the container● What about persisted data?
– Solution: volumes
docker run -v /root/jenkins:/var/jenkins_home \ jenkins
● How to access?– By default docker containers use internal unique IP– An additional process forwards ports
● Port forwards should be defined
docker run -v /root/jenkins:/var/jenkins_home \ -p 4567:8080 jenkins
Port forward
Create a docker container● Snapshot an existing container
– Easy but non reproducible– docker commit
● Create step-by-step from an existing container– docker build .
FROM java:8ADD build/version /app/versionWORKDIR /appCMD ["bin/start.sh"]
● Usually one process
“In almost all cases, you should only run a single process in a single container.
Decoupling applications into multiple containers makes it much easier to scale horizontally and reuse containers. If that service depends on another service, make use of container linking.”
Docker best practices
What is inside a container
Docker ps
Upload to a repository● docker push/pull● Very similar to the git concept
– Versions– Tagging
docker imagesdocker tag 8dbd9e392a dpc.hu:5000/backenddocker push dpc.hu:5000/ubuntu
Repository servers:● docker● artifactory
Docker definition“Docker is an open platform for developers and sysadmins to build, ship, and run distributed applications.
Consisting of Docker Engine, a portable,● lightweight runtime and ● packaging tool, and ● Docker Hub, a cloud service for sharing
applications and automating workflows”
Docker definition“… Docker enables apps to be quickly assembled from components● and eliminates the friction between● development, QA, and production environments.”
“As a result, IT can ship faster and run the same app, unchanged, on laptops, data center VMs, and any cloud.”
What is missing● How to create an alias for a complex command line
– Docker compose (was fig)– Crane
● How to manage multiple machines all together/schedule containers– Kubernetes, OpenShift
● Hande logical group of containers together– Kubernetes, OpenShift
Docker is ecosystem● ContainerOS
– CoreOS, Project Atomic, Boot2Docker● Open source PaaS
– Flynn, Dokuu, Deis● Scheduler/Orchestration/Management
– Kubernetes, Crane, Docker Compose(fig)● Continuous Integration
– Jennkins plugins, …
See also https://www.mindmeister.com/389671722/docker-ecosystem
(Without) Docker composedocker run --name mysqldb
-e MYSQL_USER=mysql -e MYSQL_PASSWORD=mysql -e MYSQL_DATABASE=sample -e MYSQL_ROOT_PASSWORD=supersecret -d mysql
docker run --name mywildfly --link mysqldb:db -p 8080:8080 -d arungupta/wildfly-mysql-javaee7
Docker composemysqldb: image: mysql:latest environment: MYSQL_DATABASE: sample MYSQL_USER: mysql MYSQL_PASSWORD: mysql MYSQL_ROOT_PASSWORD: supersecretmywildfly: image: arungupta/wildfly-mysql-javaee7 links: - mysqldb:db ports: - 8080:8080
Use it from Windows/OSX/...● Boot2docker
– Minimal virtual container (Virtualbox)– Tools to run containers in the Virtualbox
● Docker Machine installer– Windows/Linux/OSX
Who uses it?● All of the big players:● Amazon Beanstalk
– Dedicated container based hosting● Google Cloud Platform
– Containers on Google Cloud Platform powered by kubernetes
● RedHat OpenShift v3 – The new version is based on kubernetes/docker
● HP– Operations Orchestration
And others...
Docker+Java
Example standalone appDockerfileFROM java:8WORKDIR /myappCMD ["bin/myproj"]COPY build/install/myproj /myapp
mvn install/gradle packagedocker build .
//upload to the repository with docker push
docker pull dpc.hu:5000/backenddocker run dpc.hu:5000/backend
Example J2EE applicationFROM jboss/wildfly
RUN add-user.sh admin Admin#70365 --silent
CMD ["/opt/jboss/wildfly/bin/standalone.sh", "-b", "0.0.0.0", "-bmanagement", "0.0.0.0"]
COPY target/helloworld.war /opt/jboss/wildfly/standalone/deployments/
Recipes
● Arun Gupta:9 Docker recipes for Java EE Applications– Use Docker Machine– Link local mysql and wildfly together– Link remote containers– Deploy Java EE Application from Eclipse
Maven/SBT/Gradledocker plugin
Artifactory
Continuous Deployment
See: Delivery pipline and zero downtime
Arquilian Cube (1.0.0 Alpha4)“With this extension you can start a Docker container with a server installed, deploy the required deployable file within it and execute Arquillian tests.
The key point here is that if Docker is used as deployable platform in production, your tests are executed in a the same container as it will be in production, so your tests are even more real than before.
But it also lets you start a container with every required service like database, mail server, … and instead of stubbing or using fake objects your tests can use real servers.”
https://github.com/arquillian/arquillian-cube
Summary● Container technology is the present
– lightweight virtualization– packaging– repository
● Reusable components– Continuous delivery– Use exactly the same environment– limit resources– Easy to create test containers
● Growing ecosystem– Supported solutions from the biggest vendors to the
smallest startup