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
A Linux OS with Docker Engine. This may be running remotely, within VMware Fusion/Workstation, Docker Machine, etc…
It is recommended to secure your Docker Daemon for listening on a secure TCP Port:
Follow the Protect the Docker Daemon Guide for creating the relevant certificates, you do not need to perform the “Secure by Default” section
You should now have all the certificates and keys required to setup secure access to Docker from your host machine
Prep-work
From your host machine get the IP address of your Photon instance:
appcatalyst guest getip <replace with vm name>
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/
SSH to your Photon instance from your host machine:
ssh -i /opt/vmware/appcatalyst/etc/appcatalyst_insecure_ssh_key photon@192.168.135.128
Optional: Move your server side certificates to a more appropriate folder:
sudo cp ~/{ca.pem,server-cert.pem,server-key.pem} /etc/ssl/certs/
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
Find the following line:
ExecStart=/bin/docker -d -s overlay
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
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
- Setup the Docker plug-in:
- Navigate to
IntelliJ IDEA
->Preferences
->Build, Execution, Deployment
->Clouds
- Select the
+
symbol and addDocker
- Specify name as
Docker
- Specify the Docker API URL:
https://192.168.135.128:2376
- Specify the
Certificates Folder
where you copied your client certificates to with SCP and hitOK
.
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 theDocker-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/
Your deployment artifacts will need to be placed in the same folder as your
Dockerfile
. Navigate toFile
->Project Structure
Make sure your
JavaEEHelloWorld_war.war
artifact is selected and set your output directory to theDocker-out
directory and hitOK
- Now the project
Run Configuration
needs to be configured for the deployment: - Navigate to
Run
->Edit Configurations
, select+
and add a newDocker Deployment
- Specify the name as
Docker Deployment
- Ensure Deployment specifies
Docker-Out/Dockerfile
- Choose a name for the container name, e.g.
My-WebApp
- Next to
Container Settings
click the Green Arrow symbol and create a folder within your project folder labeledDocker-Settings
- In the file-name box ‘container-settings.json’ should already be specified, hit
OK
- Next to ‘Before Launch’ click the
+
symbol and selectBuild Artifacts
.
- Choose your artifact and hit
OK
to both windows - 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:
In your Web browser, navigate to
http://192.168.135.128:18080/JavaEEHelloWorld_war/
to see your WebApp successfully runningSSH 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!