Docker Compose

This article is connected to previous articles in this Docker series. If you have come to this article without reading them, and feel a little lost, I would recommend going through following articles first.

A little bit of context always helps! 🙂

In previous articles, we discussed about how to build our own image for a Spring Boot Java web application and then how to deploy it into an Apache Tomcat application server. Our application utilized a MySQL database so we had to make sure that we connect our web service to the database correctly as well. So by now we have our application set up quite nicely.

But the thing is, every time we want to whip up an environment for whatever reason, we need to run those docker run commands and make sure to setup a network, provide all the environment variables, ports etc. And that’s something we don’t want to do ’cause we’re just lazy like that!

So the solution we have for this is Docker Compose. Docker compose is now part of Docker CLI so we can use it without having to install anything in addition.

Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services. Then, with a single command, you create and start all the services from your configuration.

Docker Docs – Overview of Docker

Docker Compose YAML File

So with a docker-compose file, we basically specify everything relevant for our application containers. Since our application consists of a MySQL database and a Tomcat web server, it would have two services.

The docker-compose.yaml file for our application would look like below.

 version: '3'
 services:       
     db-myappv5:
         container_name: db-myappv5
         image: mysql
         ports:
             - 33306:3306
         environment:
             - MYSQL_ROOT_PASSWORD=charith
             - MYSQL_DATABASE=photo_app
             - MYSQL_USER=charith
             - MYSQL_PASSWORD=charith
     ws-myappv5:
         container_name: ws-myappv5
         image: myapp:v4
         ports: 
             - 38084:8080
         environment:
             - DB_HOST=db-myappv5
             - DB_USERNAME=charith
             - DB_PASSWORD=charith
         depends_on:
             - db-myappv5

So I have created this file inside my project directory, which is named mobile-app-ws. The file needs to be named as docker-compose.yaml. When I run docker compose up command, this is what happens.

  1. A network called mobile-app-ws_default is created.
  2. A container is created using db-myappv5‘s configuration. It joins the network mobile-app-ws_default under the name db-myappv5.
  3. A container is created using ws-myappv5’s configuration. It joins the network mobile-app-ws_default under the name ws-myappv5.
  4. Container name, image to use, port mapping from docker host to container, environment variables for each of the service are specified accordingly.
  5. depends_on option in ws-myappv5 service specifies the startup and shutdown order of containers. Since the web service needs the database to be running to function properly, depends_on option has been added to it.

Now all I need to do is run docker compose up command from the directory where I have placed this docker-compose.yaml file. And the output would look similar to below.

C:\Users\charith\Desktop\mobile-app-ws>docker compose up
 [+] Running 2/2
 Container db-myappv5  Created                                                                                   0.1s
 Container ws-myappv5  Created                                                                                   0.1s
 Attaching to db-myappv5, ws-myappv5
 db-myappv5  | 2021-06-27 19:40:49+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.23-1debian10 started.
 db-myappv5  | 2021-06-27 19:40:49+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
 db-myappv5  | 2021-06-27 19:40:49+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.23-1debian10 started.
 db-myappv5  | 2021-06-27 19:40:49+00:00 [Note] [Entrypoint]: Initializing database files
 db-myappv5  | 2021-06-27T19:40:49.465349Z 0 [System] [MY-013169] [Server] /usr/sbin/mysqld (mysqld 8.0.23) initializing of server in progress as process 43
 db-myappv5  | 2021-06-27T19:40:49.471019Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
 db-myappv5  | 2021-06-27T19:40:50.636919Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
 ws-myappv5  | NOTE: Picked up JDK_JAVA_OPTIONS:  --add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.io=ALL-UNNAMED --add-opens=java.base/java.util=ALL-UNNAMED --add-opens=java.base/java.util.concurrent=ALL-UNNAMED --add-opens=java.rmi/sun.rmi.transport=ALL-UNNAMED
 ws-myappv5  | 27-Jun-2021 19:40:52.901 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version name:   Apache Tomcat/9.0.46
 ws-myappv5  | 27-Jun-2021 19:40:52.915 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server built:          May 8 2021 17:35:52 UTC 

Note that I have only included the first few lines from the output here.

docker-compose came as a separate utility until very recently so you would see the command docker-compose up in many old online articles. But now it is part of docker CLI so we can simply use docker compose up.

Once the containers are started, you can test whether the application is working. Since this is a new database, I am going to create a new user. The format of this web service call is explained in detail here. Make sure the port of the URL is correct though! We have used 38084 as the port here.

Postman create user after docker compose

And it’s a success!

Share this article if it was helpful!

2 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *