Docker
Preparing to use Docker
1. Download, install and start Docker
To download Docker and find out how to install and start it, see Install Docker.
2. Specify Docker connection settings
- Open the Settings / Preferences dialog (e.g. Ctrl+Alt+S) and go to the Clouds page ( ).
- Click and select Docker.
- Depending on your Docker version and operating system, specify:
Docker Toolbox for Windows (deprecated; the defaults provided by IntelliJ IDEA should be OK):
- API URL:
https://192.168.99.100:2376
- Certificates folder:
<your_home_directory>\.docker\machine\machines\default
- API URL:
- If you are going to use Docker Compose, specify the location of your Docker Compose executable. The default setting
docker-compose
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
.
To specify an actual path to the executable file, click and select the file in the dialog that opens.
- The actual name of the executable file is
- Click OK in the Settings / Preferences dialog.
Example: Deploying a web app into the Wildfly Docker container
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.
- Create a project for developing a Java web application ( , etc.).
- When the project is created, add text (e.g.
Hello World!
) toindex.jsp
, see e.g. Developing source code. - In the project root directory, create a new directory (e.g.
docker-dir
): , 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. - Create a file
Dockerfile
in thedocker-dir
directory. - 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
.(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/
.) - Create an artifact configuration for your <artifact-name>.war:
- Open the Project Structure dialog (e.g. Ctrl+Shift+Alt+S) and select Artifacts.
- Click , select Web Application: Archive and select For '<project-name>:war exploded'.
- 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. - Select the
docker-dir
directory as the artifact output directory (the Output directory field). - Click OK in the Project Structure dialog.
- Create a Docker Deployment run/debug configuration and specify its settings:
- .
- Name. Specify the run configuration name, e.g. Docker.
- Server. Select your Docker configuration.
- Deployment. Select docker-dir/Dockerfile. This means that the application deployment will be controlled by the corresponding
Dockerfile
. - Before launch. Click , select Build Artifacts, and select your
<artifact-name>
artifact. - Select the Container tab.
- JSON file. Generate a sample container settings
.json
file and save it indocker-dir
: click , selectdocker-dir
and click OK.The rest of the settings are optional.
- Click OK in the Run/Debug Configurations dialog.
- 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.
- Execute the run/debug configuration, e.g. by clicking to the right of the run configuration selector.
Wait while IntelliJ IDEA downloads and builds the Wildfly image, deploys the application artifact, and creates and starts the container. (The corresponding info is output to the Docker tool window.)
- When the container is finally started, to see the application output, open a web browser and go to:
If you are using Docker for Windows, macOS or Linux:
http://localhost:18080/<artifact-name>/
If you are using the Docker Toolbox for Windows or macOS (deprecated):
http://192.168.99.100:18080/<artifact-name>/
- To get an impression of what other functionality is available, look at context menu commands for various items in the Docker tool window.
Example: Using Docker Compose
The project created in the previous example is used for illustrating the workflow. If your Wildfly container is still running, stop it: select the container in the Docker tool window and click .
- Create a Docker Compose YAML file and define your services.
- Create the file
docker-compose.yml
in thedocker-dir
directory. - 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
anddb
. The container for theweb
service is built according to theDockerfile
located in the same directory. The container port8080
is mapped to the host port18080
. The service is linked to a container in thedb
service. The container for thedb
service is built using thepostgres
image.)
- Create the file
- Create a Docker Deployment run configuration. In that configuration, specify that the YAML file should be used to deploy your multi-container application.
- .
- Name. Specify the run configuration name, e.g. DockerCompose.
- Server. Select your Docker configuration.
- Deployment. Select docker-dir/docker-compose.yml. This means that the application deployment will be controlled by the corresponding Docker Compose YAML file.
The rest of the settings are optional.
- Click OK in the Run/Debug Configurations dialog.
- Execute the run configuration ().
The result in the Docker tool window will finally look something like this: