Launch microservice in Docker container

Launch microservice in Docker container

Docker, microservices, and continuous delivery are increasingly popular topics among modern development teams. Today I’m going to create a simple microservice and show you how to run it in a Docker container using the Maven plugin or Jenkins pipeline.
Let’s start from the application code which is available on https://github.com/piomin/sample-docker-microservice.git. It has the endpoint for searching all the persons and a single person by id. Here’s the controller code:

package pl.piomin.microservices.person;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class Api {

  protected Logger logger = Logger.getLogger(Api.class.getName());

  private List<Person> persons;

  public Api() {
    persons = new ArrayList<>();
    persons.add(new Person(1, "Jan", "Kowalski", 22));
    persons.add(new Person(1, "Adam", "Malinowski", 33));
    persons.add(new Person(1, "Tomasz", "Janowski", 25));
    persons.add(new Person(1, "Alina", "Iksińska", 54));
  }

  @RequestMapping("/person")
  public List<Person> findAll() {
    logger.info("Api.findAll()");
    return persons;
  }

  @RequestMapping("/person/{id}")
  public Person findById(@PathVariable("id") Integer id) {
    logger.info(String.format("Api.findById(%d)", id));
    return persons.stream().filter(p -> (p.getId().intValue() == id)).findAny().get();
  }

}

We need to have Docker installed on our machine and Docker registry container running on port 5000. If you are interested in commercial support, you can use Docker Trusted Registry. It provides an image registry and some other features like LDAP/Active Directory integration, or security certificates.

$ docker run -d --name registry -p 5000:5000 registry:latest

We use OpenJDK as a base image. The application JAR file will be launched in java command and exposed on port 8080.

FROM openjdk
MAINTAINER Piotr Minkowski <piotr.minkowski@gmail.com>
ADD sample-docker-microservice-1.0-SNAPSHOT.jar person-service.jar
ENTRYPOINT ["java", "-jar", "/person-service.jar"]
EXPOSE 8080

We use docker-maven-plugin to configure the building process. There is no need for using Dockerfile with that plugin. It has equivalent tags in the configuration.

<plugin>
  <groupId>com.spotify</groupId>
  <artifactId>docker-maven-plugin</artifactId>
  <version>1.2.2</version>
  <configuration>
    <imageName>${docker.image.prefix}/${project.artifactId}</imageName>
    <imageTags>${project.version}</imageTags>
    <dockerDirectory>src/main/docker</dockerDirectory>
    <resources>
      <resource>
        <targetPath>/</targetPath>
        <directory>${project.build.directory}</directory>
        <include>${project.build.finalName}.jar</include>
      </resource>
    </resources>
  </configuration>
</plugin>

Finally, we can build our code using the Maven command mvn clean package docker:build. After running that command the image is tagged and pushed to the local repository.

Application images now are registered in the local Docker Registry. Optionally, we could push it docker.io or to an enterprise Docker Trusted Registry. We can check it out using API available at http://localhost:5000/v2/_catalog. Here’s Docker command for running with the newly created image stored in the local registry. Service is available at http://localohst:8080/person/.

$ docker run -d --name sample1 -p 8080:8080 microservice/sample-docker-microservice:1.0-SNAPSHOT

Leave a Reply