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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Analyzing “java.lang.OutOfMemoryError: Failed to create a thread” Error
  • Buildpacks: An Open-Source Alternative to Chainguard
  • How GitHub Codespaces Helps in Reducing Development Setup Time
  • Getting Started With NCache Java Edition (Using Docker)

Trending

  • Unlocking Data with Language: Real-World Applications of Text-to-SQL Interfaces
  • Comprehensive Guide to Property-Based Testing in Go: Principles and Implementation
  • Blue Skies Ahead: An AI Case Study on LLM Use for a Graph Theory Related Application
  • Concourse CI/CD Pipeline: Webhook Triggers
  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
14.8K 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

  • Analyzing “java.lang.OutOfMemoryError: Failed to create a thread” Error
  • Buildpacks: An Open-Source Alternative to Chainguard
  • How GitHub Codespaces Helps in Reducing Development Setup Time
  • Getting Started With NCache Java Edition (Using Docker)

Partner Resources

×

Comments
Oops! Something Went Wrong

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

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!