Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I develop in docker container with intellij?

I know intellij has a docker container plugin, however it doesn't seem to allow me to develop inside the container itself. The idea is simple, I don't want to configure my host to have the correct environment tools. I'd rather just a docker container setup and then use intellij to find libs, functionality and such with in the container itself.

This would be incredibly helpful for c++, java, and scala dev. Also it would be useful debugging as well.

So is it possible to develop within a docker container with intellij?

like image 548
Dr.Knowitall Avatar asked Dec 28 '16 19:12

Dr.Knowitall


People also ask

Can you develop in a Docker container?

Developing inside a Docker container generally means starting a container and leaving it running, while you edit your source code. As you make changes, you see the changes appear in the container. To get your source code inside a container, you can use something called a bind mount.

How do I debug a Docker container in IntelliJ?

Create a remote debug configuration From the main menu, select Run | Edit Configurations. and select Remote JVM Debug. and select Launch Docker Before Debug. Make sure that the ports in the Configure Docker dialog and in the remote debug configuration match.


4 Answers

So you just want to work within a container just as you would within a full-blown VM, right? Then you should just run a container, attach a display (to run IDEA) and start configuring your development environment.

For the display part I'd test some answers given in Can you run GUI apps in a docker container?. There are some very cool answers in this topic showing various approaches to running GUI apps within a container.

like image 74
jannis Avatar answered Oct 03 '22 18:10

jannis


Shouldn't the approach be rather: Have local repository and local IDE. In the repository have docker file and eventually docker-compose.yml, which spins up environment required to run project. Mount your local drive with sources into docker (volumes), so changes done in your local folder are reflected in docker, similar in other direction.

like image 40
Kamila Avatar answered Oct 05 '22 18:10

Kamila


Please look at this example for Intellij IDEA CI and JDK8 based on Alpine Linux (taken here https://raw.githubusercontent.com/shaharv/docker/master/alpine/dev/Dockerfile)

# Alpine 3.8 C++/Java Developer Image
#
# For IntelliJ and GUI (X11), run the image with:
# $ XSOCK=/tmp/.X11-unix && sudo docker run -i -v $XSOCK:$XSOCK -e DISPLAY -u developer -t [image-name]
#
# Then run IntelliJ with:
# /idea-IC-191.6707.61/bin/idea.sh

FROM alpine:3.8

ENV LANG C.UTF-8

RUN set -ex && \
    apk add --no-cache --update \
    # basic packages
        bash bash-completion coreutils file grep openssl openssh nano sudo tar xz \
    # debug tools
        gdb musl-dbg strace \
    # docs and man
        bash-doc man man-pages less less-doc \
    # GUI fonts
        font-noto \
    # user utils
        shadow

RUN set -ex && \
    apk add --no-cache --update \
    # C++ build tools
        cmake g++ git linux-headers libpthread-stubs make

RUN set -ex && \
    apk add --no-cache --update \
    # Java tools
        gradle openjdk8 openjdk8-dbg

# Install IntelliJ Community
RUN set -ex && \
    wget https://download-cf.jetbrains.com/idea/ideaIC-2019.1.1-no-jbr.tar.gz && \
    tar -xf ideaIC-2019.1.1-no-jbr.tar.gz && \
    rm ideaIC-2019.1.1-no-jbr.tar.gz

# Create a new user with no password
ENV USERNAME developer
RUN set -ex && \
    useradd --create-home --key MAIL_DIR=/dev/null --shell /bin/bash $USERNAME && \
    passwd -d $USERNAME

# Set additional environment variables
ENV JAVA_HOME /usr/lib/jvm/java-1.8-openjdk
ENV JDK_HOME  /usr/lib/jvm/java-1.8-openjdk
ENV JAVA_EXE  /usr/lib/jvm/java-1.8-openjdk/bin/java
like image 26
humkins Avatar answered Oct 04 '22 18:10

humkins


There is a better way to do this now with Jetbrains Gateway. Just make sure you have OpenSSH server installed (latest Ubuntu containers have this already installed) in the container that you initially ran with exposed ports, i.e. -p 220:22 (I like 220) and the SSH service running, i.e. service ssh start, after modifying the /etc/ssh/sshd_config to enable root login and password authentication then service ssh restart. Make sure you set a password for the root user, i.e. passwd root, (or go through other steps to setup a new user). Then all you need to do is open Jetbrains Gateway, and SSH to the container with the fields set thus: user=root, host=localhost, and port=220 (or whatever you chose); note, you will also need to specify a project location, which in my use case is a Java application repository root directory -- this means you will need to have Java and Maven or whatever other tools installed in the container at some point, but doesn't affect ability to connect. Assuming you connect with no issues you will see activity whereby Gateway installs an IDE backend inside the container (takes about 10 minutes) and then starts up a IDE client which is a light version of IntelliJ (or whatever other IDE version you selected) that is honestly a bit buggy at time of writing. But it works and has unblocked some of my colleagues stuck with Windows machines and not many options to upgrade to Macs in the current chip shortage environment. Note that any time you restart the container you also need to restart the SSH service unless you script it to automatically start up when the container does.

like image 40
reka18 Avatar answered Oct 05 '22 18:10

reka18