Mac IntelliJ - Cannot run program “docker”: error=2, No such file or directory

本文介绍了解决在MacOSX上使用IntelliJ IDEA运行Gradle任务时遇到的Docker路径环境变量错误的方法。通过修改启动方式、调整GUI应用的PATH变量或编辑IntelliJ IDEA应用包,可以确保IDE正确识别Docker命令。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

fix PATH environment variable for IntelliJ IDEA on Mac OS X

what is the problem?

Recently I wrote a Gradle plugin which uses the docker client of the local machine. When running Gradle from the terminal everything is fine. However, when I import the project into my IntelliJ IDEA and try to run the Gradle tasks using IntelliJ IDEA I keep getting the following error:

Cannot run program “docker”: error=2, No such file or directory

It seems, I am not the only one having this problem:

why the problem exists?

The cause of the problem is a different set of environment variables for terminal and GUI applications in Mac OS X. As the docker client is usually installed via brew install docker, the binary is located in /usr/local/bin. However, the PATH environment for GUI applications is limited to /usr/bin:/bin:/usr/sbin:/sbin by default. That means, the docker command is not available to IntelliJ IDEA.

how to solve the problem?

The easiest way is to start IntelliJ IDEA from the terminal:

open -a "IntelliJ IDEA 15"

That way, IntelliJ IDEA uses the same environment variables as declared in your terminal.

However, if you still want to start IntelliJ IDEA using Spotlight, Dock etc., you have to do a bit more.

Another way is to change the PATH variable for GUI applications using the following terminal command:

sudo launchctl config user path $PATH

This will set the PATH variable to the value of your terminal. However, you have to restart your Mac and this change will affect all of your GUI applications.

The last way is to edit the application package of IntelliJ IDEA. To spare you from messing around with the package, I wrote a small script that does all the editing for you:

As you can see, the script accepts two input parameters: the absolute path to the IntelliJ IDEA application package (e.g. /Applications/IntelliJ IDEA 15.app) and the value of the PATH variable to set.

To apply the changes, download the script, make it executable and run it as follows:

curl https://gist.githubusercontent.com/depressiveRobot/9cb8f799c970f0cd57ea/raw/964253533dd46e0202c4873468b3a1ef304b0af1/osx-intellij-set-path.sh > osx-intellij-set-path.sh
chmod +x osx-intellij-set-path.sh
./osx-intellij-set-path.sh "/Applications/IntelliJ IDEA 15.app" "$PATH"

This will set the PATH variable for IntelliJ IDEA to the value of your terminal. With a few minor adjustments this should be portable to the other Jetbrains editors, too, such as WebStorm and even Android Studio.

Whichever way you choose, now you’re able to run external commands with IntelliJ. Just like as you know it from the terminal ;)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值