Creating a Mobile Coding Workspace Using Docker and IntelliJ

In modern software engineering, managing your development environment can often become a tedious and complicated task. Although integrated development environments (IDEs) like IntelliJ IDEA help significantly by setting up much of the required environment, developers still need to install specific versions of compilers, runtime environments, databases, and even operating systems. Each layer of complexity adds more room for errors, particularly those frustrating hidden dependencies that seem impossible to track down.


In a team setting, these complications multiply. Ensuring that all team members are working with the same environment is critical for consistent behavior of the application. Minor discrepancies between versions can lead to considerable bugs and inconsistencies. Adding further complexity, there’s the difference between development machines and production servers, which often run on diverse systems and configurations.


This article will guide you through creating a portable development environment using Docker and IntelliJ IDEA. By the end, you’ll understand how to manage dependencies, set up consistent environments, and even debug your applications in Docker, all within the familiar setting of IntelliJ IDEA.


Why Choose Docker for Your Development Environment?

Docker is a lightweight virtualization tool that enables consistent and predictable behavior across different environments. It's an OS-level virtualization engine where your application and all its dependencies are packaged into an "image." This image runs independently of the host system, ensuring that the application behaves the same way everywhere it is deployed.


With Docker, you don't need to worry about dependency conflicts or system discrepancies. Everyone on your team can use the same Dockerfile to create identical environments. This allows you to develop, test, and deploy code with confidence that if it works on your development machine, it will work in production.


Benefits of Docker:

  • Isolation: Each container is isolated, ensuring that dependencies and environment variables don't interfere with each other.
  • Immutability: Images are immutable and versioned, ensuring consistency across deployments.
  • Reproducibility: Build once, run anywhere. The same Docker image can run on any system that supports Docker.
  • Simplified DevOps: Docker can seamlessly integrate with CI/CD pipelines, making deployment faster and more reliable.


Getting Started with Docker

First, you need to download and install Docker. You can find Docker installations for Windows, Mac, and several Linux distributions at Docker's official website. Follow the instructions specific to your operating system to complete the installation process.


Installing Docker on Different Systems:


Setting Up IntelliJ IDEA

IntelliJ IDEA is a powerful IDE for JVM-based languages like Java, Kotlin, and Scala. You can download the Community Edition for free from JetBrains. After installing IntelliJ IDEA, you need to add the Docker plugin to enable Docker support.


Installing Docker Plugin in IntelliJ:

  1. Open IntelliJ IDEA and go to File -> Settings -> Plugins.
  2. Search for "Docker" and click Install.

Integrating Docker with IntelliJ IDEA

For Docker to work seamlessly with IntelliJ, you need to add your user account to the docker-users group. This process varies slightly depending on your operating system:


Adding User to Docker-Users Group on Windows:

  1. Open Computer Management with administrative privileges.
  2. Navigate to Local Users and Groups -> Groups.
  3. Find docker-users, right-click, and select Add to Group.
  4. Add your user account and restart your system to apply the changes.

Creating and Using Docker Images

Once Docker and IntelliJ are set up, it's time to create Docker images for your application. Docker images are described using Dockerfiles, simple script files that outline the components required.


Example Dockerfile for a Spring Boot Application:

FROM openjdk:11-jdk-slim
VOLUME /tmp
ADD build/libs/your_app_file.jar app.jar
ADD build/resources/main/application.yml application.yml
ENV SPRING_PROFILES_ACTIVE=docker
ENTRYPOINT ["java","-jar","/app.jar"]

In this Dockerfile:


  • FROM specifies the base image – in this case, OpenJDK 11.
  • VOLUME creates a mount point for your application.
  • ADD copies necessary files from the host to the image.
  • ENV sets environment variables.
  • ENTRYPOINT defines the command to run within the container.


Running Docker Container from IntelliJ IDEA

After creating a Dockerfile, you can open it in IntelliJ. The IDE will automatically create a Run Configuration for the Docker container.


  1. Open Dockerfile in IntelliJ IDEA.
  2. Go to Run -> Edit Configurations.
  3. Select your Dockerfile configuration and click OK.
  4. Click Run to build and run the Docker container.

Networking Between Host and Docker Container

One of the convenient features of Docker is its ability to handle networking between the host and the container. However, by default, containers run in isolation, meaning you need to manually forward ports to access services running inside the container.


This is done via the Port Bindings section in your Docker Run Configuration. For example, if your Spring Boot application listens on port 8080, you need to bind this port to your host:


Port Binding Example:

8080:8080

Connecting to Localhost:

Be aware that localhost always refers to the container itself. If you need to connect to services on your host machine from within Docker, use:


  • docker.for.win.localhost on Windows.
  • docker.for.mac.localhost on Mac.
  • host.docker.internal, which works on both systems.


Setting Up Remote Debugging in Docker

Debugging in a Dockerized environment can be challenging, but IntelliJ IDEA provides support for remote debugging. You'll need to adjust your Dockerfile and IntelliJ configuration:


Modifying Dockerfile for Debugging:

ENTRYPOINT ["java","-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005","-jar","/app.jar"]

This command will start the JVM in debug mode, listening on port 5005. Make sure this port is also bound to your host machine.


Configuring IntelliJ for Remote Debugging:

  1. Go to Run -> Edit Configurations.
  2. Click + to add a new configuration and select Remote.
  3. Set the port to 5005 and apply the changes.
  4. Start your Docker container and then attach the remote debugger from IntelliJ.

Conclusion

Using Docker and IntelliJ IDEA to create a portable development environment can significantly alleviate the challenges of managing dependencies and environments across different machines. Although the tooling can sometimes be less mature, the consistency and predictability gained are substantial.


From setting up Docker and IntelliJ to configuring networking and debugging, this guide should provide you with a comprehensive understanding to begin leveraging the power of Docker in your development workflow.


For more detailed guides and best practices, consider subscribing to our newsletter and exploring related topics: