Docker
Docker enables developers to deploy applications inside containers for testing code in an environment identical to production. With Docker, you can run different versions of operating systems, programming languages, web servers, database applications, and so on. This helps you to test the backend part of your application avoiding side effects from your current macOS environment.
Install the Docker plugin
AppCode provides Docker support using the Docker plugin which is not installed by default. To install the plugin:
In the Preferences dialog ⌃ ⌥ S, open Plugins and select Marketplace.
In the list of available plugins, find Docker and click Install:
After the plugin is installed, AppCode will prompt you to restart the IDE for applying the plugin changes. Click Restart IDE to do it immediately or select Not now and restart AppCode any time later.
Enable Docker support
Install and run Docker.
For more information, see the Docker documentation.
Configure the Docker daemon connection settings:
Press ⌃ ⌥ S to open the IDE settings and select Build, Execution, Deployment | Docker.
Click to add a Docker configuration and specify how to connect to the Docker daemon.
The connection settings depend on your Docker version and operating system.
The Connection successful message should appear at the bottom of the dialog.
Connect to the Docker daemon.
The configured Docker connection should appear in the Services tool window ( or ⌥ 8). Select the Docker node and click , or select Connect from the context menu.
To edit the Docker connection settings, select the Docker node and click on the toolbar, or select Edit Configuration from the context menu.
In the Services tool window ( or ⌥ 8), you can pull and push images, create and run containers, manage Docker Compose services, and so on. As with other tool windows, you can start typing the name of an image or container to highlight the matching items.
Managing images
Docker images are executable packages for running containers. Depending on your development needs, you can use Docker for the following:
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, runtime environments, and so on. There are other public and private Docker registries, and you can also deploy your own registry server.
Configure a Docker registry
Press ⌃ ⌥ S to open the IDE settings and select Build, Execution, Deployment | Docker | Registry.
Click to add a Docker registry configuration and specify how to connect to the registry. If you specify the credentials, AppCode will automatically check the connection to the registry. The Connection successful message should appear at the bottom of the dialog.
Pull an image from a Docker registry
In the Services tool window, select the Images node.
Select the Docker registry and specify the repository and tag (name and version of the image, for example,
postgres:latest
.Press ⌃ ⏎ to run docker pull.
Build an image from a Dockerfile
When you are editing a Dockerfile, AppCode provides completion for images from the configured registries. You can also hold down Ctrl and click an image name to open its page in a web browser.
Open the Dockerfile from which you want to build the image.
Click in the gutter and select to build the image.
AppCode runs the docker build command.
Push an image to a Docker registry
In the Services tool window, select the image that you want to upload and click or select Push Image from the context menu.
Select the Docker registry and specify the repository and tag (name and version of the image, for example,
my-app:v2
).Click OK to run the docker push command.
AppCode stores images that you pull or build locally and lists them in the Services tool window under Images. When you select an image, you can view its ID or copy it to the clipboard by clicking on the Properties tab.
To display detailed information about an image, right-click it and select Inspect from the context menu. AppCode runs the docker image inspect command and prints the output to the Inspection tab.
Images with no tags <none>:<none>
can be one of the following:
Intermediate images that serve as layers for other images and do not take up any space
Dangling images that remain when you rebuild an image based on a newer version of another image. You should regularly prune dangling images to preserve disk space.
To hide untagged images from the list, click on the Docker toolbar, and then click Untagged Images to remove the check mark.
To delete one or several images, select them in the list and click .
Running containers
Docker containers are runtime instances of the corresponding images. AppCode uses run configurations to execute the commands that build Docker images and run containers. There are three types of Docker run configurations:
Docker Image: Created automatically when you run a container from an existing image. You can run it from a locally existing Docker image that you either pulled or built previously.
Dockerfile: Created automatically when you run a container from a Dockerfile. This configuration builds an image from the Dockerfile, and then derives a container from this image.
Docker-compose: Created automatically when you run a multi-container Docker application from a Docker Compose file.
Run a container from an existing image
In the Services tool window, select an image and click or select Create Container from the context menu.
In the Create Container popup, click Create....
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.
When you are done, click Run to launch the new configuration.
Run a container from a Dockerfile
Open the Dockerfile from which you want to run the container.
Click in the gutter and select to run the container from this Dockerfile.
This creates and starts a run configuration with default settings, which builds an image based on the Dockerfile and then runs a container based on this image.
To create a run configuration with custom settings, click in the gutter and select New Run Configuration.... 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.
Docker run configurations
Create a Docker run configuration
From the main menu, select
.In the Run/Debug Configurations dialog, click , point to Docker, and then click the desired type of run configuration.
Use this type of configuration to run a Docker container from a locally existing image that you either pulled or built previously.
Docker uses the docker run command with the following syntax:
You can set all the arguments for this command using the options of the Docker Image run configuration.
By default, the Docker Image configuration has the following options:
Item | Description |
---|---|
Name | Specify a name for the run configuration to quickly identify it among others when editing or running. |
Allow parallel run | Allow running multiple instances of this run configuration in parallel. By default, it is disabled, and when you start this configuration while another instance is still running, AppCode suggests stopping the running instance and starting another one. This is helpful when a run configuration consumes a lot of resources and there is no good reason to run multiple instances. |
Store as project file | Save the file with the run configuration settings to share it with other team members. The default location is .idea/runConfigurations. However, if you do not want to share the .idea directory, you can save the configuration to any other directory within the project. By default, it is disabled, and AppCode stores run configuration settings in .idea/workspace.xml. |
Server | Select the Docker daemon connection to use for the run configuration. |
Image ID or name | Specify the identifier or the name of the Docker image from which to create the container. |
Container name | Specify an optional name for the container. If empty, Docker will generate a random name for the container. This is similar to using the |
Before launch | Specify a list of tasks to perform before starting the run configuration. For example, compile some code and build the necessary artifacts, run some external tool or a web browser, and so on. Click or press ⌘ N to add one of the available tasks. Move tasks in the list using and to change the order in which to perform the tasks. Select a task an click to edit the task. Click to remove the selected task from the list. |
Show this page | Show the run configuration settings before actually starting it. |
Activate tool window | Open the Services tool window when you start this run configuration. When disabled, you can open the tool window manually to see the running container: or ⌥ 8. |
Use the Modify options menu to add advanced options to the run configuration:
Item | Description |
---|---|
Randomly publish all exposed ports | Publish all exposed container ports to random free ports on the host. This is similar to using the |
Bind ports | Map specific container ports to specific ports on the host. This is similar to using the Click in the Bind ports field and specify 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 Lets say you already have CouchDB running on the host port 5984, and you want to run another instance of CouchDB in a container and access it from the host via port 5985. Binding the host port 5985 to port 5984 in the container is similar to setting the following command-line option: -p 5985:5984 You can set this option explicitly in the Run options field instead of configuring the Bind ports field. |
Entrypoint | Override the default This is similar to using the |
Command | Override the default This is similar to adding the command as an argument for |
Bind mounts | Mount files and directories on the host to a specific location in the container. This is similar to using the Click in the Bind mounts field and specify 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. For example, you can mount a local CouchDB directory on the host (Users/Shared/couchdb_data) to some directory inside the container (/var/lib/couchdb/data). Mounting volumes in this manner is similar to setting the following command-line option: -v Users/Shared/couchdb_data:/var/lib/couchdb/data You can set this option explicitly in the Run options field instead of configuring the Bind mounts field. |
Environment variables | Specify environment variables. There are environment variables associated with the base image that you are using as defined by the This is similar to using the Click in the Environment variables field to add names and values for variables. For example, if you want to connect to CouchDB with a specific username by default (instead of the operating system name of the user running the application), you can set the --env COUCHDB_USER=%env-var-value You can set this option explicitly in the Run options field instead of configuring the Environment variables field. |
Run options | Set any other supported docker run options. For example, to connect the container to the --network my-net --network-alias my-app |
Attach to container | Attach to the container's standard input, output, and error streams. This is similar to using the |
Show command preview | Preview the resulting command that will be used to execute the run configuration. |
Use this type of configuration to build an image from a Dockerfile and then derive a container from this image.
Docker uses the docker build command to build an image from a Dockerfile, and then the docker run command to start a container from it.
By default, the Dockerfile configuration has the following options:
Item | Description |
---|---|
Name | Specify a name for the run configuration to quickly identify it among others when editing or running. |
Allow parallel run | Allow running multiple instances of this run configuration in parallel. By default, it is disabled, and when you start this configuration while another instance is still running, AppCode suggests stopping the running instance and starting another one. This is helpful when a run configuration consumes a lot of resources and there is no good reason to run multiple instances. |
Store as project file | Save the file with the run configuration settings to share it with other team members. The default location is .idea/runConfigurations. However, if you do not want to share the .idea directory, you can save the configuration to any other directory within the project. By default, it is disabled, and AppCode stores run configuration settings in .idea/workspace.xml. |
Server | Select the Docker daemon connection to use for the run configuration. |
Dockerfile | Specify the name and location of the Dockerfile used to build the image. |
Image tag | Specify an optional name and tag for the built image. This can be helpful for referring to the image in the future. If you leave the field blank, the image will have only a random unique identifier. |
Container name | Specify an optional name for the container. If empty, Docker will generate a random name for the container. This is similar to using the |
Before launch | Specify a list of tasks to perform before starting the run configuration. For example, compile some code and build the necessary artifacts, run some external tool or a web browser, and so on. Click or press ⌘ N to add one of the available tasks. Move tasks in the list using and to change the order in which to perform the tasks. Select a task an click to edit the task. Click to remove the selected task from the list. |
Show this page | Show the run configuration settings before actually starting it. |
Activate tool window | Open the Services tool window when you start this run configuration. When disabled, you can open the tool window manually to see the running container: or ⌥ 8. |
Use the Modify options menu to add advanced options to the run configuration:
Item | Description |
---|---|
Context folder | Specify a local directory that the daemon will use during the build process. All host paths in the Dockerfile will be processed relative to this directory. By default, if you leave it blank, Docker uses the same directory where the Dockerfile is located. |
Build args | Specify values for build-time variable that can be accessed like regular environment variables during the build process but do not persist in the intermediate or final images. This is similar to using the These variables must be defined in the Dockerfile with the
ARG PGTAG=latest
FROM postgres:$PGTAG
The Redefining the --build-arg PGTAG=9 |
Build options | Set any other supported For example, you can specify metadata for the built image with the |
Enable BuildKit (experimental) | Use the BuildKit backend when building the images. This is similar to setting the |
Randomly publish all exposed ports | Publish all exposed container ports to random free ports on the host. This is similar to using the |
Bind ports | Map specific container ports to specific ports on the host. This is similar to using the Click in the Bind ports field and specify 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 Lets say you already have CouchDB running on the host port 5984, and you want to run another instance of CouchDB in a container and access it from the host via port 5985. Binding the host port 5985 to port 5984 in the container is similar to setting the following command-line option: -p 5985:5984 You can set this option explicitly in the Run options field instead of configuring the Bind ports field. |
Entrypoint | Override the default This is similar to using the |
Command | Override the default This is similar to adding the command as an argument for |
Bind mounts | Mount files and directories on the host to a specific location in the container. This is similar to using the Click in the Bind mounts field and specify 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. For example, you can mount a local CouchDB directory on the host (Users/Shared/couchdb_data) to some directory inside the container (/var/lib/couchdb/data). Mounting volumes in this manner is similar to setting the following command-line option: -v Users/Shared/couchdb_data:/var/lib/couchdb/data You can set this option explicitly in the Run options field instead of configuring the Bind mounts field. |
Environment variables | Specify environment variables. There are environment variables associated with the base image that you are using as defined by the This is similar to using the Click in the Environment variables field to add names and values for variables. For example, if you want to connect to CouchDB with a specific username by default (instead of the operating system name of the user running the application), you can set the --env COUCHDB_USER=%env-var-value You can set this option explicitly in the Run options field instead of configuring the Environment variables field. |
Run options | Set any other supported docker run options. For example, to connect the container to the --network my-net --network-alias my-app |
Attach to container | Attach to the container's standard input, output, and error streams. This is similar to using the |
Show command preview | Preview the resulting command that will be used to execute the run configuration. |
Use this type of configuration to run multi-container Docker applications.
Docker uses the docker-compose command to define, configure, and run multi-container applications. The main command that builds, creates, starts, and attaches to containers is docker-compose up.
By default, the Docker-compose configuration has the following options:
Item | Description |
---|---|
Name | Specify a name for the run configuration to quickly identify it among others when editing or running. |
Allow parallel run | Allow running multiple instances of this run configuration in parallel. By default, it is disabled, and when you start this configuration while another instance is still running, AppCode suggests stopping the running instance and starting another one. This is helpful when a run configuration consumes a lot of resources and there is no good reason to run multiple instances. |
Store as project file | Save the file with the run configuration settings to share it with other team members. The default location is .idea/runConfigurations. However, if you do not want to share the .idea directory, you can save the configuration to any other directory within the project. By default, it is disabled, and AppCode stores run configuration settings in .idea/workspace.xml. |
Server | Select the Docker daemon connection to use for the run configuration. |
Compose files | Specify the compose files that define the necessary services. Docker Compose builds the configuration in the specified order, so any subsequent files override and add to the fields of the same service in previous files. This is similar to using the |
Services | Specify the services to build, create, and start. Click the Browse icon () to select services that are listed in the YML file. |
Before launch | Specify a list of tasks to perform before starting the run configuration. For example, compile some code and build the necessary artifacts, run some external tool or a web browser, and so on. Click or press ⌘ N to add one of the available tasks. Move tasks in the list using and to change the order in which to perform the tasks. Select a task an click to edit the task. Click to remove the selected task from the list. |
Show this page | Show the run configuration settings before actually starting it. |
Activate tool window | Open the Services tool window when you start this run configuration. When disabled, you can open the tool window manually to see the running container: or ⌥ 8. |
Use the Modify options menu to add advanced options to the run configuration:
Item | Description |
---|---|
Project name | Specify an alternate project name for Docker Compose. By default, it is the name of the current directory. This is similar to using the |
Environment variables | Specify the Docker Compose environment variables. These are used only by the Docker Compose process. They are not passed on to any of the containers. |
Environment variables file | Specify the path to a custom environment file that defines the Docker Compose environment variables. By default, Docker Compose looks for a file named .env in the project directory. This is similar to using the |
Enable BuildKit (experimental) | Use the BuildKit backend when building the images. This is similar to setting the |
Remove volumes on `down` | When stopping and removing containers, also delete named volumes declared in the Docker Compose file and anonymous volumes attached to containers. This is similar to using the |
Enable compatibility mode | Convert v3 service definitions into v2 compatible parameters. This is similar to using the |
Remove images on `down` | Configure which images should be removed when stopping and removing containers. You can choose to remove all images used by any service or only images that don't have a custom tag set in the This is similar to using the |
Timeout | Set a timeout in seconds to forcefully terminate containers that won't shutdown gracefully. Docker usually tries to gracefully terminate any container with This is similar to using the |
Exit code | Return the exit code of the selected service container. Whenever a container in the selected service stops, return its exit code and stop all other containers in the service. This is similar to using the |
Override scale | Set the number of containers to start for each service. This option overrides the This is similar to using the |
Recreate dependencies | Recreate dependent containers when starting a service. This is similar to using the |
Recreate anonymous volumes | Recreate anonymous volumes instead of retrieving data from the previous containers. This is similar to using the |
Remove orphans | Remove containers for services not defined in the Docker Compose file. This is similar to using the |
Start | Configure which services to start:
|
Attach to | Configure for which containers to show output streams:
|
Recreate containers | Configure which containers to stop and replace by new ones:
|
Build | Configure which images to build before starting containers:
|
Stop Containers | Configure how to stop containers in a service. By default, Docker Compose doesn't stop other containers in a service. You have to stop them manually. However, you can choose to stop all containers if any container in a service stops. This is similar to using the |
Interacting with containers
Created containers are listed in the Services tool window. By default, the Services tool window displays all containers, including those that are not running. To hide stopped containers from the list, click in the toolbar, select Docker, and then click Stopped Containers to remove the checkbox.
When you select a container, you can view the following tabs by default:
Build Log | Shows the deployment log produced by the corresponding Docker run configuration while building the image for the container. |
Log | Shows the log messages from the container's standard output streams: For more information, see the docker logs command reference. |
Properties | Shows the name and ID of the container, and the ID of the corresponding image. To copy any property to the clipboard, select it and click . You can also specify a new name for the container and click Save. If the container is running, it will stop and get a new name. To run the container with the new name, select it and click . However, the name does not change in the Docker run configuration that is used to create this container. |
Environment Variables | Shows the environment variables configured for the container. To add a new variable, click . To remove a variable, select it and click . To edit an existing variable, select it and click . When you make any changes and click Save, if the container is running, it will stop. To run this container with the new set of variables, select it and click . However, these variables don't change in the Docker run configuration that is used to create this container. |
Port Bindings | Show the port bindings configured for this container. Select Publish all ports to bind all exposed container ports to random free ports on the host. To add a new port binding, click . To remove a port binding, select it and click . To edit an existing port binding, select it and click . When you make any changes and click Save, if the container is running, it will stop. To run this container with the new port bindings, select it and click . However, these bindings don't change in the Docker run configuration that is used to create this container. |
Volume Bindings | Show the volume bindings configured for this container. To add a new volume binding, click . To remove a volume binding, select it and click . To edit an existing volume binding, select it and click . When you make any changes and click Save, if the container is running, it will stop. To run this container with the new volume bindings, select it and click . However, these bindings don't change in the Docker run configuration that is used to create this container. |
Files | Browse the files inside a running container. Select any file and click to open it remotely in the editor or click to create a copy of the file as a scratch. |
Execute a command inside a running container
In the Services tool window, right-click the container name and then click Exec.
In the Run Command in Container popup, click Create and Run... to create and execute a new command.
Alternatively, you can select one of the commands that you ran previously.
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
For more information, see the docker exec command reference.
View detailed information about a running container
In the Services tool window, right-click the container name and then click Inspect.
The output is rendered as a JSON array on the Inspection tab.
For more information, see the docker inspect command reference.
View processes running in a container
In the Services 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 Services 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, a 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
Define necessary services in one or several Docker Compose files.
From the main menu, select
.Click , point to Docker and then click Docker-compose.
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).For more information about the available options, see Docker-compose.
Click OK to save the Docker Compose run configuration, select it in the main toolbar and click or press ⇧ F10 to start the configuration.
When Docker Compose runs your multi-container application, you can use the Services 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
In the Services tool window, select the service you want to scale and click or select Scale from the context menu.
Specify how many containers you want for this service and click OK.
Stop a running service
In the Services tool window, select the service and click or select Stop from the context menu.
Stop all running services
In the Services tool window, select the Compose node and click or select Stop from the context menu.
Bring your application down
In the Services tool window, select the Compose node and click or select Down from the context menu.
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 Services tool window, right-click the Compose node or a nested service node and then click Jump to Source in the context menu or press 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:
Docker is running.
Your Docker connection settings are correct.
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 Preferences dialog ⌃ ⌥ S under .
Unable to use Docker Compose
Make sure that the Docker Compose executable is specified correctly in the Preferences dialog ⌃ ⌥ S under .
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, AppCode 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 tool window and select Associate with File Type from the context menu.
If the Associate with File Type actions is disabled, this probably means that the filename 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:
Press ⌃ ⌥ S to open the IDE settings and select Editor | File Types.
Select the relevant file type (in this case: Text) and remove the pattern with the name of the file.
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 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.