Giter Site home page Giter Site logo

gitish / guide-docker Goto Github PK

View Code? Open in Web Editor NEW

This project forked from openliberty/guide-docker

0.0 1.0 0.0 307 KB

A guide on how to use Docker containers for iterative development of microservices: https://openliberty.io/guides/docker.html

Home Page: https://openliberty.io/guides/docker.html

License: Other

Java 36.61% HTML 42.48% Dockerfile 6.11% Shell 14.80%

guide-docker's Introduction

Using Docker containers to develop microservices

Note
This repository contains the guide documentation source. To view the guide in published form, view it on the Open Liberty website.

Learn how to use Docker containers for iterative development.

What you’ll learn

You will learn how to set up, run, and iteratively develop a simple REST application in a container with Open Liberty and Docker.

Open Liberty is an application server designed for the cloud. It’s small, lightweight, and designed with modern cloud-native application development in mind. Open Liberty simplifies the development process for these applications by automating the repetitive actions associated with running applications inside containers, like rebuilding the image and stopping and starting the container.

You’ll also learn how to create and run automated tests for your application and container.

The implementation of the REST application can be found in the start/src directory. To learn more about this application and how to build it, check out the Creating a RESTful web service guide.

What is Docker?

Docker is a tool that you can use to deploy and run applications with containers. You can think of Docker like a virtual machine that runs various applications. However, unlike a typical virtual machine, you can run these applications simultaneously on a single system and independent of one another.

Learn more about Docker on the official Docker website.

What is a container?

A container is a lightweight, stand-alone package that contains a piece of software that is bundled together with the entire environment that it needs to run. Containers are small compared to regular images and can run on any environment where Docker is set up. Moreover, you can run multiple containers on a single machine at the same time in isolation from each other.

Learn more about containers on the official Docker website.

Why use a container to develop?

Consider a scenario where you need to deploy your application on another environment. Your application works on your local machine, but when you try to run it on your cloud production environment, it breaks. You do some debugging and discover that you built your application with Java 8, but this cloud production environment has only Java 11 installed. Although this issue is generally easy to fix, you don’t want your application to be missing dozens of version-specific dependencies. You can develop your application in this cloud environment, but that requires you to rebuild and repackage your application every time you update your code and wish to test it.

To avoid this kind of problem, you can instead choose to develop your application in a container locally, bundled together with the entire environment that it needs to run. By doing this, you know that at any point in your iterative development process, the application can run inside that container. This helps avoid any unpleasant surprises when you go to test or deploy your application down the road. Containers run quickly and do not have a major impact on the speed of your iterative development.

Additional prerequisites

Before you begin, Docker needs to be installed. For installation instructions, refer to the official Docker documentation. You will build and run the application in Docker containers.

Make sure to start your Docker daemon before you proceed.

Creating the Dockerfile

Dockerfile

link:finish/Dockerfile[role=include]

.dockerignore

link:finish/.dockerignore[role=include]

The first step to running your application inside of a Docker container is creating a Dockerfile. A Dockerfile is a collection of instructions for building a Docker image that can then be run as a container. Every Dockerfile begins with a parent or base image on top of which various commands are run. For example, you can start your image from scratch and run commands that download and install Java, or you can start from an image that already contains a Java installation.

Navigate to the start directory to begin.

Create the Dockerfile.
Dockerfile

The FROM instruction initializes a new build stage and indicates the parent image from which your image is built. If you don’t need a parent image, then use FROM scratch, which makes your image a base image.

In this case, you’re using the openliberty/open-liberty:kernel-java8-openj9-ubi image as your parent image, which comes with the latest Open Liberty runtime.

The COPY instructions are structured as COPY [--chown=<user>:<group>] <source> <destination>. They copy local files into the specified destination within your Docker image. In this case, the server configuration file that is located at src/main/liberty/config/server.xml is copied to the /config/ destination directory.

Writing a .dockerignore file

When Docker runs a build, it sends all of the files and directories that are located in the same directory as the Dockerfile to its build context, making them available for use in instructions like ADD and COPY. If there are files or directories you wish to exclude from the build context, you can add them to a .dockerignore file. By adding files that aren’t nessecary for building your image to the .dockerignore file, you can decrease the image’s size and speed up the building process. You may also want to exclude files that contain sensitive information, such as a .git folder or private keys, from the build context.

A .dockerignore file is available to you in the start directory. This file includes the pom.xml file and some system files.

Launching Open Liberty in dev mode

The Open Liberty Maven plug-in includes a devc goal that builds a Docker image, mounts the required directories, binds the required ports, and then runs the application inside of a container. This development mode, known as dev mode, also listens for any changes in the application source code or configuration and rebuilds the image and restarts the container as necessary.

Build and run the container by running the devc goal from the start directory:

mvn liberty:devc

After you see the following message, your application server in dev mode is ready:

************************************************************************
*    Liberty is running in dev mode.

Open another command-line session and run the following command to make sure that your container is running and didn’t crash:

docker ps

You should see something similar to the following output:

CONTAINER ID        IMAGE                   COMMAND                  CREATED             STATUS              PORTS                                                                    NAMES
ee2daf0b33e1        guide-docker-dev-mode   "/opt/ol/helpers/run…"   2 minutes ago       Up 2 minutes        0.0.0.0:7777->7777/tcp, 0.0.0.0:9080->9080/tcp, 0.0.0.0:9443->9443/tcp   liberty-dev

To view a full list of all available containers, you can run the docker ps -a command.

If your container runs without problems, go to the http://localhost:9080/system/properties URL where you can see a JSON file that contains the system properties of the JVM in your container.

Updating the application while the container is running

PropertiesResource.java

link:finish/src/main/java/io/openliberty/guides/rest/PropertiesResource.java[role=include]

With your container running, make the following update to the source code:

Update the PropertiesResource class.
src/main/java/io/openliberty/guides/rest/PropertiesResource.java

Change the endpoint of your application from properties to properties-new by changing the @Path annotation to "properties-new".

After you make the file changes, Open Liberty automatically updates the application. To see these changes reflected in the application, go to the http://localhost:9080/system/properties-new URL.

Testing the container

EndpointIT.java

link:finish/src/test/java/it/io/openliberty/guides/rest/EndpointIT.java[role=include]

You can test this service manually by starting a server and going to the http://localhost:9080/system/properties-new URL. However, automated tests are a much better approach because they trigger a failure if a change introduces a bug. JUnit and the JAX-RS Client API provide a simple environment to test the application. You can write tests for the individual units of code outside of a running application server, or they can be written to call the application server directly. In this example, you will create a test that calls the application server directly.

Create the EndpointIT class.
src/test/java/it/io/openliberty/guides/rest/EndpointIT.java

This test makes a request to the /system/properties-new endpoint and checks to make sure that the response has a valid status code, and that the information in the response is correct.

You will see the following output:

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running it.io.openliberty.guides.rest.EndpointIT
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 2.884 sec - in it.io.openliberty.guides.rest.EndpointIT

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

When you are finished, press CTRL+C in the session that the dev mode was started from to stop and remove the container.

Starting dev mode with run options

Another useful feature of dev mode with a container is the ability to pass additional options to the docker run command. You can do this by adding the <dockerRunOpts> tag to the pom.xml file under the <configuration> tag of the Liberty Maven Plugin. Here is an example of an environment variable being passed in:

<groupId>io.openliberty.tools</groupId>
<artifactId>liberty-maven-plugin</artifactId>
<version>3.3.2</version>
<configuration>
    <dockerRunOpts>-e ENV_VAR=exampleValue</dockerRunOpts>
</configuration>

If the Dockerfile isn’t located in the directory that the devc goal is being run from, you can add the <dockerfile> tag to specify the location. Using this parameter sets the context for building the Docker image to the directory that contains this file.

Additionally, both of these options can be passed from the command line when running the devc goal by adding -D as such:

mvn liberty:devc \
-DdockerRunOpts="-e ENV_VAR=exampleValue" \
-Ddockerfile="./path/to/file"

To learn more about dev mode with a container and its different features, check out the Documentation.

guide-docker's People

Contributors

ahmad-ayyoub avatar andrewdes avatar chyt avatar dependabot[bot] avatar evelinec avatar gkwan-ibm avatar griffinhadfield avatar justineechen avatar kinueng avatar kubik42 avatar lauracowen avatar maihameed avatar nimg98 avatar proubatsis avatar tt-le avatar wasdevb1 avatar yeekangc avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.