Spring Cloud Microservices Beginner Tutorial (1): Microservices Introduction Spring Cloud Microservices Beginner Tutorial (2): Service Registration and Discovery — Eureka Spring Cloud Microservices Beginner Tutorial (3): Service Registration Spring Cloud Microservices Beginner Tutorial (4): Inter-service Invocation — FeignClient Spring Cloud Microservices Beginner Tutorial (5): Centralized Config — ConfigService Spring Cloud Microservices Beginner Tutorial (6): Spring Cloud BUS Message Bus for Dynamic Config Refresh Spring Cloud Microservices Beginner Tutorial (7): Spring Cloud Stream Message-driven Microservices Spring Cloud Microservices Beginner Tutorial (8): Spring Cloud Zuul API Gateway Dynamic Routing, Cookie Passing, and CORS Spring Cloud Microservices Beginner Tutorial (9): Zuul Gateway Integrating Swagger2 for Auto-generated RESTful API Docs Spring Cloud Microservices Beginner Tutorial (10): Spring Cloud Hystrix Circuit Breaking and Service Degradation Spring Cloud Microservices Beginner Tutorial (11): Spring Cloud Sleuth + Zipkin Service Tracing and Link Monitoring Spring Cloud Microservices Beginner Tutorial (12): Spring Cloud Docker Containerized Deployment
Code will be shared at https://github.com/NeilRen/SpringCloudDemo, with different branches for different chapters; the Master branch contains the final combined content. This chapter’s code is at: https://github.com/NeilRen/SpringCloudDemo/tree/feature/docker
Original This article is original, author: Ren Fei. Please cite the author and source when reposting.
After the previous section on service tracking and link monitoring, the basic microservices framework is nearly complete. Deployment in a microservices setup inevitably involves Docker, so how do we build a Docker image? In this section we’ll cover packaging the code into an image file and uploading it to an image repository.
Introduction to Dockerfile
Since the topic is microservices, we won’t go deep into Docker. If you know nothing about Docker, you should first get a basic understanding of it; we’ll only cover what’s enough for you. The most common way to create an image in Docker is to use a Dockerfile. A Dockerfile is a description file for a Docker image. It contains instructions, one per line; each instruction builds a layer, so the content of each instruction describes how that layer should be built.
We create a Dockerfile under each module; refer to the following for the content:
# Based on OpenJDK 8 image
FROM openjdk:8
# Maintainer and contact
MAINTAINER RenFei i@renfei.net
# Add the Maven-packaged jar to the image
ADD target/*.jar app.jar
# Expose port 8761
EXPOSE 8761
# Entry point; the image starts execution here
ENTRYPOINT ["java","-jar","app.jar"]
Then run docker build -t <tag-name> to build the image, then run docker push <repo-address> to push the image to a remote image repository. Typing the commands manually every time is tedious, so we can write a shell script to perform the whole sequence; let’s create a build.sh file.
The build.sh Script
To simplify our operations, we create a script. Linux and Mac users use a shell script named build.sh; Windows users need a batch file named build.bat, with slightly different commands. Here I’ll demonstrate on a Mac environment:
#!/usr/bin/env bash
mvn clean package -Dmaven.test.skip=true -U
docker build -t XXXXXX
docker push XXXXXX
Running this script packages with Maven, builds the image, and pushes it — all in one go.
Running the Image
After the image is built locally, you can run it locally. The command is docker run -p <host-port>:<container-port> -d <image-name>, for example: docker run -p 8761:8761 -d renfei/eureka starts the registry module.
If it says the file is not executable, you also need the command chmod +x build.sh to give the file execute permission.
About Image Repositories
There are many image repository providers: you can build your own private registry, or use the official DockerHub; NetEase Cloud and Alibaba Cloud also provide registry services. For personal practice, NetEase Cloud is more commonly used domestically.
