We’ve decided to use docker containers to package and deploy application components as nano services. This post documents initial experimentation to build and run a sample application as a service.
The application is a spring integration stomp over websockets chat sample app. It is a java application running in an embedded tomcat container, packaged as a stanadalone (uber) jar.
This type of application seems to be a great fit for a docker container
- self-contained; includes dependencies
- provides a service
- consists of one process
- can be used by other services (or people but we’ll pretend it’s a messaging service for our applications)
install docker
I’m using OS X so I used the installation instructions found here. Be sure to do the boot2docker bit.
Use whatever is appropriate for your OS.
get application code
I’m using Spring integration samples from here. I cloned the git repo locally using
1
|
|
Modify the index.html a bit so the host:port are not hard-coded:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
|
run code locally
from ~/spring-integration-samples
directory run
1
|
|
using a browser point to http://localhost:8080/ and play with the app.
build uber jar
1
|
|
will build and test the app.
The file we will use is applications/stomp-chat/build/libs/stomp-chat-3.0.0.BUILD-SNAPSHOT.jar
build docker image
Make new empty directory for the Dockerfile and context (files destined for image) and copy the jar file there.
1 2 |
|
Dockerfile
create Dockerfile and populate with these lines:
1 2 3 4 |
|
Those instructions tell docker to
- use a base image that has java installed
- copy our jar file into the /app directory (creating the directory if needed)
- make port 8080 available for mapping
- execute the jar file when the container is started
create docker image
On OS X I need to point at my docker vm to communicate with the daemon. I can get the necessary environment variable exports using
1 2 3 4 5 6 7 |
|
Copy the export commands and paste in shell to execute.
Build the image
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
The -t switch tags the image with a name used later to reference the image.
Verify the image exists
1 2 3 |
|
create and a run docker container from the image
1 2 |
|
The run command returns the container id. We should now have a running container which has mapped it’s port 8080 to the vm port 8080 (that’s what the -p switch does). The -d switch tells the container to run detached. The –name switch gives the container a name we can use to reference it, besides the id.
We can verify the container is running with the docker ps command.
1 2 3 |
|
test app running in container
on my host the docker vm is at IP address 192.168.59.103
so I point my browser to http://192.168.59.103:8080
to test the app. Be sure to login from multiple browser pages as different people to get the full effect!