IntelliJ: use docker jvm or docker maven?

2019-02-07 14:28发布

问题:

I have a docker/jvm instance, which I use from the command line to compile and run java code. IntelliJ's project config requires me to point at a jvm on the filesystem.

So, I was wondering, can I configure intellij to use this docker container? I suppose I could configure a docker container, keep it running, and mount/share its filesystem, but I don't want that -- I want to use my ephemeral container instance.

I have the same idea for using maven -- can I use a docker instance of maven without mounting the filesystem, from within intellij? Again, intellij seems to require a pointer to a filesystem location for maven, so this seems problematic.

Does anyone have any clue if this is possible, or how to go about configuring such a thing?

回答1:

Again, intellij seems to require a pointer to a filesystem location for maven, so this seems problematic.

The only way for an application to use another containerized one would be for that application itself to be in the same container (or to mount a data volume from another container, which avoids mounting the local filesystem)

So using something like dlsniper/docker-intellij/ (used for go, but easily adapted for java) would work, and allow an IntelliJ container to

  • share the X11 socket in order to see the containerized application on the host desktop,
  • uses data volumes (again, without mounting a local filesystem directly):
    • data-volume for the jvm
    • data-volume for the java sources


回答2:

If you're on a posix system, you can make a simple executable shell script that takes command line argument and forwards those to:

docker exec [OPTIONS] CONTAINER COMMAND [ARGS...]

This may even work on Windows with a batch file.

One problem is when you have to pass in file paths. A solution could be to mount the working directory at an identical path in the container, as on the host.

Don't forget the shebang if you go for a shell script.

On Windows, a filepath may be 'unixified' in a batch file like this:

set FILEPATH=%FILEPATH:C:=/c%
set FILEPATH=%FILEPATH:\=/%