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.
Join the DZone community and get the full member experience.
Join For FreeIn 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
+-------------------+
| 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:
docker pull amazonlinux:2023
2. Create a Dockerfile
Create a directory for your project and inside it, create a 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:
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:
{
"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...
underProject SDK
, then selectJDK
and navigate to/usr/lib/jvm/java-17-openjdk
within your Docker container. - Alternatively, you can configure this through the terminal by running:
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 newApplication
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:
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!
Opinions expressed by DZone contributors are their own.
Comments