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.

  • Take note that real-postgres-2 was created by Docker Compose.
  • We are setting up a local Postgres database with docker-compose for our Node.js service such that we can persist data.
  • You can read more from Docker’sdocumentation.
  • Now we are going to try to access the Postgres container with psql from the host operating system.

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.
  • We need to install both the Node.js packages knex and pg.
  • Execute docker ps to check on running containers on Docker.
  • Run the following command, which downloads the PostgreSQL image and configures the database with admin user and bwcemon database.
  • We should now see the created dvdrental database.

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.

  • Psql -U postgres -d dvdrental is the psql command with the postgres user and the dvdrental database specified.
  • We’ll be using a project created in Adding persistence to a website, which uses Exposed to connect to an H2 file database.
  • You can read in a different post why TypeScript is awesome for SQL queries.
  • We send a select query to one of Postgres already existing tables to see if it succeeds.
  • In this article we are going to walk through the Postgres implementation on Docker.
  • The pg package requires the package @types/pg package for the types.

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.

Article was published on: 09/29/22

Author: Viktor Nikolaev

Victor is a professional crypto investor and stockbroker, specializing in such areas as trading on the stock exchange, cryptov currencies, forex, stocks and bonds. In this blog he shares the secrets of trading, current currency indices, crypt currency rates and tells about the best forex brokers. If you have any questions, you can always contact nikolaev@forexaggregator.com

Leave a Reply