Institute of Computer Science
  1. Courses
  2. 2023/24 spring
  3. Enterprise System Integration (MTAT.03.229)
ET
Log in

Enterprise System Integration 2023/24 spring

  • Home
  • Lectures
  • Practicals
  • Assignements
  • Project and exam
  • Message Board

1. Creating a Docker Compose File (YAML) for a Spring Boot Project and PostgreSQL

Note: If you are not familiar with YAML, you can refer to YAML Ain't Markup Language

In this document, we will create a docker-compose setup using both Spring Boot application and PostgreSQL database. Note that PostgreSQL is already available as a Docker image in Docker hub, so we just need to pull it from there, you just need to select the version you need from there, I have chosen 14.

We assume you have already completed all the steps in 1. Creating a Dockerfile for a Spring Boot project, i.e., you have already created the image of the Spring Boot project.

In what follows, we will create the docker-compose.yml file which allows us to combine Spring Boot application and PostgreSQL database in this document.

1. Create a file named docker-compose.yml in the project root directory

2. Copy the following content into that file. Note. replace week3-docker2 with the name of your service.

# the version of the Docker compose
version: '3.8'
services:
  # our first service
  week3-docker2:
    build: .  #the location where it can find the Dockerfile to build this service
    # using the already built image of the Spring Boot project
    image: 'docker-week3:latest'  
    # if we want to build an image, we can use "build"
    # restarts the container if it stops. 
    restart: always
    # mapping local machine port along with port inside a docker container
    ports:
      - 8080:8080
    # Express dependency between services. 
    depends_on:
      - postgre_db
    # Allows adding environment variables
    environment:
      - spring.datasource.url=jdbc:postgresql://postgre_db:5432/esi2023
      # note that we have replaced the localhost with the name of the database service
      - spring.datasource.username=postgres
      - spring.datasource.password=postgres
  # our second service
  postgre_db:
    # we do not need a build command here because we will pull an already built image from docker hub
    image: "postgres:14"
    restart: always
    ports:
      - 5432:5432
    environment:
      - POSTGRES_PASSWORD=postgres
      - POSTGRES_ROOT_PASSWORD=postgres
      - POSTGRES_USER=postgres
      - POSTGRES_DB=esi2023
    #volumes:
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres"] #checking the connection status of a PostgreSQL database server
      interval: 10s
      timeout: 5s
      retries: 5

Running Spring Boot Application and PostgreSQL Database Using Docker Compose

Now we have our docker-compose setup for this application.

1. Clean up your project by deleting /target folder. In the explorer bar of VSCode, expand Maven, expand the project, under lifecycle, run clean.

2. Converts our .java source code into a .jar and puts it into the /target folder. In the explorer bar of VSCode, expand Maven, expand the project, under lifecycle, run package.

3. Use the following command to build images in the docker-compose. yml file.

$ docker-compose build

4. Check how many containers are running now by using the following command

$ docker ps

5. Use the following command to run the whole setup using docker-compose, which will capture the docker-compose.yml and start running using the instructions given on that file.

$ docker-compose up

6. Check how many images there are (there should be 2 images) by using the following command

$ docker image ls 

7. Check again how many containers are running now by using the following command

$ docker ps

8. Test your application by sending some HTTP requests, everything should be working as expected

7. You can use the following command to stop docker-compose.

$ docker-compose down
  • Institute of Computer Science
  • Faculty of Science and Technology
  • University of Tartu
In case of technical problems or questions write to:

Contact the course organizers with the organizational and course content questions.
The proprietary copyrights of educational materials belong to the University of Tartu. The use of educational materials is permitted for the purposes and under the conditions provided for in the copyright law for the free use of a work. When using educational materials, the user is obligated to give credit to the author of the educational materials.
The use of educational materials for other purposes is allowed only with the prior written consent of the University of Tartu.
Terms of use for the Courses environment