DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Solving the Mystery: Why Java RSS Grows in Docker on M1 Macs
  • Java Backend Development in the Era of Kubernetes and Docker
  • Java in a Container: Efficient Development and Deployment With Docker
  • TOP-5 Lightweight Linux Distributions for Container Base Images

Trending

  • LLM-Powered Deep Parsing for Industrial Inventory Search
  • Genkit Middleware: Intercept, Extend, and Harden your Gen AI Pipelines
  • How AI Is Transforming Software Engineering and How Developers Can Take Advantage
  • Building a Production-Ready AI Agent in 2026: Beyond the Hello World Demo
  1. DZone
  2. Software Design and Architecture
  3. Containers
  4. Setting Up a Local Development Environment With IntelliJ, DevContainers, and Amazon Linux 2023

Setting Up a Local Development Environment With IntelliJ, DevContainers, and Amazon Linux 2023

This tutorial explains how to set up a Java development environment with IntelliJ, DevContainers, and Amazon Linux 2023 for consistent, isolated development.

By 
Mohammed Fazalullah Qudrath user avatar
Mohammed Fazalullah Qudrath
·
Jul. 19, 24 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
16.9K Views

Join the DZone community and get the full member experience.

Join For Free

In modern software development, containerization offers an isolated and consistent environment, which is crucial for maintaining parity between development and production setups. This guide provides a comprehensive walkthrough on creating a local development environment using IntelliJ IDEA, DevContainers, and Amazon Linux 2023 for Java development.

Why Use DevContainers?

What Are DevContainers?

DevContainers are a feature provided by Visual Studio Code and other IDEs like IntelliJ IDEA through extensions. They allow you to define a consistent and reproducible development environment using Docker containers. By encapsulating the development environment, you ensure that all team members work in an identical setup, avoiding the "it works on my machine" problem.

Benefits of DevContainers

  • Consistency: Every developer uses the same development environment, eliminating discrepancies due to different setups.
  • Isolation: Dependencies and configurations are isolated from the host machine, preventing conflicts.
  • Portability: Easily share development environments through version-controlled configuration files.
  • Scalability: Quickly scale environments by creating new containers or replicating existing ones.

Diagram of DevContainers Workflow

Plain Text
 
+-------------------+

| Developer Machine |

+-------------------+

          |

          | Uses

          v

+-----------------------+

|   Development Tools   |

|  (IntelliJ, VS Code)  |

+-----------------------+

          |

          | Connects to

          v

+-----------------------+

|    DevContainer       |

|   (Docker Container)  |

+-----------------------+

          |

          | Runs

          v

+-----------------------+

|  Development Project  |

| (Amazon Linux 2023,   |

|  Java, Dependencies)  |

+-----------------------+


Step-By-Step Guide

Prerequisites

Before starting, ensure you have the following installed on your machine:

  • Docker: Install Docker
  • IntelliJ IDEA: Download IntelliJ IDEA
  • Visual Studio Code (optional, for DevContainer configuration): Download VS Code

Step 1: Setting Up Docker and Amazon Linux 2023 Container

1. Pull Amazon Linux 2023 Image

Open a terminal and pull the Amazon Linux 2023 image from Docker Hub:

Shell
 
   docker pull amazonlinux:2023


2. Create a Dockerfile

Create a directory for your project and inside it, create a Dockerfile:

Dockerfile
 
   FROM amazonlinux:2023



   # Install necessary packages

   RUN yum update -y && \

       yum install -y java-17-openjdk-devel git vim



   # Set environment variables

   ENV JAVA_HOME /usr/lib/jvm/java-17-openjdk

   ENV PATH $JAVA_HOME/bin:$PATH



   # Create a user for development

   RUN useradd -ms /bin/bash developer

   USER developer

   WORKDIR /home/developer


3. Build the Docker Image

Build the Docker image using the following command:

Shell
 
   docker build -t amazonlinux-java-dev:latest .


Step 2: Configuring DevContainers

1. Create a DevContainer Configuration

Inside your project directory, create a .devcontainer directory. Within it, create a devcontainer.json file:

JSON
 
   {

       "name": "Amazon Linux 2023 Java Development",

       "image": "amazonlinux-java-dev:latest",

       "settings": {

           "java.home": "/usr/lib/jvm/java-17-openjdk",

           "java.jdt.ls.java.home": "/usr/lib/jvm/java-17-openjdk"

       },

       "extensions": [

           "vscjava.vscode-java-pack"

       ],

       "postCreateCommand": "git clone https://github.com/your-repo/your-project ."

   }


2. Optional: Configure VS Code for DevContainers

If using VS Code, ensure the DevContainers extension is installed. Open your project in VS Code and select "Reopen in Container" when prompted.

Step 3: Setting Up IntelliJ IDEA

1. Open IntelliJ IDEA

Open IntelliJ IDEA and navigate to File > New > Project from Existing Sources.... Select your project directory.

2. Configure Remote Development

IntelliJ offers remote development capabilities, but since we're using DevContainers, we'll set up the project to work with our local Docker container.

3. Configure Java SDK

  • Navigate to File > Project Structure > Project.
  • Click New... under Project SDK, then select JDK and navigate to /usr/lib/jvm/java-17-openjdk within your Docker container.
  • Alternatively, you can configure this through the terminal by running:
Shell
 
     docker exec -it <container_id> /bin/bash


... and then configuring the path inside the container.

4. Import Project

IntelliJ should automatically detect the project settings. Make sure the project SDK is set to the Java version inside the container.

Step 4: Running and Debugging Your Java Application

1. Run Configuration

  • Navigate to Run > Edit Configurations....
  • Click the + button and add a new Application configuration.
  • Set the main class to your main application class.
  • Set the JRE to the one configured inside the container.

2. Run the Application

You should now be able to run and debug your Java application within the containerized environment directly from IntelliJ.

Step 5: Integrating With Git

1. Clone Repository

If not already cloned, use the following command to clone your repository inside the container:

Shell
 
   git clone https://github.com/your-repo/your-project .


2. Configure Git in IntelliJ

  • Navigate to File > Settings > Version Control > Git.
  • Ensure the path to the Git executable is correctly set, usually /usr/bin/git within the container.

Conclusion

By following this guide, you now have a robust, isolated development environment for Java development using IntelliJ, DevContainers, and Amazon Linux 2023. This setup ensures consistency across development and production, reducing the "it works on my machine" syndrome and improving overall development workflow efficiency.

Remember, containerization and DevContainers are powerful tools that can significantly streamline your development process. Happy coding!

Visual Studio Code Docker (software) intellij Java (programming language) Linux (operating system)

Opinions expressed by DZone contributors are their own.

Related

  • Solving the Mystery: Why Java RSS Grows in Docker on M1 Macs
  • Java Backend Development in the Era of Kubernetes and Docker
  • Java in a Container: Efficient Development and Deployment With Docker
  • TOP-5 Lightweight Linux Distributions for Container Base Images

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook