Deploying microservices in a Docker container

We will now deploy the CourseManagement microservice that we created earlier in this chapter (the one using WildFly Swarm) in a Docker container. You can either copy the project and paste it in Eclipse Project Explorer with a different name, or use the same project. The example code has a project called coursemanagement-docker for this section.

We need to make one change in persistence.xml. Recall that in our earlier example, the JDBC URL in this file referred to 127.0.0.1 or localhost. This worked then because both the application and the database were running in the same environment. But now our database and application are going to run in separate Docker containers, with isolated runtime environments. Therefore, we can no longer access the database using the localhost URL in the microservice. So, how do we access a database running in a separate container? The answer is using the container name, if both containers are running in the same Docker network mode. We configured the container for the DB to run in the coursemanagment network, and later in this section we are going to do the same for the microservice container. So, we will need to change the JDBC URL in persistence.xml to refer to name of the container running our database server, which is course-management-mysql.

Open persistence.xml and replace IP 127.0.0.1 in the JDBC URL with course-management-mysql:

<property name="javax.persistence.jdbc.url" value="jdbc:mysql://course-management-mysql/course_management?autoReconnect=true&amp;useSSL=false"/>

Next, create a file named Dockerfile in root of the project with the following content:

FROM openjdk:8
ENV swarm.http.port 8080
RUN mkdir microservices
COPY ./target/coursemanagement-swarm.jar ./microservices
EXPOSE 8080
ENTRYPOINT java -jar -Djava.net.preferIPv4Stack=true ./microservices/coursemanagement-swarm.jar

We will be using this Dockerfile to create the image for our microservice container. Let’s understand each of the instructions in this file:

  • FROM openjdk:8: The base image for this container is OpenJDK, Version 8.
  • ENV swarm.http.port 8080: We are setting the swarm.http.port environment variable in the container. This is really not necessary for this example, because the WildFly Swarm server runs on port 8080 by default. Change the port number if you want to run the server on a different port.
  • RUN mkdir microservices: We are creating a folder named microservices in the container.
  • COPY ./target/coursemanagement-swarm.jar ./microservices: We are copying coursemanagement-swarm.jar from the target folder in our project to the microservices folder in the container.
  • EXPOSE 8080:  We ask Docker Engine to expose port 8080 from the container. Our application server listens for requests on port 8080 in the container.
  • ENTRYPOINT java -jar -Djava.net.preferIPv4Stack=true ./microservices/coursemanagement-swarm.jar: Finally, we specify the main application to execute in the container, which is running the standalone microservice application.

We need to build the application to create a single JAR file that we will run in the Docker container. If you try to build the application by running the Maven goal wildfly-swarm:run (we did that to run the application earlier), it is going to fail because it will also try to run the application. This is not going to work because we modified the JDBC URL in persistence.xml with the name of the DB container. So, run the Maven goal to only package the application, without running tests. Right-click on the project in Project Explorer and select Run As | Maven Build:

Figure 12.14: Eclipse run configuration to package the Docker-microservice project

Enter package in the Goals field. Select the Skip Tests option and click Run to create the application JAR file in the target folder.

Let’s now create the Docker image from the Dockerfile we created. Right-click on the file in Project Explorer and select the Run As | Docker Image Build menu option.  

Figure 12.15: Building a Docker image from a Dockerfile

This will create a Docker image named coursemanagement-microservice and tag it as the 1.0 version. Switch to the Docker Tooling perspective in Eclipse and you should see this image listed.

We are going to create an instance of this image, that is, create a container from this image that will actually run our microservice. Right-click on the image and select Run...:

Figure 12.16: Creating a container from an image

This opens a wizard to configure the container:

Figure 12.17: Configuring a Docker container

Specify a name for the container in the first page of the wizard. Leave Endpoint and Command empty; the image is already created with the ENTRYPOINT that we specified in the Dockerfile. You can override that in this page, but we are not going to do that.
Make sure the Publish all exposed ports to random ports on the host interfaces option is unchecked. We want to publish port 8080 from the container as the same port number to the host. Click Next. Leave the default options on the second page and click Next again.

Figure 12.18: Setting network mode for a Docker container

The last page (see Figure 12.18) allows you to specify a network for the container. Here, we are going to specify the network we created earlier, coursemanagement. Recall that we also created a MySQL container with the same network, so that microservice container can access the MySQL container with the container name.

Once the application starts in the microservice container, browse to http://localhost:8080/course_management/courses and you should see list of courses in the database.

The process to deploy the microservice we created using Spring Boot earlier is also similar to the one we saw in this section. One main difference is that in the Spring Boot project, you need to update the JDBC URL in application.properties, instead of the persistence.xml that we modified in this section. For your reference, the sample code has a project named coursemanagementspring-docker.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.50.222