PhpStorm 2017.2 Help

Docker

Where do I get Docker from?

  1. Download Docker from the Docker Official website.
  2. Install and start Docker as described in the Docker Installation Instructions.

How do I configure connection with Docker?

Step 1: Create a Docker configuration
On the main menu, choose File | Settings | Build, Execution, Deployment | Docker for Windows and Linux or PhpStorm | Preferences | Build, Execution, Deployment | Docker for macOS. On the Docker Settings page that opens, click add.png in the central pane.

Step 2: Specify the Docker connection settings
The connection settings depend on your Docker version and operating system.
Option 1: Docker for macOS
Connect to Docker daemon with: Docker for Mac
Option 2: Docker for Windows

  1. Connect to Docker daemon with: TCP socket
  2. Engine API URL: tcp://localhost:2375
  3. Certificates folder: Leave this field empty.
  4. In the General section of your Docker settings, turn on the Expose daemon on tcp://localhost:2375 without TLS option.
/help/img/idea/2017.2/03_DockerSettings.png
Option 3: Docker for Linux
Connect to Docker daemon with: Unix socket
Option 4: Docker Toolbox for Windows or macOS
Connect to Docker daemon with: Docker Machine

Step 3: Check your connection
If the specified settings are correct, a Connection successful message is displayed. If no message appear, check your Docker Machine executable setting on the Docker | Tools page. For more information, see Docker connection settings.

Step 4 (optional): Map local folders to paths in the container
If you are going to bind container volumes with local folders, on Windows and macOS you need to specify the corresponding mappings in the Path mappings section. For more information, see Working with volume bindings.

Step 5 (optional): Configure Docker Compose
Open the Tools page (File | Settings | Build, Execution, Deployment | Docker | Tools for Windows and Linux or PhpStorm | Preferences | Build, Execution, Deployment | Docker | Tools for macOS) and specify the location of your Docker Compose executable. Accept the default setting docker-compose or click /help/img/idea/2017.2/browseButton.png and choose your actual executable in the dialog box that opens.

How do I connect to Docker?

  1. Open the Docker Tool Window by choosing View | Tool Windows | Docker.
  2. Select a Docker node /help/img/idea/2017.2/iconDocker.png and click /help/img/idea/2017.2/run.png or choose Connect on the context menu.
    /help/img/idea/2017.2/51_DockerConnected.png

How do I manage Docker images?

With PhpStorm, you can pull and push Docker images, add tags to them, detect IDs of images, as well as search images by parts of their names or ID. All these operations are performed from the dedicated Docker Tool Window.

To open the Docker tool window
Choose View | Tool Windows | Docker on the main menu.

To pull an image
In the Docker tool window, select a Docker node /help/img/idea/2017.2/iconDocker.png and click /help/img/idea/2017.2/iconPullImageDocker.png or choose Pull image on the context menu.

/help/img/idea/2017.2/52_DockerPullImage.png
In the dialog that opens, specify the following:
  • In the Registry field, type the URL of the image repository service (e.g. registry.hub.docker.com for Docker Hub) or a Docker Registry configuration.
  • In the Repository field, type the image name.
  • In the Tag field, set a tag to the image tag, e.g. latest.
/help/img/idea/2017.2/53_DockerPullImageDialog.png

To find out the ID of an image
In the Docker tool window, select the image of interest. The image ID is shown on the Properties tab.

/help/img/idea/2017.2/54_DockerImageID.png

To hide images without tags
In the Docker tool window, select the Images node or one of the images and click /help/img/idea/2017.2/iconUntaggedImages.png or choose Untagged images on the context menu.

To find a local image by its name or ID
In the Docker tool window, switch to the left-hand pane and start type the name or ID of an image. PhpStorm highlights this text in the names and IDs of the images and containers wherever the text is detected.

/help/img/idea/2017.2/55_DockerFindImage.png

To build an image

  1. Create a Dockerfile for the image that you want to build and open the file in the editor.
  2. To the left of FROM, click /help/img/idea/2017.2/runTwoGreenArrows.png and choose Run on 'Docker'.
    /help/img/idea/2017.2/56_DockerfileRun_1.png
    Your image is built and run in a container according to your Dockerfile.

To delete a container after the corresponding image is built
In the Docker tool window, select the container and click /help/img/idea/2017.2/delete.png or choose Delete container on the context menu.

To push an image to an image repository

  1. In the Docker tool window, select the image to upload and click /help/img/idea/2017.2/iconPushImageDocker.png or choose Push image on the context menu.
    /help/img/idea/2017.2/57_DockerPushImage.png
  2. In the dialog that opens, specify the Docker Registry configuration to use, the name of the image you are pushing and the attached tag.
    /help/img/idea/2017.2/58_DockerPushDialog.png

To delete one or several images
In the Docker tool window, select the image or images to delete and click /help/img/idea/2017.2/close.png or choose Delete image on the context menu.

How do I specify my image repository user account information?

When you push an image to a repository you are required to log on to the corresponding server. Your image repository user account info that you have to provide in such cases is stored in what is called a Docker Registry configuration.

Step 1: Start creating a Docker Registry configuration

  1. In the Docker tool window, select the image to upload and click /help/img/idea/2017.2/iconPushImageDocker.png or select a Docker node /help/img/idea/2017.2/iconDocker.png and click /help/img/idea/2017.2/iconPullImageDocker.png, depending on whether you are pushing or pulling an image.
  2. In the Pull image or the Push image dialog that opens, click New.

Step 2: Specify the following Registry Configuration settings

  • In the Address field, type the image repository service URL, e.g. registry.hub.docker.com for Docker Hub or quay.io for Quay.
  • The user name and password for your user account.
  • The email address that you specified when creating your user account.
  • In the Server field, type the name of the associated Docker connection settings (usually, Docker). They are used to connect to the service to check that your user account info is correct.
/help/img/idea/2017.2/59_DockerRegistryDialog.png

How do I run images?

PhpStorm provides interface for running images from the Docker Tool Window or from a Dockerfile. In either case, PhpStorm automatically creates a dedicated Docker Deployment run/debug configuration with the default settings.

You can also view deployment logs and convert the command-line options for the docker run command into the container settings JSON file, this file can be used for configuring your container at its start.

Option 1: Run an image from the Docker tool window

  1. In the Docker tool window, select the image to run and click /help/img/idea/2017.2/iconCreateContainerDocker.png or choose Create container on the context menu.
    /help/img/idea/2017.2/62_DockerCreateContainer.png
  2. In the Create container popup, click Create.
  3. In the Create Deployment Configuration dialog that opens specify the following:
    • In the Deployment field, the Docker Image option is selected. This indicates that the run configuration is based on an image available locally.
    • The Image ID field initially shows the ID of the image for which you called the Create container command.
    • In the Container name field, specify the name for the container that will be created. Otherwise, Docker will give your container a name automatically.
    /help/img/idea/2017.2/63_DockerImageDeployment.png
  4. Click Run.

Option 2: Run an image from a Dockerfile

  1. Open your Dockerfile in the editor.
  2. To the left of FROM, click /help/img/idea/2017.2/runTwoGreenArrows.png and select Run on 'Docker'.
    /help/img/idea/2017.2/56_DockerfileRun_1.png
    Your image runs according to the Dockerfile and a Dockerfile-based run configuration is created.
The main settings of a run configuration associated with a Dockerfile are:
  • The Deployment field shows you Dockerfile.
  • The name and the tag of the image that will be built, e.g. myimage:latest.
  • The name of the container that will be created. If omitted, Docker will itself give your container a name.
/help/img/idea/2017.2/61_DockerfileRunConfig.png

Option 3: Use a Docker Deployment run configuration
This method is helpful when you run a multi-container application using a Docker Compose docker-compose.yml file.

  1. On the main menu, choose Run | Edit Configurations, click /help/img/idea/2017.2/add.png in the dialog that opens, and choose Docker Deployment from the list.
  2. Complete the configuration settings in the Docker Deployment dialog that opens.
  3. Select the newly created configuration from the list on the toolbar and click /help/img/idea/2017.2/run.png.

To re-run a Dockerfile
Click /help/img/idea/2017.2/runTwoGreenArrows.png and choose Run '<ConfigurationName>' from the list.

/help/img/idea/2017.2/60_DockerfileRunEdit.png

To view deployment log
After an image is built and corresponding container is started, select the container in the Docker tool window. The log is shown in the right-hand pane on the Deploy log tab.

/help/img/idea/2017.2/65_DockerDeploymentLog.png

To convert command-line options into container settings JSON

  1. In the corresponding Docker Deployment run configuration, select the Container tab.
  2. To the right of the JSON file field, click CLI.
    /help/img/idea/2017.2/66_DockerContainerCLI.png
  3. In the dialog that opens, specify the options and the location of the resulting .json file.
    /help/img/idea/2017.2/66_DockerCLIToJSON.png

Working with containers

Running commands in a container

You can run docker exec commands:

  1. In the Docker tool window, right-click the container of interest and select Exec.
  2. In the Run command in container popup, click Create.
  3. In the dialog that opens, type the command and click OK. For
    docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
    you need to specify only the
    COMMAND [ARG...] part. For example:
    ls /tmp or mkdir /tmp/my-new-dir.

Starting a Shell or Bash session in a container

Use Exec as described earlier, i.e:

  1. In the Docker tool window, right-click the container and select Exec | Create.
  2. Type:
    /bin/sh or /bin/bash
    /help/img/idea/2017.2/67_DockerExecBinBash.png
  3. Press Enter or click OK.
    /help/img/idea/2017.2/67_DockerExecBinBash_1.png

Finding out the container and image IDs

In the Docker tool window, select the container of interest. Its ID and the ID of its parent image are shown on the Properties tab.

You can copy the image ID, and the container ID and name onto the clipboard: select the corresponding row in the table and click /help/img/idea/2017.2/copy.gif. As an alternative, you can use the Copy image ID and Copy container ID context menu commands.

Renaming a container

  1. In the Docker tool window, select the container of interest.
  2. Select the Properties tab.
  3. In the Container name field, specify a new name for your container, and click Save.

Inspecting a container

You can get detailed low-level information about a container in JSON format by running docker inspect:
In the Docker tool window, right-click the container of interest and select Inspect. The result is shown on the Inspection tab.

/help/img/idea/2017.2/68_DockerInspect.png

Showing container processes

To show the list of processes running in a container, right-click the container of interest in the Docker tool window and select Show processes. The result is shown on the Processes tab.

Opening a console for an ENTRYPOINT process

To see the output of the ENTRYPOINT process running inside a container, you can attach to its stdin/out:
In the Docker tool window, right-click the container and select Attach. The console will open on the Attached console tab.

Viewing the container log

When you select a container in the Docker tool window, the container log is shown on the Log tab.

Stopping a container

In the Docker tool window, select the container, and then click /help/img/idea/2017.2/stop.gif or select Stop container from the context menu.

Restarting a container

In the Docker tool window, select the container, and then click /help/img/idea/2017.2/deploymentConsoleDeployAll.png or select Redeploy from the context menu.

Rerunning an image with different settings

  1. In the Docker tool window, select the container in which the image of interest runs.
  2. Click /help/img/idea/2017.2/edit_config_docker.png or select Edit Configuration from the context menu.
  3. In the associated Docker Deployment run configuration that opens, edit the settings as necessary.
  4. Click /help/img/idea/2017.2/deploymentConsoleDeployAll.png or select Redeploy from the context menu.

Hiding stopped containers

In the Docker tool window, you can hide the containers that are not running:
Select the Containers node or one of the containers, and then click /help/img/idea/2017.2/iconStoppedContainers.png or select Stopped containers from the context menu.

Working with volume bindings

Preparing for volume bindings on Windows and macOS

To be able to map host folders to container volumes on Windows or macOS, you should first specify corresponding path mappings:

  1. Open your Docker connection settings: in the Docker tool window, select the Docker node /help/img/idea/2017.2/iconDocker.png and click /help/img/idea/2017.2/edit_config_docker.png. Alternatively, Ctrl+Alt+S | Build, Execution, Deployment | Docker.
  2. In the Path mappings section, select an existing mapping and click /help/img/idea/2017.2/edit1.png to edit it, or click /help/img/idea/2017.2/add.png to create a new mapping.
    /help/img/idea/2017.2/69_Docker_path_mappings.png
  3. In the dialog that opens, specify the mapping:
    • Local path. The path to a local folder that you want to make available for volume bindings.
    • Virtual machine path. This is the corresponding directory path in the Docker virtual machine's file system.
    /help/img/idea/2017.2/69_Define_path_mapping.png

Specifying volume bindings in a run configuration

  1. Create a new Docker Deployment run configuration or open an existing one.
  2. Select the Container tab and expand the Volume bindings section.
  3. Click /help/img/idea/2017.2/add.png to create a new binding.
  4. In the dialog that opens, specify:
    • Container path. The path to the data volume in the container.
    • Host path. The path to the host directory that should be mounted at the specified container path.
    • Readonly. Select the check box to make the container volume readonly.
    /help/img/idea/2017.2/69_DockerVolumeBindingsTomcat.png

Viewing and editing volume bindings for a running container

  1. In the Docker tool window, select the container and then select the Volume Bindings tab.
  2. To create a new binding, click /help/img/idea/2017.2/add.png. To edit an existing one, select the binding and click /help/img/idea/2017.2/edit1.png.
  3. Specify the settings as necessary.
  4. To apply the changes, click Save.
    /help/img/idea/2017.2/70_DockerVolumeBindingsTomcatDTW.png

Working with port bindings

You can specify the port binding settings in a Docker Deployment run configuration. Then, when your container is running, you can view and change these settings, and apply the changes.

Specifying the port binding settings in a run configuration

  1. Create a new Docker Deployment run configuration or open an existing one.
  2. Select the Container tab and expand the Volume bindings section.
  3. To publish all the container ports, select the corresponding check box.
  4. To specify individual bindings (the Publish all ports option in this case should be off), expand the Volume bindings section, and click /help/img/idea/2017.2/add.png to create a new binding.
  5. In the dialog that opens, specify:
    • Container port. The container port to be mapped.
    • Protocol. TCP or UDP.
    • Host IP. To make the port accessible only from your localhost, specify localhost or 127.0.0.1. To make the port accessible from other computers on your network as well, specify 0.0.0.0.
    • Host port. The corresponding host port.
    /help/img/idea/2017.2/71_DockerPortBindingsTomcat.png

Viewing and editing the port binding settings for a running container

  1. In the Docker tool window, select the container and then select the Port Bindings tab.
  2. If the container was started with the Publish all ports option on, to see the port mappings, run the Inspect command from the container's context menu, and then search the result (Ctrl+F) for "Ports".
  3. To create a new binding, click /help/img/idea/2017.2/add.png. To edit an existing one, select the binding and click /help/img/idea/2017.2/edit1.png. If the Publish all ports option is currently on, turn it off to be able to specify individual port mappings.
  4. For each particular binding, specify the settings as necessary.
  5. To apply the changes, click Save.
    /help/img/idea/2017.2/72_DockerPortBindingsTomcatDTW.png

Working with environment variables

The environment variables are usually set in the Dockerfile associated with the base image that you are using. There are also the environment variables that Docker sets automatically for each new container.

In a Docker Deployment run configuration, you can specify additional variables and redefine the ones that Docker sets. At a later time, when your container is running, you can view and edit the existing variables, and create and set new ones.

Specifying the environment variables in a run configuration

  1. Create a new Docker Deployment run configuration or open an existing one.
  2. Select the Container tab and expand the Environment variables section.
  3. Click /help/img/idea/2017.2/add.png to create a new variable.
  4. In the dialog that opens, specify the variable name and value.
    /help/img/idea/2017.2/73_DockerEnvVarJar.png

Viewing and editing the environment variables for a running container

  1. In the Docker tool window, select the container and then select the Environment variables tab.
  2. To create a new variable, click /help/img/idea/2017.2/add.png. To edit an existing one, select the variable and click /help/img/idea/2017.2/edit1.png.
  3. To apply the changes, click Save.
    /help/img/idea/2017.2/74_DockerEnvVarJarDTW.png

Links to other containers are specified in a Docker Deployment run configuration:

  1. Open a Docker Deployment run configuration for a container that you want to link with another container.
  2. Select the Container tab and expand the Links section.
  3. Click /help/img/idea/2017.2/add.png to create a new link.
  4. In the dialog that opens, specify:
    • Name. Select the container you want to link to.
    • Alias. The link name alias.
  5. Start the container by executing the run configuration.
  6. Now, to see the link information, select Inspect from the container's context menu in the Docker tool window. Then search the inspection result (shown on the Inspection tab, Ctrl+F) for HostConfig. The link information will be shown as
    "Links" : [ "<container_name>:<link_name_alias>" ].

    Alternatively, you can open the Terminal tool window and run the following command:
    docker inspect -f "{{ .HostConfig.Links }}" <container_name>

Using Docker Compose

Running services via a Docker Deployment run configuration

  1. Create a Docker Compose docker-compose.yml file and specify your services.
  2. Create a Docker Deployment run configuration: Run | Edit Configurations | /help/img/idea/2017.2/add.png | Docker Deployment.
  3. Next to Deployment, select your docker-compose.yml file.
    /help/img/idea/2017.2/64_DockerComposeDeployment.png
  4. Execute the run configuration: /help/img/idea/2017.2/run.png.
    /help/img/idea/2017.2/95_DockerComposeRunning.png

Scaling a service

  1. In the Docker tool window, select the service you want to scale.
  2. Click /help/img/idea/2017.2/IconDockerScaleService.png or select Scale from the context menu.
    /help/img/idea/2017.2/96_DockerComposeScaleService.png
  3. Specify how many containers you want in the service.
    /help/img/idea/2017.2/97_DockerComposeServiceScaled.png

Stopping and starting a service

In the Docker tool window, select the service.

  • To stop the service, click /help/img/idea/2017.2/stop.gif or select Stop from the context menu.
  • To start the service, click /help/img/idea/2017.2/run.png or select Start from the context menu.
    /help/img/idea/2017.2/98_DockerComposeServiceStart.png

Interacting with containers

Most of the functions that PhpStorm provides for working with "independent" containers are also available for the containers within Docker Compose services. To access these functions, use the containers' context menus and the toolbar icons in the Docker tool window. For more info, see Working with containers.

Troubleshooting

Unable to connect to Docker

Make sure that:

  • Docker is running.
  • Your Docker connection settings are correct.
  • If you are using Docker for Windows:
    In the General section of your Docker settings, the Expose daemon on tcp://localhost:2375 without TLS option is on.
  • If you are using Docker Toolbox:

Unable to pull an image

When trying to pull an image, you may be getting the following message:

Failed to parse dockerCfgFile: <your_home_dir>/.docker/config.json, caused by: ... {"credsStore":"wincred"}

If this is the case, go to <your_home_dir>/.docker directory and delete the file config.json.

Unable to use Docker Compose

Make sure that the Docker Compose executable setting in your Docker settings is correct.

Unable to use port bindings

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

My deployment log is empty

If you have another project open at the moment, you may find your deployment log in that project.

Limitations

Our Docker integration has limitations but we are working on its improvement. See the list of Docker issues in our tracking system and vote for the ones that you think should be resolved first of all.

Last modified: 12 October 2017

See Also