ForexAggregator

Docker Compose

Docker volumes are the recommended way to persist data. When you stop or down the docker container, the database and connection details will still be there. In this topic, we’ll show how to run a server Ktor application under Docker Compose. We’ll be using a project created in Adding persistence to a website, which uses Exposed to connect to an H2 file database. Volumes tag is used to mount a folder from the host machine to the container. Its the path in the host where the database data is stored. The above config will create volume name local_pgdata and mount this volume to container’s path.

There is also docker compose and docker-compose. This will allow pgAdmin to connect to the postgreSQL database . We need to install both the Node.js packages knex and pg. The knex package contains the TypeScript types for Knex. The pg package requires the package @types/pg package for the types. Please share your queries and feedback in the comments section.

Accessing the database

A decent text editor, such as Vim or Notepad++, to create the configuration YAML files. Run the following command to start the monitoring server on 8080 port. You can use Docker Compose to run monitoring application along with the PostgreSQL database on docker. Now, calling the endpoint yields a success 204 if the database is accessible. With the docker-compose configuration specified, the docker-compose tool can be run. The web service is used to run the Ktor application packaged inside the image.

An endpoint GET /health allows us from the outside to verify if the application can access the database. We send a select query to one of Postgres already existing tables to see if it succeeds. If the database can not be accessed, the query would throw an error and lead to a 500 response. In the production configuration, we assign the database object from the environment variable DATABASE_URL. The application needs to know where it can find the database and how to access it.

Databases

Managing a container with a long list of arguments to Docker is tedious and error prone. Instead of the Docker CLI command we could use Docker Compose, which is a tool for managing containers from a YAML manifest file. Let’s start building a “real world” Postgres container. Firstly we need to create the persistent storage. Let’s try a few Postgres commands and import the famous “dvdrental” sample database to our Postgres installation. The init function now accepts ApplicationConfig and uses config.property to load custom settings. Now we are going to try to access the Postgres container with psql from the host operating system.

Environment defines a set of environment variables , for both services we have set the user id and password. The configuration contains an object database that defines the connection configuration expected by Postgres. We are setting up a local Postgres database with docker-compose for our Node.js service such that we can persist data. Wouldn’t it be handy if we could create a new database and define the table schema structure? Ktorjournal is the name of the database created when running services. Execute docker ps to check on running containers on Docker. Take note that the real-postgres container has port forwarding information.

Security & Compliance

Psql -U postgres is the psql command with its switch to specify the Postgres user. You can use this alias or IP address to connect from other containers you create. The connection IP in the pgadmin gui is dependent on the docker network. Run the following command, which downloads the PostgreSQL image and configures the database with admin user and bwcemon database.

This tutorial goes through using Docker-Compose to install and use the PostgreSQL database. You want to use PostgreSQL and avoid manually installing the database by using Docker-Compose.

Production configuration

First line defines the version of the compose file which is 3.8. You can read more from Docker’sdocumentation.

Run the following command to build the application monitoring image. Needs to review the security of your connection before proceeding. Internet access is required to pull or download the Postgres container image from the Docker Hub. Besides running as an executable binary in an operating system, Postgres is able to run as a containerized application on Docker! In this article we are going to walk through the Postgres implementation on Docker.