IntelliJ IDEA 2016.2 Help

Docker

You can add Docker support to IntelliJ IDEA by installing the Docker integration plugin.

Prerequisites

Make sure that the following prerequisites are met:

Overview of Docker support

The Docker integration plugin adds the following to IntelliJ IDEA:

  • Docker configurations. These are named sets of settings for accessing the Docker Remote API.
  • Docker Deployment run/debug configurations. They let you download and build Docker images, create and start Docker containers, and deploy your application artifacts into the containers. Application debugging is supported only for Java.
  • Docker tool window that lets you manage your Docker images and containers.
  • Docker Registry configurations that represent your Docker image repository user accounts.

Working with Docker: Process overview

  1. Install Docker and start the Docker daemon (on Windows and OS X - along with the Docker VM in which it runs). For corresponding instructions, see Get Started with Docker.
  2. Install the Docker integration plugin. For instructions, see Installing, Updating and Uninstalling Repository Plugins.

    Note: For the plugin to work properly, IntelliJ IDEA should be started using Java 7 or a later version.

  3. Create a Docker configuration. You can do that:
    • Separately, in the Settings / Preferences dialog: Ctrl+Alt+S | Build, Execution, Deployment | Clouds | add | Docker.
    • When creating a Docker Deployment run configuration: Run | Edit Configurations | add | Docker Deployment, etc.

    See the settings for a Docker configuration.

  4. If you are going to deploy your application into a container, create an artifact configuration for your application and build the artifact (e.g. Build | Build Artifacts).
  5. Create a Docker Deployment run configuration.
  6. Execute the run configuration.
  7. Use the Docker Tool Window to manage your Docker images, containers and deployed applications.

Example: Deploying a web app into the Wildfly Docker container

Once you have installed Docker and the Docker integration plugin, you can try out some of the Docker integration features.

In this example, we'll use the Wildfly app server Docker image to create a container and deploy a one-page JSP application into that container.

  1. Create a project for developing a Java web application (File | New | Project | Java | Web Application, etc.), see e.g Enabling Web app development support when creating a project or module.
    01_DockerNewProject
  2. When the project is created, add text (e.g. Hello World!) to index.jsp, see e.g. Developing source code.
    02_DockerIndexJsp
  3. Create a Docker configuration and specify Docker API settings:
    1. Open the Clouds page of the Settings / Preferences dialog: File | Settings | Build, Execution, Deployment | Clouds on Windows and Linux, or IntelliJ IDEA | Preferences | Build, Execution, Deployment | Clouds on OS X.
    2. Click add and select Docker.

      The default settings provided by IntelliJ IDEA should be OK:

      • API URL: https://192.168.99.100:2376
      • Certificates folder: <your_home_directory>\.docker\machine\machines\default

      The rest of the settings, at the moment, don't matter.

      03_DockerSettings
    3. Click OK in the Settings / Preferences dialog.
  4. In the project root directory, create a new directory (e.g. docker-dir): File | New | Directory, etc.

    We'll use this directory to store our Dockerfile, a .war application artifact and a container settings file - just to keep all the files for working with Docker in one place.

  5. Create a file Dockerfile in the docker-dir directory.
  6. Add the following to your Dockerfile:
    FROM jboss/wildfly ADD <artifact-name>.war /opt/jboss/wildfly/standalone/deployments/

    Use the actual artifact name in place of <artifact-name>. On the following picture, the name of the artifact is HelloDocker.

    04_DockerDockerFile

    (This Dockerfile sets jboss/wildfly as the base image and copies the local file <artifact-name>.war located in the same directory as the Dockerfile to the container directory /opt/jboss/wildfly/standalone/deployments/.)

  7. Create an artifact configuration for your <artifact-name>.war:
    1. Open the Project Structure dialog (e.g. Ctrl+Shift+Alt+S) and select Artifacts.
    2. Click add, select Web Application: Archive and select For '<project-name>:war exploded'.
    3. Change the artifact name (in the Name field). The name should be the same as in your Dockerfile (<artifact-name>) but without .war at the end.
    4. Select the docker-dir directory as the artifact output directory (the Output directory field).
      05_DockerArtifact
    5. Click OK in the Project Structure dialog.
  8. Create a Docker Deployment run/debug configuration and specify its settings:
    1. Run | Edit Configurations | add | Docker Deployment.
    2. Name. Specify the run configuration name, e.g. Docker.
    3. Server. Select your Docker configuration.
    4. Deployment. Select docker-dir/Dockerfile. This means that the application deployment will be controlled by the corresponding Dockerfile.
    5. Before launch. Click add, select Build Artifacts, and select your <artifact-name> artifact.
      06_DockerRunConfig
    6. Select the Container tab.
    7. JSON file. Generate a sample container settings .json file and save it in docker-dir: click import_icon, select docker-dir and click OK.
      06_DockerRunConfig_Container

      The rest of the settings are optional.

    8. Click OK in the Run/Debug Configurations dialog.
  9. Open container-settings.json in the editor. Note this fragment:
    "HostConfig": { "PortBindings":{ "8080/tcp": [{ "HostIp": "0.0.0.0", "HostPort": "18080" }] } }

    This means that the TCP port 8080 inside the container will be available to the outside world as the port 18080.

  10. Execute the run/debug configuration, e.g. by clicking run to the right of the run configuration selector.
    07_DockerRun

    Wait while IntelliJ IDEA downloads and builds the Wildfly image, deploys the application artifact, and creates and starts the container. (The Docker tool window opens and corresponding info is output there.)

    08_DockerAppServers
  11. When the container is finally started, open a web browser and go to http://192.168.99.100:18080/<artifact-name>/ to see the application output.
    09_DockerAppInBrowser
  12. To get an impression of what other functionality is available, look at context menu commands for various items in the Docker tool window.
    10_DockerAppServersContextMenu

    Note that you can also run Bash docker commands. On OS X and Linux you can use the IntelliJ IDEA Terminal tool window for that. On Windows you should use the Bash command-line interpreter available in the Docker Toolbox.

Example: Using Docker Compose

The project created in the previous example is used for illustrating the workflow.

To use Docker Compose, you should:

  1. Make sure that the Docker Compose executable setting in your Docker configuration is correct.
    1. On Windows or Linux: File | Settings | Build, Execution, Deployment | Clouds.
      On OS X: IntelliJ IDEA | Preferences | Build, Execution, Deployment | Clouds.
    2. If there is more than one configuration on the Clouds page, select the Docker configuration of interest under add and delete.
    3. The default setting docker-compose for Docker Compose executable is fine if:
      • The actual name of the executable file is docker-compose.
      • The path to the directory where the file is located is included in the environment variable Path.

      If necessary, you can specify an actual path to the executable file: click browseButton and select the file in the dialog that opens.

      12_DockerComposeExecutable
    4. Click OK in the Settings / Preferences dialog.
  2. Create a Docker Compose YAML file and define your services.
    1. Create the file docker-compose.yml in the docker-dir directory.
    2. Copy the following service definitions into the file:
      version: '2' services: web: build: . ports: - "18080:8080" links: - db db: image: postgres

      (This file defines two services: web and db. The container for the web service is built according to the Dockerfile located in the same directory. The container port 8080 is mapped to the host port 18080. The service is linked to a container in the db service. The container for the db service is built using the postgres image.)

      11_DockerComposeYmlInEditor
  3. Create a Docker Deployment run configuration. In that configuration, specify that the YAML file should be used to deploy your multi-container application.
    1. Run | Edit Configurations | add | Docker Deployment.
    2. Name. Specify the run configuration name, e.g. DockerCompose.
    3. Server. Select your Docker configuration.
    4. Deployment. Select docker-dir/docker-compose.yml. This means that the application deployment will be controlled by the corresponding Docker Compose YAML file.
      13_DockerDeploymentRunConfig

      The rest of the settings are optional.

    5. Click OK in the Run/Debug Configurations dialog.
  4. Execute the run configuration (run).
    14_DockerComposeRun

    The result in the Docker tool window will finally look something like this:

    15_DockerToolWindowComposeResult

See Also

Last modified: 5 September 2016