How can I develop in docker container with intellij?

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.


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.


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.


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