First, we need to containerize the application using a Dockerfile. After copying/pasting from another service, I found out that my base
openjdk image was deprecated. There were a few suggestions for alternatives, but went with Azul’s image as the new base image.
Next, we need to package the application from the service’s directory using
mvn clean package and build the Docker image with the containerized application with
docker build -t jmreif/goodreads-config . at the command line. Our application is packaged and the Docker image built, so we need to add the service to our
# build: ./config-server
I temporarily commented out the previous service1, service2, and service3 blocks, so that I could focus on adding each piece individually. The
goodreads-config service contains many of the same fields we have used before. I added a commented-out
build option test and make changes locally, rather than pulling from the remote image as the parameter above it does. The
depends_on option specifies that the database container must be started before this service can start. Note: This does not mean that the database container is in a "ready" state, only started.
environment block, we have a variable set up for a Spring profile. This allows us to use different credentials, depending on whether we are running in a local test environment or in Docker. The main difference is the use of
localhost to connect to other services in a local environment (specified by
native profile), versus container names in a Docker environment (profile:
docker). The next option for
volumes sets up the location of the config files (for now, in a local
config directory) and log files.
*Note:* I played around with moving the profile variable into the config file, but found out that we need it as a variable in the container (either from the application in
application.properties or in docker-compose.yml). Sourcing it from outside the container didn’t cooperate.
Let’s test what we changed so far!
Round 1: Put it to the test!
We can run this much of the system with a single command.
*Note:* If you are building local images with the build field in docker-compose.yml, then use the command
docker-compose up -d --build. This will build the Docker containers each time on startup from the directories.
Next, we can test our
goodreads-config service by accessing the configuration file it is hosting. We can do this at the command line with
curl localhost:8888/mongo-client/docker or using a browser with the URL
localhost:8888/mongo-client/docker. This should show something like the screenshot below.
Bring everything back down again with another command.