Skip to content

Dockerized backend

Jakub Żuchowski edited this page Apr 18, 2024 · 10 revisions

How to run the docker app?

FAQ

1. How to update backend Docker image to the newest version?

Go to terminal or command prompt and run:

docker pull ghcr.io/java-dzgs/ingreedio-api:develop

Then restart Docker image so it can load new version.

2. How to reset MongoDB/PostgresSQL database to its default state?

Delete container.

Frontend devs

1. Generate a Personal Access Token

Generate a Personal Access Token with the read:packages scope from your GitHub account. Go to Settings > Developer settings > Personal access tokens > Generate new token. Set expiration time to never, so you don't have to recreate the token! Make sure to save the token as it will be displayed only once!

2. Login to Github Packages

Open your terminal or command prompt and run the following command:

docker login ghcr.io -u USERNAME -p YOUR_TOKEN

Replace USERNAME with your GitHub username and YOUR_TOKEN with the Personal Access Token you generated in Step 1.

3. Copy this docker-compose.yml

Copy the following docker-compose.yml configuration somewhere into your project directory:

version: '2'

services:
  app:
    image: 'ghcr.io/java-dzgs/ingreedio-api:develop'
    ports:
      - '8080:8080'
    container_name: app
    depends_on:
      - postgres
      - mongo
    environment:
      - SPRING_DATASOURCE_URL=jdbc:postgresql://postgres:5432/ingreedio
      - SPRING_DATASOURCE_USERNAME=compose-postgres
      - SPRING_DATASOURCE_PASSWORD=compose-postgres
      - SPRING_JPA_HIBERNATE_DDL_AUTO=update
      - SPRING_DATA_MONGODB_HOST=mongo
      - SPRING_DATA_MONGODB_USERNAME=compose-mongo
      - SPRING_DATA_MONGODB_PASSWORD=compose-mongo

  postgres:
    image: 'postgres:13.1-alpine'
    container_name: postgres
    ports:
      - '5431:5432'
    environment:
      - POSTGRES_USER=compose-postgres
      - POSTGRES_PASSWORD=compose-postgres
      - POSTGRES_DB=ingreedio

  mongo:
    image: 'mongo'
    container_name: mongo
    restart: always
    ports:
      - '27016:27017'
    environment:
      MONGO_INITDB_ROOT_USERNAME: compose-mongo
      MONGO_INITDB_ROOT_PASSWORD: compose-mongo

  mongo-express:
    image: 'mongo-express'
    container_name: mongo-express
    restart: always
    ports:
      - '8081:8081'
    depends_on:
      - mongo
    environment:
      ME_CONFIG_MONGODB_ADMINUSERNAME: compose-mongo
      ME_CONFIG_MONGODB_ADMINPASSWORD: compose-mongo
      ME_CONFIG_MONGODB_URL: mongodb://compose-mongo:compose-mongo@mongo:27017/
      ME_CONFIG_BASICAUTH: false

4. Run the image!

Navigate to the directory containing your docker-compose.yml file in your terminal or command prompt. Run the following command to start the backend: 😻🐈

docker compose up

Congratulations! Your Docker app should now be running. You can access it at http://localhost:8080.

Backend devs

For ease of use it is recommended to use IntelliJ IDEA Ultimate, which is free for students.

1. Clone the Repository (Optional)

If you haven't already, you may need to clone the repository to your local machine. This step is optional if you already have the project setup. 🐈

2. Run the Start in Docker configuration.

This step involves a specific configuration within IntelliJ that automates the Docker image build and initiates Docker Compose within a Docker container. This process builds the Docker image and starts Docker Compose.

Start in Docker run configuration

After initiating, the process will complete after some time:

Finished docker composition

If you encounter any issues, please contact your DevOps specialist for assistance. 😸

Once complete, you can test the app by navigating to localhost:8080. You should see a Hello, world! greeting :3 Remember to stop all Docker containers!!! after verifying that it works:

Stopping docker

3. Setup the Application configuration

This configuration allows you to build the app outside Docker, then inject it into Docker for debugging purposes.

Here’s how you can set it up:

Configuration

Change the Run on option to Docker compose...:

Run on option

In the new window, select Service: app

Service: app

Confirm the selection and complete the setup. You don't need to change anything else, so just click through the rest 😸

Final configuration

Test the 'Application' configuration by running it. Unlike the previous setup, the app will not start on port 8080. Instead, IntelliJ provides a new, dynamic port each time:

Application configuration

4. Possible error

You may run into error like this: Invalid Java version

This error indicates that your configured Java version is too old. 🙀 To resolve this, ensure that your Gradle configuration uses the appropriate Project SDK. Follow these steps:

Navigate to Settings > Build, Execution, Deployment > Build Tools > Gradle and check following setting: Gradle java

After that, please change the SDK in Project Structure to at least 21: Change version These steps should resolve the Java version error, allowing you to proceed with running the Docker application seamlessly. 😻