#1
If you want a lightweight, Java-based SQL database that’s easy to spin up locally, H2 is a great choice. Pairing it with Docker means you can get started quickly—no installation hassles. Here's a simple guide to installing and using H2 Database with Docker in your development workflow.

1. Pick a Docker Image

You can use a ready-made Docker image for H2—like jesperdj/h2—that bundles the H2 console and TCP server.
Pull it:
docker pull jesperdj/h2
This image starts both the web-based H2 Console (port 8082) and the JDBC/TCP server (port 9092).

2. Run the Container

Start H2 with a simple Docker command:
docker run -d \
  --name h2-dev \
  -p 8082:8082 \
  -p 9092:9092 \
  jesperdj/h2
  • 8082: access the H2 web console (http://localhost:8082)
  • 9092: connect via TCP JDBC (jdbc:h2:tcp://localhost/test)
H2 creates an initial test database automatically.

3. Persist Data with Volumes

Without a volume, H2 creates an in-memory database that disappears when the container stops. To save your data:
docker run -d \
  --name h2-dev \
  -p 8082:8082 \
  -p 9092:9092 \
  -v ~/h2-data:/h2-data \
  jesperdj/h2
Now JDBC URL changes to:
jdbc:h2:tcp://localhost/~/h2-data/test
Your data will live in ~/h2-data, so your work isn't lost on restarts .

4. Access the Web Console

Visit http://localhost:8082 in your browser.
Login details:
  • JDBC URL: jdbc:h2:tcp://localhost/~/h2-data/test
  • User: sa
  • Password: (leave blank)
The console is handy for browsing schemas, running queries, or managing tables, all without writing Java code.

5. Use the H2 Shell (Optional)

Inside the running container, you can also use the H2 shell:
docker exec -it h2-dev bash
h2cli -url jdbc:h2:/h2-data/test -user sa
This launches a CLI where you can run SQL commands directly.

6. Sample Docker Compose Setup

For better organization, here's a docker-compose.yml you can use:
version: '3'
services:
  h2:
    image: jesperdj/h2
    container_name: h2-dev
    ports:
      - "8082:8082"
      - "9092:9092"
    volumes:
      - ./data:/h2-data
Run:
docker-compose up -d
Now your development database is versioned alongside your project, thanks to ./data.

7. Connect from Your App

Your Java application can connect like this:
Connection conn = DriverManager.getConnection(
  "jdbc:h2:tcp://localhost/~/h2-data/test", "sa", ""
);
In frameworks like Spring Boot, add this to application.properties:
spring.datasource.url=jdbc:h2:tcp://localhost/~/h2-data/test
spring.datasource.username=sa
spring.datasource.password=
It’s straightforward and reusable across projects.

8. Next Steps

  • Create additional databases by changing the JDBC URL path.
  • Set passwords by appending ;PASSWORD=MySecret.
  • Enable encryption or clustering using H2's advanced properties.
  • For production-like tests, consider using Docker in CI pipelines or switch to a real RDBMS—H2 is perfect for prototyping.

Why Docker Makes H2 Awesome

  • No install required—just Docker.
  • Web console makes schema and data exploration intuitive.
  • Data persistence via volumes helps with long-term projects.
  • Portable setup—share docker-compose.yml with your team or CI environment.
  • Flexible use—works well as an embedded test DB or standalone server.
With Docker and H2 Database, you're set up for fast development cycles, clean configurations, and easy data exploration—all with minimal fuss. Happy coding!

image quote pre code