CLion 2018.3 Help

Docker

Docker enables developers to deploy applications inside containers for testing code in an environment identical to production. CLion provides Docker support using the Docker integration plugin.

Enable Docker support

  1. Install Docker

  2. Configure the Docker daemon connection settings
    • In the Settings/Preferences dialog (Ctrl+Alt+S), click Docker under Build, Execution, Deployment.

    • Add a Docker configuration (The Add button) and specify how to connect to the Docker daemon.

      The connection settings depend on your Docker version and operating system. For more information, see Docker connection settings

      The Connection successful message should appear at the bottom of the dialog.

      The Docker connection settings

      The Path mappings table is used to map local folders to corresponding directories in the Docker virtual machine's file system. Only specified folders will be available for volume binding.

  3. Connect to the Docker daemon

    In the Docker tool window (View | Tool Windows | Docker), select the Docker node Docker node, and then click The Connect button or select Connect from the context menu.

    The Docker tool window, connected to Docker

The Docker tool window (View | Tool Windows | Docker) enables you to manage images, run containers, and use Docker Compose. As with other tool windows, you can start typing the name of an image or container to highlight the matching items.

The Docker tool window, text search

Managing images

Docker images are executable packages for running containers. Depending on your development needs, you can use Docker to:

  • Pull pre-built images from a Docker registry. For example, you can pull an image that runs a Postgres server container to test how your application will interact with your production database.

  • Build images locally from a Dockerfile. For example, you can build an image that runs a container with the Java Runtime Environment (JRE) of some specific version and executes your Java application inside it.

  • Push your images to a Docker registry if you want to share them with others. For example, if you want to demonstrate to someone how your application runs in some specific version of the JRE instead of setting up the proper environment, they can run a container from your image.

Images are distributed via the Docker registry. Docker Hub is the default public registry with all of the most common images: various Linux flavors, database management systems, web servers, runtimes, and so on. There are other public and private Docker registries, and you can also deploy your own registry server.

Configure a Docker registry

  1. In the Settings/Preferences dialog (Ctrl+Alt+S), select Build, Execution, Deployment | Docker | Registry.

  2. Add a Docker registry configuration (The Add button) and specify how to connect to the registry. If you specify the credentials, CLion will automatically check the connection to the registry. The Connection successful message should appear at the bottom of the dialog.

    The Docker Registry dialog

Pull an image from a Docker registry

  1. In the Docker tool window, right-click the Images node, and select Pull image from the context menu.

    The Pull Image context menu item

  2. Select the Docker registry and specify the repository and tag (name and version of the image, for example, tomcat:latest ).

    The Pull Image dialog

For more information, see the docker pull command reference.

Build an image from a Dockerfile

  1. Open the Dockerfile from which you want to build the image.

  2. Click Build on Docker in the gutter and select to build the image on a specific Docker node.

    The Build Image on Docker popup

For more information, see the docker build command reference.

Push an image to a Docker registry

  1. In the Docker tool window, select the image that you want to upload and click Push image, or right-click it and select Push image from the context menu.

    The Push Image context menu item

  2. Select the Docker registry and specify the repository and tag (name and version of the image, for example, my-app:v2).

    The Push Image dialog

For more information, see the docker push command reference.

Images that you pull or build are stored locally and are listed in the Docker tool window. When you select an image, you can view its ID or copy it to the clipboard by clicking the The Copy to clipboard button button on the Properties tab.

Docker image properties

Images with no tags (<none>:<none>) can be one of the following:

  • Intermediate images serve as layers for other images and do not take up any space.

  • Dangling images remain when you rebuild an image based on a newer version of another image. Dangling images should be pruned to conserve disk space.

To hide untagged images from the list, click Filter menu on the Docker toolbar, and then click Show Untagged Images to remove the check mark.

To delete one or several images, select them in the list and click Delete Image or choose Delete image from the context menu.

Running containers

Containers are runtime instances of corresponding images. For more information, see the docker run command reference.

CLion uses run configurations (Run | Edit Configurations) to run Docker containers. There are three types of Docker run configurations:

Run a container from an existing image

  1. In the Docker tool window, select the image and click Create container on the Docker toolbar or right-click it and select Create container from the context menu.

    The Create container context menu item

  2. In the Create container popup, click Create.

  3. In the Create Docker Configuration dialog that opens, you can provide a unique name for the configuration and specify a name for the container. If you leave the Container name field empty, Docker will give it a random unique name.

    The Create Docker Configuration dialog

Run a container from a Dockerfile

  1. Open the Dockerfile from which you want to run the container.

  2. Click Run on Docker in the gutter and select to run the container on a specific Docker node.

    The Run on Docker popup

This creates and starts a run configuration, which builds an image based on the Dockerfile and then runs a container based on this image.

To edit the run configuration, right-click the container and click Edit Configuration. In the Edit Deployment Configuration dialog, you can specify a custom tag for the built image, as well as a name for the container, and a context folder from which to read the Dockerfile. The context folder can be useful, for example, if you have some artifacts outside of the scope of your Dockerfile, which you would like to add to the file system of the image.

Command-line options

When running a container on the command line, the following syntax is used:

docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

All optional parameters can be specified in the corresponding Docker run configuration fields.

The Edit Deployment Configuration dialog with command-line options
  • Options are specified in the Run options field. In the previous screenshot, the container is connected to the my-net network and is assigned an alias my-app.

  • Commands and arguments to be executed when starting the container are specified in the Entrypoint and Command fields. These fields override the corresponding ENTRYPOINT and CMD instructions in the Dockerfile. In the previous screenshot, when the container starts, it executes the docker-entrypoint.sh script with postgres as an argument.

The Command preview field shows the actual Docker command used for this run configuration.

You can also configure the following container settings in the run configuration:

Bind mounts

Docker can mount a file or directory from the host machine to the container using the -v or --volume option. You can configure this in the Docker run configuration using the Bind mounts field.

In the Bind Mounts dialog, you can create a list of bindings by specifying the host directory and the corresponding path in the container where it should be mounted. Select Read only if you want to disable writing to the container volume.

The Edit Deployment Configuration dialog with bind mounts

The Bind mounts field shows the configured volume bindings. For example, if you want to mount some local PostgreSQL data directory (/Users/Shared/pg-data) to the PostgreSQL data directory inside the container (/var/lib/pgsql/data), this can be configured as illustrated on the previous screenshot.

If you expand the Command preview field, you will see that the following line was added:

-v /Users/Shared/pg-data:/var/lib/pgsql/data

This can be used in the Command line options field instead of creating the list of volume bindings using the Bind Mounts dialog.

View and modify volume bindings for a running container

  1. In the Docker tool window, select the container and then select the Volume Bindings tab.

    The Volume Bindings tab

  2. To create a new binding, click The Add icon. To edit an existing one, select the binding and click The Edit icon.

  3. Specify the settings as necessary and click Save to apply changes.

The container is stopped and removed, and a new container is created with the specified changes. However, changes are not saved in the corresponding run configuration.

Bind ports

Docker can map specific ports on the host machine to ports in the container using the -p or --publish option. This can be used to make the container externally accessible. In the Docker run configuration, you can choose to expose all container ports to the host or use the Bind ports field to specify port mapping.

In the Port Bindings dialog, you can create a list of bindings by specifying which ports on the host should be mapped to which ports in the container. You can also provide a specific host IP from which the port should be accessible (for example, you can set it to 127.0.0.1 to make it accessible only locally, or set it to 0.0.0.0 to open it for all computers in your network).

The Edit Deployment Configuration dialog with bind ports

The Bind ports field shows the configured port bindings. For example, if you already have PostgreSQL running on the Docker host port 5432, you can map port 5433 on the host to 5432 inside the container as illustrated on the previous screenshot. This will make PostgreSQL running inside the container accessible via port 5433 on the host.

If you expand the Command preview field, you will see that the following line was added:

-p 5433:5432

This can be used in the Command line options field instead of creating the list of port bindings using the Port Bindings dialog.

View and modify port bindings for a running container

  1. In the Docker tool window, select the container and then select the Port Bindings tab.

    The Port Bindings tab

  2. To create a new binding, click The Add icon. To edit an existing one, select the binding and click The Edit icon. If the Publish all ports check box is selected, clear it to be able to specify individual port mappings.

  3. Specify the settings as necessary and click Save to apply changes.

The container is stopped and removed, and a new container is created with the specified changes. However, changes are not saved in the corresponding run configuration.

Environment variables

Environment variables are usually set in the Dockerfile associated with the base image that you are using. There are also environment variables that Docker sets automatically for each new container. You can specify additional variables and redefine the ones that Docker sets using the -e or --env option. In a Docker run configuration, you can use the Environment variables field to configure environment variables.

In the Environment Variables dialog, you can create a list of names and values for variables.

The Edit Deployment Configuration dialog with environment variables

The Environment variables field shows the configured variables. For example, if you want to connect to PostgreSQL with a specific user name by default (instead of the operating system name of the user running the application), you can define the PGUSER variable as illustrated on the previous screenshot.

If you expand the Command preview field, you will see that the following line was added:

--env PGUSER=pg-admin

This can be used in the Command line options field instead of creating the list of names and values using the Environment Variables dialog. If you need to pass sensitive information (passwords, secrets, etc.) as environment variables, you can use the --env-file option to specify a file with this information.

View and modify environment variables for a running container

  1. In the Docker tool window, select the container and then select the Environment variables tab.

    The Environment Variables tab

  2. To add a new variable, click The Add icon. To edit an existing one, select the variable and click The Edit icon.

  3. Specify the settings as necessary and click Save to apply changes.

The container is stopped and removed, and a new container is created with the specified changes. However, changes are not saved in the corresponding run configuration.

Build-time arguments

Docker can define build-time values for certain environment variables that do not persist in the intermediate or final images using the --build-arg option for docker build. These must be specified in the ARG instruction of the Dockerfile with a default value. You can configure build-time arguments in the Docker run configuration using the Build args field.

For example, you can use build-time arguments to build the image with a specific version of PostgreSQL. To do this, add the ARG instruction to the beginning of your Dockerfile:

ARG PGTAG=latest FROM postgres:$PGTAG

The PGTAG variable in this case will default to latest if you do not redefine it as a build-time argument. So by default, this Dockerfile will produce an image with the latest available PostgreSQL version. However, you can use the Build Args field to redefine the PGTAG variable.

The Edit Deployment Configuration dialog with build-time arguments

In the previous screenshot, PGTAG is set to 9, which will instruct Docker to pull postgres:9. When you deploy this run configuration, it will build an image and run the container with PostgreSQL version 9. To check this, execute postgres --version inside the container and see the output: it should be postgres (PostgreSQL) 9.6.6 or some later version.

If you expand the Command preview field, you will see that the following option was added to the docker build command:

--build-arg PGTAG=9

Interacting with containers

Created containers are listed in the Docker tool window. When you select a container, you can view its ID (and the ID of the corresponding image) and copy it to the clipboard using the The Copy to clipboard button button on the Properties tab. You can also specify a new name for the container and click Save to start another container with this new name from the same image.

By default, the Docker tool window displays all containers, including those that are not running. To hide stopped containers from the list, click the Filter menu on the Docker toolbar, and then click Show Stopped Containers to remove the check mark.

If a container was created using a Docker run configuration, to view its deployment log, select it and open the Deploy log tab. To view the log messages from the container's STDOUT and STDERR, select it and open the Log tab. For more information, see the docker logs command reference.

Execute a command inside a running container

  1. In the Docker tool window, right-click the container name and then click Exec.

  2. In the Run command in container popup, click Create.

  3. In the Exec dialog, type the command and click OK. For example:

    ls /tmp

    List the contents of the /tmp directory

    mkdir /tmp/my-new-dir

    Create the my-new-dir directory inside the /tmp directory

    /bin/bash

    Start a bash session

    The Exec tab with /bin/bash running

For more information, see the docker exec command reference.

View detailed information about a running container

  • In the Docker tool window, right-click the container name and then click Inspect.

    The output is rendered as a JSON array on the Inspection tab.

    The Inspection tab

For more information, see the docker inspect command reference.

View processes running in a container

  • In the Docker tool window, right-click the container name and then click Show processes.

    The output is rendered as a JSON array on the Processes tab.

For more information, see the docker top command reference.

Attach a console to the output of an executable container

  • In the Docker tool window, right-click the container and then click Attach.

    The console is attached to the output of the ENTRYPOINT process running inside a container, and is rendered on the Attached console tab.

For more information, see the docker attach command reference.

Docker Compose

Docker Compose is used to run multi-container applications. For example, you can run a web server, backend database, and your application code as separate services. Each service can be scaled by adding more containers if necessary. This enables you to perform efficient development and testing in a dynamic environment, similar to production.

Run a multi-container Docker application

  1. Define necessary services in one or several Docker Compose files.

  2. In the Run menu, click Edit Configurations.

  3. Click The Add icon, point to Docker and then click Docker-compose.

    The Docker-compose configuration

  4. Specify the Docker Compose files that define services which you want to run in containers. If necessary, you can restrict the services that this configuration will start, specify environment variables, and force building of images before starting corresponding containers (that is, add the --build option for the docker-compose up command).

  5. When the run configuration is ready, execute it.

When Docker Compose runs your multi-container application, you can use the Docker tool window to control specific services and interact with containers. The containers are listed under the dedicated Compose nodes, not under the Containers node (which is only for standalone containers).

Scale a service

  1. In the Docker tool window, right-click the service you want to scale and select Scale from the context menu.

    The Scale context menu item

  2. Specify how many containers you want for this service and click OK.

Stop a running service

  • In the Docker tool window, select the service and click Stop container or right-click it and select Stop from the context menu.

    The Stop context menu item

Stop all running services

  • In the Docker tool window, select the Compose node and click Stop container or right-click it and select Stop from the context menu.

    Docker Compose stop all services

Bring your application down

  • In the Docker tool window, right-click the Compose node and then click Down in the context menu.

    Docker Compose remove application

This stops and removes containers along with all related networks, volumes, and images.

Open the Docker Compose file that was used to run the application

  • In the Docker tool window, right-click the Compose node or a nested service node and then click Jump to Source in the context menu (F4).

The Docker-compose run configuration will identify environment files with the .env suffix if they are located in the same directory as the Docker Compose file.

Troubleshooting

If you encounter one of the following problems, try the corresponding suggested solution.

Unable to connect to Docker

Make sure that:

If you are using Docker for Windows, enable the Expose daemon on tcp://localhost:2375 without TLS option in the General section of your Docker settings.

If you are using Docker Toolbox, make sure that Docker Machine is running and its executable is specified correctly in the Settings / Preferences dialog (Ctrl+Alt+S) under Build, Execution, Deployment | Docker | Tools.

Unable to use Docker Compose

Make sure that the Docker Compose executable is specified correctly in the Settings / Preferences dialog (Ctrl+Alt+S) under Build, Execution, Deployment | Docker | Tools.

Unable to use port bindings

Make sure that the corresponding container ports are exposed. Use the EXPOSE command in your Dockerfile.

Unable to associate existing Dockerfiles or Docker Compose files with relevant types

When you create new Dockerfiles or Docker compose files, CLion automatically identifies their type. If a file type is not evident from its name, you will be prompted to select the file type manually. To associate an existing file with the correct type, right-click it in the Project view and select Associate with File Type from the context menu.

If the Associate with File Type actions is disabled, this probably means that the file name is registered as a pattern for current file type. For example, if you have a Dockerfile with a custom name that is recognized as a text file, you cannot associate it with the Dockerfile type. To remove the file type pattern, do the following:

  1. In the Settings/Preferences dialog (Ctrl+Alt+S), select Editor | File types.

  2. Select the relevant file type (in this case: Text) and remove the pattern with the name of the file.

  3. Click OK to apply the changes.

Now you should be able to set the correct file type using Associate with File Type in the context menu.

Limitations

The Docker integration plugin has certain limitations and bugs, however JetBrains is constantly working on fixes and improvements for it. You can find the list of Docker issues in our bug tracking system and vote for the ones that affect you the most. You can also file your own bugs and feature requests.

Last modified: 14 February 2019