Docker compose with dockerfile in subdirectory

Had a nice challenge today playing around with docker, mainly docker-compose. What I wanted to try out was to have the entire application running in docker containers.

The setup was the following: I hade a Java Maven backend communicating with the Postgres database and for the frontend, it was a React application.

The main issue or hardship I had was creating the correct docker-compose file because the folder structure was so that the docker-compose file would reside in the root of the application path and the other parts would be in subfolders called frontend and backend.

So far I had minimal knowledge of docker or docker-compose. The main problem was, understanding how I would need to define the context where the dockerfile is and how the correct volume paths would be.

First: creating dockerfile for react app Second: creating dockerfile for java maven project Thirdly: creating docker-compose file

version: '3'

services: 
  kao-frontend:
    image: kao-frontend:latest
    build:
      context: Frontend
      dockerfile: dockerfile
    volumes: 
      - './Frontend:/app'
      - '/app/node_modules'
    ports: 
      - '3000:3000'
    depends_on:
      - kao-app-container

  kao-app-container:
    image: kao-app:latest
    build:
      context: Backend/kao/.
      dockerfile: dockerfile
    volumes:
      - /data/kao-app
    ports:
      - "8081:8081"
    depends_on:
      - kaopostgres
  
  kaopostgres:
    image: postgres
    ports:
     - "5432:5432"
    environment:
     - POSTGRES_PASSWORD=password
     - POSTGRES_USER=postgres
     - POSTGRES_DB=kaodb