Posted on Jul 5th 2016
Why Do I Care?
If you’re not deploying in containers yet, you likely will be soon because complex applications are moving to containerization technologies—like Docker—for ease of deployment, simplified management, increased reliability, platform portability and microservices support. This overview gives you the basics of how to adapt your development process in a simple way to support this rapidly approaching reality.
Who’s This Overview For?
This is an introductory overview that only assumes you’ve got some prior experience with Java web application development. You’ll learn just enough to be dangerous with MyEclipse and Docker.
What Are We Going to Do (and Not Do)?
In this overview we’re going to:
- Use MyEclipse to build a web application for Tomcat 7.
- Construct a Docker Tomcat 7 container for our application.
- Launch the Docker container and run our application within it.
However, we’re not going to use any of the Docker plugins available for Eclipse. Because instead of running wizards, filling in forms, pushing buttons, and hoping things work, we’re actually going to learn some key principles behind container-based development by using the command line so we can build a solid foundation of knowledge.
To use the command line we’ll need a thorough understanding of what we’re doing and we’ll have the flexibility to do anything we wish. On the other hand, good tools reduce the knowledge required to do certain tasks but they also limit flexibility. It’s always a trade-off. But at the end of this tutorial, once we fully understand the terminology, concepts, capabilities, and advantages of using Docker containers, we’ll then be able to intelligently decide which (if any) Docker plugins we’d like to test in order to facilitate a portion of the development process.
How Do I Install MyEclipse?
Here’s the download link for the latest MyEclipse release. It has a free 30-day trial so just run the installer and you are all set.
How Do I Install Docker?
The folks at Docker have done a great job making it as simple as possible. Here’s a link to the toolbox download.
For either Mac or Windows there’s a nice double-click installer. Download and run all the steps as well as verify/test your installations as described here: (Mac, Windows).
Please don’t move forward until you have both: seen the “whale” icon and successfully run the “hello-world” container in step 4 of the installation guide.
What Does Our Application Look Like?
We’re going to be aggressively productive and use one of the many examples available in MyEclipse. Specifically, the SpringShoppingCartExample.
Installation of the SpringShoppingCartExample is simple:
- Launch MyEclipse.
- Select MyEclipse>Examples On-Demand. The Example tab opens on the MyEclipse Dashboard.
- Select the Spring tab and then click Install from the Spring Shopping Cart Example.
- Once the installation completes, the SpringShoppingCartExample project is added to your workspace.
Now that we have a Tomcat application, let’s see how we can create a Docker Tomcat 7 container to run it in.
How Do I Create a Docker Tomcat 7 Container for My Application?
To run the SpringShoppingCartExample we’re going to need a Tomcat 7 Docker container. This is easy to set up because DockerHub contains a large library of curated containers for all different types of server configurations, including Tomcat 7.
Creating a Dockerfile for the Project
Since we’re associating this container with our project and want to share the configuration with the rest of our team, let’s create a Dockerfile that describes the Tomcat 7 Docker container within the project:
- Right-click on SpringShoppingCartExample in the MyEclipse Explorer View and select New>Folder. Name the folder DockerResources.
- Rick-click on DockerResources and select New>File. Name the file Dockerfile
(exact name and case is important).
- Within Dockerfile add these two lines:
# We'll be using the curated Tomcat 7 container from DockerHub
Now your workspace looks like this:
Setting up the Terminal for Docker Commands
Using Docker containers means we’re going to be running a lot of command lines directly in the shell and we can do this easily from within MyEclipse with the Terminal view. But before we launch a terminal we’ll need to set one command line argument for it.
- Open the application preferences, select Terminal>Local Terminal and in arguments type -l (that’s a dash-el). This ensures the shells you launch in the Terminal view behave like a login shell and get the correct execution path so all the Docker toolbox commands can be found.
- Now we’re going to open a Terminal with the working directory of the DockerResources folder we created earlier. Right-click on DockerResources and select Show In>Terminal.
- Within this new shell we need to run a couple of commands. The
eval command will set up the environment variables so our Docker commands will work and the
docker version command will simply show what that environment looks like as a test.
$ eval $(docker-machine env default)
$ docker version
API version: 1.23
Go version: go1.5.4
Git commit: 5604cbe
Built: Tue Apr 26 23:44:17 2016
API version: 1.23
Go version: go1.5.4
Git commit: 5604cbe
Built: Wed Apr 27 00:34:20 2016
Testing Our Tomcat 7 Docker Container
Now that we have a Dockerfile and the terminal is ready to run Docker commands, we need to build an image that describes our Tomcat 7 Docker Container—based on the instructions in the Dockerfile documentation. You can think of an image as a detailed definition for a container, akin to how a class is the detailed definition for an instantiated object.
Using the command line let’s build our container image, put it in a local repository named myeclipse, name the image examples, and create a version with the tag run. This way we can easily refer to it when we run it later.
$ docker build -t myeclipse/examples:run .
Sending build context to Docker daemon 2.048 kB
Step 1 : FROM tomcat:7
Successfully built 705d4a88eedc
How Do I Run Docker Container?
Now that we have our Tomcat 7 Docker Container defined, running it is only a command line away. In the terminal we created earlier, we can run the following command to launch a container based on our new image. This command tells Docker to run the container as a daemon (-d) and to map port 8080—the default Tomcat port—within the container to port 8080 on the Docker host which is the virtual machine that runs all the Docker containers. Docker responds by showing us the internal ID of our running container.
$ docker run -d -p 8080:8080 myeclipse/examples:run
Just in case you want other status than the container ID, you can run the ps command to get a more detailed status for our running container. Note that in the status our container was also given the name “cranky_mcnulty” and we can use this instead of the ID to refer to the container in any Docker commands (you may need to hover your mouse over the following example and scroll to the right to see the NAMES column). Finally, in the PORTS column we have confirmation that the mapping of port 8080 took effect as well.
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
12b15728e8ad myeclipse/examples:run "catalina.sh run" 9 seconds ago Up 7 seconds 0.0.0.0:8080->8080/tcp cranky_mcnulty
So now we know we need to access port 8080 on the Docker host’s IP to see Tomcat running in the container we just launched. But what’s the Docker host’s IP? Let’s again turn to the command line to determine that:
$ docker-machine env
Currently, my Docker host is running on 192.168.99.100; so if I open a web browser in MyEclipse (little globe icon on the menu bar) and then navigate to http://192.168.99.100:8080 in the browser I get the default Tomcat 7 welcome screen. Of course the Docker host will be running at a different IP on your computer, but if you followed along correctly you now see something like this:
Now that we’ve seen our Tomcat 7 Docker container actually run, it’s time to shut it down and figure out how to load it with our application. Shutting it down is also done via the command line by ID or name. We can then make sure it’s down, in case you don’t believe Docker, by running the ps command again:
$ docker stop cranky_mcnulty
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
How Do I Run My Application in the Docker Container?
There are actually a number of ways to load our Tomcat application into the Docker container we just created. Since we want to mimic how our application will run in the container in a production environment, the key is to ensure the end result of our chosen deployment method for our development environment results in a container that looks exactly like the end result of our production deployment, even though the mechanisms will likely be quite different.
In a production environment you can create an automated build process for your Docker container that includes your application. Details on how to do this vary with goals and tools used, but a good overview of the process is provided in the Docker documentation. Since we’re interested in rapid development turnaround, we’re not going to do that here. We’re going to go a much easier route that still results in an identical container being created.
As a reminder, a correct deployment of our SpringShoppingCartExample requires that the application be built, assembled and then placed into the Tomcat server’s webapps directory. While a fancy automated build process will accomplish that, MyEclipse already knows how to create a correct deployment so we’re going to leverage that ability and combine it with a simple volume mapping that places our deployment into the correct directory within the Docker container. The steps look like this:
- Configure MyEclipse to deploy SpringShoppingCartExample to a convenient location on the local file system.
- Launch our Tomcat 7 Docker container and map the local deployment location to <tomcat-install-dir>/webapps/SpringShoppingCartExample within the container.
Configuring a Deployment of SpringShoppingCartExample
Since we’ll be managing the lifecycle of the Docker container ourselves, we’ll need to configure our deployment of SpringShoppingCartExample for use with an external server. Fortunately, MyEclipse has great support for external servers so this is as easy as following a few simple steps and answering some questions in the wizard dialogs.
- To create a new deployment, right-click on the project name in the MyEclipse Explorer and select MyEclipse>Add and Remove Project Deployments…
- From the Manage Deployments dialog, click Add… to select a new server type for our deployment.
- In the Deploy Modules dialog, select the Manually define a new server radio button and under the Basic category select Externally Launched Server. Finally, in the Server name field specify Docker Tomcat 7 Container and click the Next button.
- Now we need to tell MyEclipse the location on disk that we’d like the deployment to be written since there’s no set location for externally launched servers. For our purposes, any location that can be easily shared with the Docker container is fine. The Docker installation does a bit of virtual machine configuration that makes sharing any location under your home directory easy, so we’ll pick <UserHomeDir>/Applications/DockerDeployments and then click Finish.
- When we look at the Servers view we can see a new server, Tomcat 7 Docker Container, has been created and it has one deployment, SpringShoppingCartExample, as intended. However, there is one more bit of housekeeping we need to finish. Since we’re using an externally launched server, MyEclipse doesn’t know when it is most appropriate to deploy the application. So we’re going to right click on the deployment line and select Publish, effectively saying “right now is a good time to deploy.”
- After the Publish operation is complete, the Status will change to Synchronized. Additionally, if we hover the mouse on the deployment line, a popup confirms that the application is now deployed to the exact location we specified. And from now on MyEclipse will keep that location in sync with all the changes we make to the code in our workspace, which will make testing changes in the Docker container extremely simple.
Launching the Docker Container with Our Deployment
Now that we have a correct deployment for SpringShoppingCartExample written to disk, we need to launch our Docker container so that it maps our local deployment to the correct position in the file position of the container. This is easily accomplished using data volume mapping that is specified as part of the docker run command using the -v option. The command below is exactly like the one we used to launch the Docker container previously except now we’re specifying that the deployment directory of SpingShoppingCartExample be mapped to the correct file system location within the Docker container so that Tomcat will know the application is deployed. The full command looks like this:
docker run -v "/Users/todd/Applications/DockerDeployments/SpringShoppingCartExample":/usr/local/tomcat/webapps/SpringShoppingCartExample -p 8080:8080 -d myeclipse/examples:run
Once we’ve started the server with the run command, we can now use the internal MyEclipse Web Browser to navigate to http://192.168.99.100:8080/SpringShoppingCartExample/ and we’ll see that the Tomcat 7 Docker container is indeed running the SpringShoppingCartExample. We can also run another
docker ps command to see the name our container was assigned so we can shut it down as before, except using the name “angry_mestorf” instead. A screenshot of a successful run looks like this:
Final Closing Tip
Now that you are familiar with some key Docker commands, how about a shortcut? MyEclipse 2016 CI 4 introduced the ability to drag and drop often used Docker commands from the Snippets view to the Terminal—a real timesaver!
I hope this has been a useful introduction into Docker containers. And now that we can run SpringShoppingCartExample in the Docker container, I bet you’re thinking it would be really great to be able to debug it easily. Well, we can! I give you the details in my next blog post. If you’re not already subscribing to our blogs, why not subscribe now so you’ll be notified as soon as new blogs are available?
If you have any questions or feedback, I’d love to hear from you. Hit us up on twitter at @MyEclipseIDE or on the MyEclipse forum.