Ubuntu Dev Container: Java, Maven, MySQL, Tomcat Setup

Alex Johnson
-
Ubuntu Dev Container: Java, Maven, MySQL, Tomcat Setup

Hey guys! Ever found yourself wrestling with environment setups for your Java projects? Setting up the perfect development environment can be a real headache, especially when you need multiple tools like Java, Maven, MySQL, and Tomcat. But what if you could have a consistent, reproducible environment every time, without the hassle? That’s where Docker dev containers come in! In this guide, we’ll walk you through creating a developer-friendly Ubuntu dev container packed with all the essentials for Java development. Let's dive in and make your developer life a whole lot easier!

Objective: Building the adithyaekanayaka/ubuntu:dev Container

Our main goal here is to create a Docker container named adithyaekanayaka/ubuntu:dev based on the latest Ubuntu Server. This container will be pre-configured with everything you need for Java development, including Java 1.8, Maven, MySQL 8, and Tomcat. Think of it as your personal development playground, ready to go whenever you are. We’re aiming for an environment that’s not only functional but also easy to use and customize. So, buckle up, and let’s get started on building this awesome dev container!

Why Use a Dev Container?

Before we jump into the nitty-gritty, let’s quickly chat about why dev containers are a game-changer. Imagine you’re working on multiple projects, each needing different versions of Java or specific configurations. Dev containers allow you to encapsulate each project’s dependencies in its own isolated environment. This means no more compatibility issues or conflicts between projects! Plus, it ensures everyone on your team is working in the same environment, reducing those “it works on my machine” moments. It’s all about consistency, portability, and ease of use. Trust me, once you go dev container, you won’t want to go back!

Requirements: Setting the Stage

To make our adithyaekanayaka/ubuntu:dev container the ultimate Java development haven, we have a few key requirements to nail down. These requirements ensure our container is not only functional but also secure and user-friendly.

  1. Base Image: We're going with the latest Ubuntu Server (22.04 or 24.04 recommended). Ubuntu is a popular choice for its stability, extensive package availability, and strong community support. Starting with a solid base image is crucial for a smooth development experience.
  2. User Setup: We need an admin user with sudo privileges. For demo purposes, we’ll set the password to admin (but remember, change this for production!). Configuring the container to default to the admin user on login makes it super convenient for everyday use.
  3. Java Installation: We're installing OpenJDK 8 (java-1.8) and setting the JAVA_HOME environment variable. Java 8 is still widely used, so it’s a great starting point. Setting JAVA_HOME ensures Java-based tools know where to find the Java installation.
  4. Maven Installation: The latest stable version of Maven will be installed. We’ll verify the installation with mvn -v to make sure everything is working correctly. Maven is essential for managing Java project dependencies and builds.
  5. MySQL Installation: We'll install MySQL 8, set the root password (again, admin for demo, change for production!), and create an admin user if needed. Enabling remote connections might be necessary depending on your setup, so we’ll cover that too.
  6. Tomcat Installation: The latest Tomcat version compatible with Java 8 will be installed. We'll configure an admin user for the Tomcat manager and expose the necessary ports (like 8080). Tomcat is our go-to servlet container for deploying Java web applications.
  7. SSH Setup: OpenSSH will be installed and configured to allow secure remote access. We'll expose port 22 and enable password authentication for the admin user. Optionally, we might generate SSH keys for enhanced security.
  8. System Tools: Essential tools like git, curl, wget, build-essential, net-tools, and vim will be included. These tools are indispensable for development, debugging, and general system administration.
  9. Entrypoint & Service Management: We'll use supervisord or systemd to run MySQL, Tomcat, and SSH in the background. These tools ensure that our services start automatically and keep running smoothly. Systemd might be easier for developers to fine-tune for other needs.
  10. Container Image Publishing: Finally, we’ll tag the image as adithyaekanayaka/ubuntu:dev and push it to GitHub Container Registry: ghcr.io/adithyaekanayaka/ubuntu:dev. This makes our container accessible to anyone who needs it.

Diving into the Deliverables

To bring our dev container vision to life, we need a few key deliverables. These components will work together to create a seamless development environment.

1. The Dockerfile: Our Blueprint

The heart of our project is the Dockerfile. This file contains all the instructions Docker needs to build our container. Think of it as a recipe that lays out each step, from setting the base image to installing software and configuring services. A well-crafted Dockerfile is essential for reproducibility and consistency.

2. Supporting Scripts and Config Files

Sometimes, a Dockerfile alone isn't enough. We might need additional scripts or configuration files to handle more complex tasks. For example, we might use a supervisord config file to manage our services or a tomcat-users.xml file to set up Tomcat admin users. These supporting files ensure everything is configured exactly as we need it.

3. README.md: Your User Manual

A good README.md is crucial for anyone using our container. It should include clear usage instructions, default credentials (for demo purposes), and any other essential information. Think of it as a user manual that helps others (and your future self) get up and running quickly.

4. Example Command: Running the Container

We’ll provide an example command to pull and run the container. This command will support common architectures (arm, x86_64, etc.), making our container accessible to a wide range of developers. Here’s a sneak peek:

docker run -it --rm -p 22:22 -p 8080:8080 -p 3306:3306 ghcr.io/adithyaekanayaka/ubuntu:dev

This command maps ports 22 (SSH), 8080 (Tomcat), and 3306 (MySQL) from the container to the host machine, allowing us to access these services easily.

Step-by-Step Guide to Building the Dev Container

Alright, let’s get our hands dirty and walk through the steps of creating our adithyaekanayaka/ubuntu:dev container. We’ll break it down into manageable chunks, so it’s easy to follow along.

1. Setting Up the Base Image and User

First, we’ll start with the base Ubuntu image and set up our admin user. This involves specifying the base image in the Dockerfile, creating the user, setting the password, and granting sudo privileges.

FROM ubuntu:latest

# Set environment variables
ENV DEBIAN_FRONTEND=noninteractive

# Create admin user
RUN useradd -ms /bin/bash admin
RUN echo "admin:admin" | chpasswd
RUN usermod -aG sudo admin

# Set default user
USER admin
WORKDIR /home/admin

2. Installing Java and Maven

Next up, we’ll install OpenJDK 8 and Maven. This includes downloading and installing the packages, setting the JAVA_HOME environment variable, and verifying the Maven installation.

# Install Java 8
RUN apt-get update && apt-get install -y openjdk-8-jdk

# Set JAVA_HOME
ENV JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64
ENV PATH="${JAVA_HOME}/bin:${PATH}"

# Install Maven
RUN apt-get install -y maven

# Verify Maven installation
RUN mvn -v

3. Setting Up MySQL

Installing MySQL involves fetching the MySQL 8 packages, setting the root password, creating an admin user (if needed), and enabling remote connections.

# Install MySQL 8
RUN apt-get install -y mysql-server

# Set MySQL root password
RUN sed -i "s/.*debian.sys.ucf.debconf-communicate.*//

You may also like