back to all blogsSee all blog posts

Fast iterative development with Open Liberty dev mode and containers (Tech Preview)

image of author
Eric Lau on Aug 25, 2020
Post available in languages:

When developing an application that will eventually be deployed to production, you can avoid potential issues by ensuring that your development and production environments are as similar as possible. This aligns with Twelve Factor App methodology, particularly factor 10, which calls for dev/prod parity. For cloud-native applications, part of this issue is addressed by using containers where your environment can be codified to provide consistency between development and production.

Container support in dev mode

In this post, we introduce a technology preview for container support in Open Liberty development mode, known as dev mode. With container support, you can develop applications on your local environment while your Open Liberty server runs in a container. The development container image is kept as similar as possible to the production container image, while still allowing for iterative development. Your code changes are automatically hot deployed to the container and picked up by the running server. Additionally, dev mode allows you run tests either automatically or on demand, and you can attach a debugger at any time to debug your application.

Because container support in dev mode is in technology preview, its features and parameters are subject to change in future milestones or releases of the Liberty Maven or Gradle plug-ins. We invite you to try it out and let us know your thoughts so that we can make it even more useful for your development scenarios.

Iterative development

With container support for Open Liberty dev mode, you can use the same Dockerfile for both development and production. You use the same base image and customizations, and you specify the exact configuration files that you need for your application in your Dockerfile. This prevents you from running into any surprises when you deploy your application to production. Dev mode changes how the image is built and run to enable iterative development, so the images are identical except for how application and configuration files are mounted into the containers. With dev mode, simply save a source file in any text editor or IDE, and it is recompiled and picked up without any need to rebuild the image or restart the server.

Changing a Java file while dev mode is running with container support

Starting dev mode with container support

For Maven projects, specify the Liberty Maven plug-in with version 3.3-M3 in your project pom.xml file:

    <plugin>
        <groupId>io.openliberty.tools</groupId>
        <artifactId>liberty-maven-plugin</artifactId>
        <version>3.3-M3</version>
    </plugin>

For Gradle projects, specify the Liberty Gradle plug-in with version 3.1-M2 in your project build.gradle file:

apply plugin: 'liberty'

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'io.openliberty.tools:liberty-gradle-plugin:3.1-M2'
    }
}

Create a Dockerfile in your project root directory that includes COPY commands for your application and configuration files, as shown in the Open Liberty Docker image documentation.

Start dev mode with container support by running one of following commands in your project:

  • Maven: mvn liberty:devc

  • Gradle: gradle libertyDevc

These commands compile your application, build the development image, and run the server in the container. You can continue to make changes to your source code or configuration files while dev mode is running. Press Enter in the terminal to run tests, or attach a debugger to port 7777 at any time to debug your application. Quit dev mode by pressing CTRL+C in the terminal, or type q and press Enter.

Additional resources

For a more comprehensive demo, see the devc branch of the demo-devmode project.

For more details on dev mode with container support, see the documentation for the devc goal of the Liberty Maven plug-in or the libertyDevc task of the Liberty Gradle plug-in.

Next steps

Since this is a tech preview, functionality can change and we are open to any comments or suggestions. We aim to iteratively provide additional previews for feedback, so give it a try and let us know what works or doesn’t work for you. You can post in the Open Liberty developer experience chat or open issues in the Liberty Maven plug-in or Liberty Gradle plug-in GitHub repositories.