This project sets up a development environment using Vagrant and Docker for developing Java apps. We're assuming that you have an IDE installed on your laptop or PC.
Update The project is now integrated with Travis CI to build the docker image and test it once a commit is detected.
So the step of packaging the app is taken out of the docker build. Once a commit is made, the build job executes mvn package
and copies the package to the docker image as a part of the testing.
If you want the build to be done in the Docker image, use this release.
- Download and install VirtualBox
- Download and install Vagrant
- Download and extract the latest release of this project
- Change your path to the project directory (
cd hello-dropwizard
) - If you're in windows extract the zip package and navigate to the dir
hello-dropwizard
- Linux or Mac: Execute
setupdev.sh
- Windows: Execute
setupdev-windows.bat
- This script installs the
vagrant-vbguest
plugin - keeps the VM's guest-additions up-to-date. - It runs
export VAGRANT_CWD=./vagrants
orSET VAGRANT_CWD=./vagrants
to set the path for detecting the Vagrantfile. - It then runs the
vagrant up
command to spin up the VM.
- This script installs the
- Once the VM is created run
vagrant ssh
to login to the VM. - The project directory will be mounted as
/vagrant
- Configuration for the VM can be edited by modifying the values in
vagrants/vmconfig.yml
Note: The first run will take time as it will download the base VM image and installs the packages. Subsequent runs will be quick.
- Run
vagrant ssh
and navigate to the project directory in the VMcd /vagrant
- If this is the first run then the docker image has to be created for the app.
- Run
docker build -t dropwiz --build-arg artifactid=hello-dropwizard --build-arg artver=1.0 .
- This uses the
Dockerfile
to build the image for our app. artifactid
andartver
are the arguments it uses to create the application jar filehello-dropwizard-1.0-SNAPSHOT.jar
- Feel free to change these values to build newer image versions.
- Run
- Run
docker images
to list and verify that the image is created - Run
docker-compose up -d
to spin up the docker containers.- This will use the
docker-compose.yml
file to create servicesdropwiz
andngproxy
using the app image we just created and nginx image.
- This will use the
vagrant@devhost:/vagrant$ docker-compose up -d
Creating network "vagrant_proxy-bridge" with the default driver
Pulling ngproxy (nginx:latest)...
latest: Pulling from library/nginx
27833a3ba0a5: Pull complete
ea005e36e544: Pull complete
d172c7f0578d: Pull complete
Digest: sha256:e71b1bf4281f25533cf15e6e5f9be4dac74d2328152edf7ecde23abc54e16c1c
Status: Downloaded newer image for nginx:latest
Creating nginx ...
Creating dropwiz ...
Creating nginx
Creating nginx ... done
vagrant@devhost:/vagrant$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
cba79308c8ef nginx "nginx -g 'daemon of…" About a minute ago Up About a minute 0.0.0.0:80->80/tcp nginx
d3c10118b3f2 dropwiz "sh -c '/usr/bin/jav…" About a minute ago Up About a minute 8080-8081/tcp dropwiz
vagrant@devhost:/vagrant$
- Run
docker-compose down
to stop the containers.
- If you intend to destroy the VM and recreate it or share the app locally without uploading to a repository, then you can save the app image.
- Navigate to
/vagrant
in the VM and rundocker save dropwiz > dropwiz-v1.0.tar
to save the image. - The image will be saved as /vagrant/dropwiz-v1.0.tar on the host system.
- This can then be shared and loaded on another VM using
docker load < dropwiz-v1.0.tar
Run export VAGRANT_CWD=./vagrants
or SET VAGRANT_CWD=./vagrants
if your host is linux/mac or windows respectively.
- To shutdown the OS and stop the VM run
vagrant halt
. Runvagrant up
to start. - If you want something similar to hibernate in windows, then just run
vagrant suspend
- If you want to completely remove the VM and recreate it then run
vagrant destroy
Ctrl+D
to exit the VM to your host machine- Run
vagrant destroy
to destroy this VM - Run
setupdev.sh
orsetupdev-windows.sh
to start all over again.
- Configuration for the VM can be edited by modifying the values in
vagrants/vmconfig.yml
- We're using Ansible to install the required packages. Edit
ansible/roles/setup/defaults/main.yml
to add or remove packages. - We've provided two arguments
artifactid
andartver
in theDockerfile
to create the application jar filehello-dropwizard-1.0-SNAPSHOT.jar
- They can be modified when executing the
docker build..
command to build a different image, for example adropwizv1.5
- An environment variable
artifact=${artifactid}-${artver}-SNAPSHOT.jar
is used in the Dockerfile
- They can be modified when executing the