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.
-
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
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.
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
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.
Docker Compose works by applying many rules declared within a single docker-compose.yml configuration file.
-
Start application by running
docker-compose upWith 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
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.
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.
-
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