Docker Compose¶
Introduction¶
To be able to deploy on a Cloud environment, a virtualization engine has to be used. As my project uses an implementation of microservices, these can be served through the cloud by enabling an engine to do so. In my initial proof of concept, I decided to use Docker Compose to see if my docker containers could run in my localhost.
Theory¶
Docker Compose is a tool that was developed to help define and share multi-container applications. With Compose, we can create a YAML file to define the services and with a single command, can spin everything up or tear it all down.
The big advantage of using Compose is you can define your application stack in a file, keep it at the root of your project repo (it’s now version controlled), and easily enable someone else to contribute to your project. Someone would only need to clone your repo and start the compose app.
Implementation¶
After installing docker compose on my windows computer (where I do most of my developing) I first begun by making an individual Dockerfile for every microservice. I did this using a maven template, exposing the rest API on port 9521.
#
# Build stage
#
FROM maven:3.6.0-jdk-11-slim AS build
COPY src /home/app/src
COPY pom.xml /home/app
RUN mvn -f /home/app/pom.xml clean package
#
# Package stage
#
FROM openjdk:11-jre-slim
COPY --from=build /home/app/target/ram-1.jar /usr/local/lib/ram-1.jar
EXPOSE 9521
ENTRYPOINT ["java","-jar","/usr/local/lib/ram-1.jar"]
Once this container ran successfully using docker build -f .\Dockerfile . (see output below), I continued and created a Docker Compose file that could spin the environment up including an MySQL file.
So the next step is to compose the environment, as my microservice uses a mysql implementation that I put with the cluster. This was merely to try out to see if it would work, and it did. Here is the configuration for the Docker Compose file.
version: "3.8"
services:
mysqldb:
image: mysql:5.7
restart: unless-stopped
env_file: .env
environment:
- MYSQL_ROOT_PASSWORD=$MYSQLDB_ROOT_PASSWORD
- MYSQL_DATABASE=$MYSQLDB_DATABASE
ports:
- 3306:3306
volumes:
- db:/var/lib/mysql
gameService:
depends_on:
- mysqldb
build: ./gameService/
image: ramses-gameservice:latest
container_name: ram
restart: on-failure
env_file: .env
ports:
- 9521:9521
environment:
SPRING_APPLICATION_JSON: '{
"spring.datasource.url" : "jdbc:mysql://mysqldb:$MYSQLDB_DOCKER_PORT/$MYSQLDB_DATABASE?useSSL=false",
"spring.datasource.username" : "$MYSQLDB_USER",
"spring.datasource.password" : "$MYSQLDB_ROOT_PASSWORD",
"spring.jpa.properties.hibernate.dialect" : "org.hibernate.dialect.MySQL5InnoDBDialect",
"spring.jpa.hibernate.ddl-auto" : "update"
}'
volumes:
- .m2:/root/.m2
stdin_open: true
tty: true
volumes:
db:
This script could then be ran using docker-compose up -d and would yield the following results:
And here it is running in my Docker cluster: