Skip to content

Latest commit

 

History

History
90 lines (66 loc) · 3.76 KB

File metadata and controls

90 lines (66 loc) · 3.76 KB

Containerization

In this exercise we will launch our application in a container. For this we have to build the image, deploy it in Rancher Desktop and update it when we change something in our application.

Get to know Docker and Rancher Desktop

  • Run in a CLI docker images. What do you see?

  • Check in CLI if docker containers are running. What do you see?

  • Build our application with maven and package it

Docker Compose

Now, we will containerize our application. We will learn how to run docker compose with postgres, our application and keycloak as well how to navigate inside of a container.

Running with docker

Familiarize yourself with the structure of Dockerfile.jvm and docker-compose.yaml

First create a separate network for the database and app by running: : docker network create --driver bridge quarkus

  • Start database container:

    docker pull postgres
    docker run --name myPostgresDb -p 5432:5432 --net=quarkus -e POSTGRES_USER=quarkus -e POSTGRES_PASSWORD=quarkus -e POSTGRES_DB=quarkus-db -d postgres
  • Build and start the application:

    mvn package
    docker build -f src/main/docker/Dockerfile.jvm -t quarkus/backend .
    docker run -i --rm -p 8080:8080 --net=quarkus -e QUARKUS_DATASOURCE_JDBC_URL=”jdbc:postgresql://myPostgresDb:5432/quarkus-db” quarkus/backend
  • Check created containers running docker ps

Run docker compose

To run the containers you had to execute many commands. It would be easier if we only had to execute one command. We can achieve this by using Docker Compose.

Running Docker compose

Docker Compose works by applying many rules declared within a single docker-compose.yml configuration file.

  • Start application by running docker-compose up

    With this command we started container for our backend app, database and keycloak. This images are defined as services in docker compose. A volume is a shared directory in the host, visible from some or all containers. Similarly, networks define the communication rules between containers, and between a container and the host.

  • To list all build images run docker-compose images

  • Run this command to see that the containers arte up and running docker-compose ps

Access the database

Now we will access Postgres using psql.

  • Connect to Postgres with this command docker exec -it <name of your db container> bash

Now you are ‘inside’ your container.

  • Connect to our quarkus database:

    psql -h localhost -p 5432 -U <name of db user> -W

  • List all available databases inside of your container with \l

  • List all tables that were created with \dt, press "Enter" to show more tables.

Access keycloak

Keycloak is an open source identity and access management solution which mainly aims at applications and services. Users can authenticate with Keycloak rather than individual applications. So, the applications don’t have to deal with login forms, authenticating users and storing users.

  • Access Keycloak bei call the URL localhost:<keycloak port from docker compose>

  • Go to "Administration Console" and enter the user login data from docker compose

  • Navigate to users > view all users. Here you can see your admin-user.

Stop services with Docker compose

  • To safely stop the active services, we can use stop, which will preserve containers, volumes, and networks, along with every modification made to them docker-compose stop

  • To reset the status of our project, we can simply run down, which will destroy everything with the exception of external volumes docker-compose down