This Banner is For Sale !!
Get your ad here for a week in 20$ only and get upto 15k traffic Daily!!!

Add postgres to docker compose and dokku infrastructure for telegram bot in NestJS




Hyperlinks

https://github.com/EndyKaufman/kaufman-bot – supply code of bot

https://telegram.me/DevelopKaufmanBot – present bot in telegram

https://github.com/dokku/dokku-postgres – dokku-postgres plugin for docker compose

https://github.com/EndyKaufman/postgres-default – 13 postgres with enabled extensions



Add postgres to dev and prod infrastructure

Organising a database for prod and dev infra is similar, I describe just for dev infra



Replace docker compose file

docker/dev/docker-compose.yml

model: "3"
networks:
    kaufman-bot-network:
        ipam:
            config:
                - subnet: "172.6.0.0/16"

volumes:
    kaufman-bot-postgres-volume:
        exterior: true

companies:
    kaufman-bot-postgres:
        picture: 'endykaufman/postgres-default'
        container_name: 'kaufman-bot-postgres'
        setting:
            - POSTGRES_USER=${ROOT_POSTGRES_USER}
            - POSTGRES_PASSWORD=${ROOT_POSTGRES_PASSWORD}
            - POSTGRES_DB=postgres
        env_file:
            - ../../.env.native
        ports:
            - '5432:5432'
        volumes:
            - kaufman-bot-postgres-volume:/var/lib/postgresql/knowledge
        networks:
            - kaufman-bot-network

    kaufman-bot-server:
        picture: node:16-alpine
        consumer: ${CURRENT_UID}
        container_name: "kaufman-bot-server"
        setting:
            - TELEGRAM_BOT_TOKEN=${TELEGRAM_BOT_TOKEN}
            - PORT=3000
        ports:
            - "3000:3000"
            - "9229:9229"
        working_dir: "/app"
        volumes:
            - ./../../:/app
        networks:
            - kaufman-bot-network
        command: "npm run serve"
        tty: true
        depends_on:
            - kaufman-bot-postgres

Enter fullscreen mode

Exit fullscreen mode



Replace you env file

.env.native

TELEGRAM_BOT_TOKEN=1111111:AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
ROOT_POSTGRES_USER=postgres
ROOT_POSTGRES_PASSWORD=postgres
Enter fullscreen mode

Exit fullscreen mode



Replace up script

docker/dev/docker-compose-up.sh

#!/bin/bash
#export UID=$(id -u)
#export GID=$(id -g)
export CURRENT_UID=$(id -u):$(id -g)
docker quantity create --name=kaufman-bot-postgres-volume --label=kaufman-bot-postgres-volume
docker-compose -f ./docker/dev/docker-compose.yml --compatibility up -d
Enter fullscreen mode

Exit fullscreen mode



Add second down script with drop quantity

docker/dev/docker-compose-clean-down.sh

#!/bin/bash
#export UID=$(id -u)
#export GID=$(id -g)
export CURRENT_UID=$(id -u):$(id -g) 
docker-compose -f ./docker/dev/docker-compose.yml down
docker quantity rm kaufman-bot-postgres-volume --force
Enter fullscreen mode

Exit fullscreen mode



For prod infra, we do the identical factor solely in different folders

For prod infra, we do the same thing only in other folders



Add new scripts to bundle.json

bundle.json

...
    "docker:dev:clean-down": "export $(xargs < ./.env.native) > /dev/null 2>&1 && ./docker/dev/docker-compose-clean-down.sh",
    "docker:dev:clean-restart": "npm run docker:dev:clean-down && npm run docker:dev:up",
    "docker:prod:clean-down": "export $(xargs < ./.env.native) > /dev/null 2>&1 && ./docker/prod/docker-compose-clean-down.sh",
    "docker:prod:clean-restart": "npm run docker:prod:clean-down && npm run docker:prod:up"
...
Enter fullscreen mode

Exit fullscreen mode



Restart dev infra

npm run docker:dev:clean-restart

endy@endy-virtual-machine:~/Initiatives/present/kaufman-bot$ npm run docker:dev:clean-restart

> kaufman-bot@0.0.0 docker:dev:clean-restart
> npm run docker:dev:clean-down && npm run docker:dev:up


> kaufman-bot@0.0.0 docker:dev:clean-down
> export $(xargs < ./.env.native) > /dev/null 2>&1 && ./docker/dev/docker-compose-clean-down.sh

Stopping kaufman-bot-server   ... carried out
Stopping kaufman-bot-postgres ... carried out
Eradicating kaufman-bot-server   ... carried out
Eradicating kaufman-bot-postgres ... carried out
Eradicating community dev_kaufman-bot-network
kaufman-bot-postgres-volume

> kaufman-bot@0.0.0 docker:dev:up
> export $(xargs < ./.env.native) > /dev/null 2>&1 && ./docker/dev/docker-compose-up.sh

kaufman-bot-postgres-volume
Creating community "dev_kaufman-bot-network" with the default driver
Creating kaufman-bot-postgres ... carried out
Creating kaufman-bot-server   ... carried out
Enter fullscreen mode

Exit fullscreen mode

Restart dev infra



Test database

Connect with container with database

docker exec -it $(docker ps -aqf “title=kaufman-bot-postgres”) sh
Connect to container with database

Swap consumer

su postgres

Run psql mode

psql

Choose database title

SELECT current_database();

endy@endy-virtual-machine:~/Initiatives/present/kaufman-bot$ docker exec -it $(docker ps -aqf "title=kaufman-bot-postgres") sh
# su postgres
postgres@48966265f189:/$ psql
psql (13.3 (Debian 13.3-1.pgdg100+1))
Sort "assist" for assist.

postgres=# SELECT current_database();
 current_database 
------------------
 postgres
(1 row)

postgres=# 
Enter fullscreen mode

Exit fullscreen mode

Check database



Add postgres to dukku infrastructure (on server)



Set up dokku-postgres

Now we join by way of ssh to our server on Ubuntu and arrange work with postgres

sudo dokku plugin:set up https://github.com/dokku/dokku-postgres.git postgres

root@vps17825:~# sudo dokku plugin:set up https://github.com/dokku/dokku-postgres.git postgres
-----> Cloning plugin repo https://github.com/dokku/dokku-postgres.git to /var/lib/dokku/plugins/accessible/postgres
Cloning into 'postgres'...
distant: Enumerating objects: 2356, carried out.
distant: Counting objects: 100% (417/417), carried out.
distant: Compressing objects: 100% (276/276), carried out.
distant: Whole 2356 (delta 244), reused 267 (delta 118), pack-reused 1939
Receiving objects: 100% (2356/2356), 483.24 KiB | 3.48 MiB/s, carried out.
Resolving deltas: 100% (1580/1580), carried out.
-----> Plugin postgres enabled
Including consumer dokku to group adm
Beginning nginx (by way of systemctl): nginx.service.
14.1: Pulling from library/postgres
5eb5b503b376: Pull full 
daa0467a6c48: Pull full 
7cf625de49ef: Pull full 
bb8afcc973b2: Pull full 
c74bf40d29ee: Pull full 
2ceaf201bb22: Pull full 
1255f255c0eb: Pull full 
d27501cd0cca: Pull full 
ff5b6d09a5d0: Pull full 
f635aec27645: Pull full 
a165c6729250: Pull full 
b0aa4f86b611: Pull full 
9efc4664d9d2: Pull full 
Digest: sha256:3162a6ead070474b27289f09eac4c865e75f93847a2d7098f718ee5a721637c4
Standing: Downloaded newer picture for postgres:14.1
docker.io/library/postgres:14.1
1.31.1-uclibc: Pulling from library/busybox
76df9210b28c: Pull full 
Digest: sha256:cd421f41ebaab52ae1ac91a8391ddbd094595264c6e689954b79b3d24ea52f88
Standing: Downloaded newer picture for busybox:1.31.1-uclibc
docker.io/library/busybox:1.31.1-uclibc
0.3.3: Pulling from dokku/ambassador
aad63a933944: Pull full 
2888dfab2eb5: Pull full 
51ccf60e0642: Pull full 
Digest: sha256:87c0214e190e7f6975953027157a8933701596b4b864ff66dd3cc3f6ead5c38d
Standing: Downloaded newer picture for dokku/ambassador:0.3.3
docker.io/dokku/ambassador:0.3.3
0.10.3: Pulling from dokku/s3backup
aad63a933944: Already exists 
6654c5b7b2dc: Pull full 
26abcd9faf98: Pull full 
d1a36cd3ba61: Pull full 
9517d44e685b: Pull full 
32e8b2c4797f: Pull full 
Digest: sha256:3651f8ef12000206df55fec8ad4860d6f26b2b5af1308c0e2358253641626024
Standing: Downloaded newer picture for dokku/s3backup:0.10.3
docker.io/dokku/s3backup:0.10.3
0.4.3: Pulling from dokku/wait
aad63a933944: Already exists 
3409ea528c35: Pull full 
88e35d065209: Pull full 
Digest: sha256:5eb9da766abdd5e8cedbde9870acd4b54c1c7e63e72c99e338b009d06f808f04
Standing: Downloaded newer picture for dokku/wait:0.4.3
docker.io/dokku/wait:0.4.3
-----> Priming bash-completion cache
root@vps17825:~# 
Enter fullscreen mode

Exit fullscreen mode



Create database service

I’m utilizing my customized construct with UUID extension enabled https://hub.docker.com/r/endykaufman/postgres-default

dokku postgres:create global-postgres –image “endykaufman/postgres-default” –image-version newest –root-password=ROOT_PASSWORD –password=ADMIN_PASSWORD

root@vps17825:~# dokku postgres:create global-postgres --image "endykaufman/postgres-default" --root-password=ROOT_PASSWORD --password=ADMIN_PASSWORD
/var/lib/dokku/plugins/enabled/postgres/subcommands/create: unlawful possibility -- -
/var/lib/dokku/plugins/enabled/postgres/subcommands/create: unlawful possibility -- -
Error response from daemon: manifest for endykaufman/postgres-default:14.1 not discovered: manifest unknown: manifest unknown
 !     Postgres picture endykaufman/postgres-default:14.1 pull failed
root@vps17825:~# dokku postgres:create global-postgres --image "endykaufman/postgres-default" --image-version newest --root-password=ROOT_PASSWORD --password=ADMIN_PASSWORD
/var/lib/dokku/plugins/enabled/postgres/subcommands/create: unlawful possibility -- -
/var/lib/dokku/plugins/enabled/postgres/subcommands/create: unlawful possibility -- -
newest: Pulling from endykaufman/postgres-default
b4d181a07f80: Pull full 
46ca1d02c28c: Pull full 
a756866b5565: Pull full 
36c49e539e90: Pull full 
664019fbcaff: Pull full 
727aeee9c480: Pull full 
796589e6b223: Pull full 
6664992e747d: Pull full 
0f933aa7ccec: Pull full 
99b5e5d88b32: Pull full 
a901b82e6004: Pull full 
625fd35fd0f3: Pull full 
9e37bf358a5d: Pull full 
8c5f37d7fa57: Pull full 
Digest: sha256:a3d342741451f717b79b2404e88363ea902a769d45a0bd7dbbbeeb73bb443f93
Standing: Downloaded newer picture for endykaufman/postgres-default:newest
docker.io/endykaufman/postgres-default:newest
 !     Specified password might not be as safe because the auto-generated password
       Ready for container to be prepared
       Creating container database
       Securing connection to database
=====> Postgres container created: global-postgres
=====> global-postgres postgres service data
       Config dir:          /var/lib/dokku/companies/postgres/global-postgres/knowledge
       Config choices:                               
       Information dir:            /var/lib/dokku/companies/postgres/global-postgres/knowledge
       Dsn:                 postgres://postgres:assword=ADMIN_PASSWORD@dokku-postgres-global-postgres:5432/global_postgres
       Uncovered ports:       -                        
       Id:                  50dbeaef39b80ca97823ad35ac771b241c2214eb3bd5cd81564f9dee546ae783
       Inner ip:         172.17.0.5               
       Hyperlinks:               -                        
       Service root:        /var/lib/dokku/companies/postgres/global-postgres
       Standing:              operating                  
       Model:             endykaufman/postgres-default:newest
Enter fullscreen mode

Exit fullscreen mode



Record all postgres companies

dokku postgres:record

List all postgres services



Test database

Connect with database

dokku postgres:join global-postgres
Connect to database

Choose database title

SELECT current_database();

root@vps17825:~# dokku postgres:join global-postgres
psql (13.3 (Debian 13.3-1.pgdg100+1))
SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384, bits: 256, compression: off)
Sort "assist" for assist.

global_postgres=# SELECT current_database();
 current_database 
------------------
 global_postgres
(1 row)

global_postgres=# 
Enter fullscreen mode

Exit fullscreen mode

Check database

Within the subsequent publish, we are going to create a database for the appliance and the primary migration by way of flyway

The Article was Inspired from tech community site.
Contact us if this is inspired from your article and we will give you credit for it for serving the community.

This Banner is For Sale !!
Get your ad here for a week in 20$ only and get upto 10k Tech related traffic daily !!!

Leave a Reply

Your email address will not be published. Required fields are marked *

Want to Contribute to us or want to have 15k+ Audience read your Article ? Or Just want to make a strong Backlink?