Often times you may have a need to spin up multiple machines to test out an application you are developing. Frequently this can lead you to install all manner of software and frameworks on your local laptop. Overtime this may lead to application conflicts or just a plain mess. With IntelliJ IDEA and Docker, it’s now quite easy to deploy your application into a Docker container or containers directly from your IDE with the click of a button.

System Requirements

  • IntelliJ IDEA Ultimate Edition: 14.1 or newer

Pre-requisites

  1. A Linux OS with Docker Engine. This may be running remotely, within VMware Fusion/Workstation, Docker Machine, etc…

  2. It is recommended to secure your Docker Daemon for listening on a secure TCP Port:

  3. Follow the Protect the Docker Daemon Guide for creating the relevant certificates, you do not need to perform the “Secure by Default” section

  4. You should now have all the certificates and keys required to setup secure access to Docker from your host machine

Prep-work

  1. From your host machine get the IP address of your Photon instance:

    appcatalyst guest getip <replace with vm name>
    
  2. Copy the client certificates from your Docker Host to your preferred folder on your development machine, in this case: /home/DockerCerts/:

    scp username@192.168.135.128:"ca.pem cert.pem ca.pem key.pem" ~/DockerCerts/
    
  3. SSH to your Photon instance from your host machine:

    ssh -i /opt/vmware/appcatalyst/etc/appcatalyst_insecure_ssh_key photon@192.168.135.128
    
  4. Optional: Move your server side certificates to a more appropriate folder:

    sudo cp ~/{ca.pem,server-cert.pem,server-key.pem} /etc/ssl/certs/
    
  5. The Docker service in Photon is configured through SystemD. From your Photon SSH connection, modify your service file to change from Unix Socket to a secure TCP connection:

    sudo nano /lib/systemd/system/docker.service
    
  6. Find the following line:

    ExecStart=/bin/docker -d -s overlay
    
  7. Replace it with the following line:

    ExecStart=/bin/docker -d -s overlay --tlsverify --tlscacert=/etc/ssl/certs/ca.pem --tlscert=/etc/ssl/certs/server-cert.pem --tlskey=/etc/ssl/certs/server-key.pem -H=0.0.0.0:2376
    
  8. Reboot your Photon instance or reload and restart Docker using systemctl:

    sudo shutdown -r now
    

    Or

    sudo systemctl daemon-reload
    sudo systemctl restart docker
    

IntelliJ IDEA Configuration

For this example, a simple Java Enterprise Project is used with the Web Application framework deployed as a Web Application Archive. Please feel free to create a new project, use an existing one, or follow the steps in the tutorial from JetBrains: https://www.jetbrains.com/idea/help/developing-a-java-ee-application.html

  1. Setup the Docker plug-in:
  2. Navigate to IntelliJ IDEA -> Preferences -> Build, Execution, Deployment -> Clouds
  3. Select the + symbol and add Docker
  4. Specify name as Docker
  5. Specify the Docker API URL: https://192.168.135.128:2376
  6. Specify the Certificates Folder where you copied your client certificates to with SCP and hit OK.

IntelliJ IDEA Cloud Pref

  1. Configure your project specific settings: 3. Create a folder under your project root folder named Docker-out 3. You will need to create a Dockerfile to contain the commands which are issued to Docker during image build. Within the Docker-out directory, create a new file called ‘Dockerfile’ and enter the following commands. The first line specifies the base image and the second species the artifact you wish to add to the new image:

    FROM jboss/wildfly
    ADD JavaEEHelloWorld_war.war /opt/jboss/wildfly/standalone/deployments/
    
  2. Your deployment artifacts will need to be placed in the same folder as your Dockerfile. Navigate to File -> Project Structure

  3. Make sure your JavaEEHelloWorld_war.war artifact is selected and set your output directory to the Docker-out directory and hit OK

IntelliJ IDEA Project Folders

  1. Now the project Run Configuration needs to be configured for the deployment:
  2. Navigate to Run -> Edit Configurations, select + and add a new Docker Deployment
  3. Specify the name as Docker Deployment
  4. Ensure Deployment specifies Docker-Out/Dockerfile
  5. Choose a name for the container name, e.g. My-WebApp
  6. Next to Container Settings click the Green Arrow symbol and create a folder within your project folder labeled Docker-Settings
  7. In the file-name box ‘container-settings.json’ should already be specified, hit OK
  8. Next to ‘Before Launch’ click the + symbol and select Build Artifacts.

IntelliJ IDEA Project Run Config

  1. Choose your artifact and hit OK to both windows
  2. Finally navigate to Run -> Run Docker Deploy

IntelliJ IDEA will now build your artifact, communicate to your Photon hosted Docker service, start up a new Container, and deploy your artifact to the container. If this is the first time you have pulled down the Docker base image then it may take 5-15 minutes to download depending on your internet connection.

IntelliJ IDEA Post Deployment Test

To test a successful build and deploy from IntelliJ IDEA to your Docker container you can try one of the following steps:

  1. In your Web browser, navigate to http://192.168.135.128:18080/JavaEEHelloWorld_war/ to see your WebApp successfully running

  2. SSH to your Photon Instance and issue the Docker command to list running containers. You should see “My-WebApp” under the Names column:

    ssh -i /opt/vmware/appcatalyst/etc/appcatalyst_insecure_ssh_key photon@<insert guest ip here>
    sudo docker --tlsverify --tlscacert=ca.pem --tlscert=cert.pem --tlskey=key.pem -H=<insert guest ip here>:2376 ps
    

What’s Next

So now you’ve configured a basic project within your IDE to work with VMware AppCatalyst, Photon, and Docker containers. You can now look to implement these settings within your own project work and deploy more complex environments with the click of a button!

Appendix