JavaEE Development Using Docker With MyEclipse
As the popularity of Docker and containers continues to grow, see how you can incorporate them into the enterprise.
Join the DZone community and get the full member experience.Join For Free
Section 1: About Docker
Complex applications are moving to containerization technologies — like Docker — for ease of deployment, simplified management, increased reliability, platform portability and microservices support, so if you’re not deploying in containers yet, you likely will be soon. Docker containers wrap a piece of software in a complete filesystem that contains everything needed to run: code, runtime, system tools, system libraries – anything that can be installed on a server.
Section 2: Installing Docker
The Docker Toolbox
Note: For either Mac or Windows, there’s a double-click installer. Download and run all the steps as well as verify/test your installations as described here: (Mac, Windows). Please ensure that “whale” icon is visible and “hello-world” container is run successfully in step 4 of the installation guide.
Section 3: Installing MyEclipse
Latest MyEclipse installer. (It has a free 30-day trial)
Section 4: Building a Sample Web App
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.
Section 5: Creating a Docker Tomcat 7 Container
1. Creating a Dockerfile for 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
Your workspace should look like this:
2. Setting up the Terminal for Docker Commands
Using Docker containers involves running a lot of command lines directly in the shell and which can be done easily from within MyEclipse with the Terminal view as detailed below.
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.
Open a Terminal with the working directory of the DockerResources folder created earlier. Right-click on DockerResources and select Show In>Terminal.
The eval command will set up the environment variables so 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 Client: Version: 1.11.1 API version: 1.23 Go version: go1.5.4 Git commit: 5604cbe Built: Tue Apr 26 23:44:17 2016 OS/Arch: darwin/amd64
Server: Version: 1.11.1 API version: 1.23 Go version: go1.5.4 Git commit: 5604cbe Built: Wed Apr 27 00:34:20 2016 OS/Arch: linux/amd64
Section 6: Testing the Tomcat 7 Docker Container
Build an image that describes Tomcat 7 Docker Container — based on the instructions in the Dockerfile documentation using command line mentioned below.
Note: Image is a detailed definition for a container, akin to how a class is the detailed definition for an instantiated object.
The command lines will:
Build container image.
Put it in a local repository named MyEclipse
Name the image examples.
Create a version with the tag run.
$ docker build -t myeclipse/examples:run Sending build context to Docker daemon 2.048 kB Step 1 : FROM tomcat:7 ---> 705d4a88eedc Successfully built 705d4a88eedc
Section 7: Running the Docker Container
Run the following command in your terminal to launch a container based on a 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 the internal ID of our running container.
$ docker run -d -p 8080:8080 myeclipse/examples:run 12b15728e8adab5e49e484524a319f7349275f3165294161c3118220801ea7
Tip: Run the ps command to get a more detailed status for running container.
$ 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
The command below tells the Docker host’s IP:
$ docker-machine env export DOCKER_TLS_VERIFY="1" export DOCKER_HOST="tcp://192.168.99.100:2376" export DOCKER_CERT_PATH="/Users/todd/.docker/machine/machines/default" export DOCKER_MACHINE_NAME="default"
Shut down the Tomcat 7 Docker container
$ docker stop cranky_mcnulty
Section 8: Running a Web App in the Docker Container
1. Configuring a Deployment of SpringShoppingCartExample
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 to manually define a new server radio button
Under the Basic category, select Externally Launched Server.
Finally, in the Server name field specify Docker Tomcat 7 Container and click the Next button.
The Docker installation does a bit of virtual machine configuration that makes sharing any location under your home directory easy, <UserHomeDir>/Applications/DockerDeployments and then click Finish.
The status will change to Synchronized. Hover the mouse on the deployment line, a popup confirms that the application is now deployed to the exact location specified. MyEclipse keeps that location in sync with all the changes made to the code in workspace, which makes testing changes in the Docker container extremely simple.
2. Launching the Docker Container With Our Deployment
We need to launch Docker container so that it maps local deployment to the correct position in the file position of the container. This is 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 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
Use the internal MyEclipse Web Browser to navigate to http://192.168.99.100:8080/SpringShoppingCartExample/ and see that the Tomcat 7 Docker container is running the SpringShoppingCartExample. 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.
Published at DZone with permission of Silvia Rojas, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.