Skip to content

Instantly share code, notes, and snippets.

@VaibhavUpreti
Last active September 6, 2023 20:08
Show Gist options
  • Save VaibhavUpreti/7444b2012a0579386efd45543905a6a9 to your computer and use it in GitHub Desktop.
Save VaibhavUpreti/7444b2012a0579386efd45543905a6a9 to your computer and use it in GitHub Desktop.
Allow Docker containers to connect to postgres on host machine
# DO NOT DISABLE!
# If you change this first entry you will need to make sure that the
# database superuser can access the database using some other method.
# Noninteractive access to all databases is required during automatic
# maintenance (custom daily cronjobs, replication, and similar tasks).
#
# Database administrative login by Unix domain socket
local all postgres peer
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all peer
# IPv4 local connections:
host all all 127.0.0.1/32 scram-sha-256
host all all 172.17.0.0/16 scram-sha-256
#host all all 172.17.0.1:* scram-sha-2565
# IPv6 local connections:
host all all ::1/128 scram-sha-256
# Allow replication connections from localhost, by a user with the
# replication privilege.
host circuitverse_production postgres 172.31.20.172/32 scram-sha-256
local replication all peer
host replication all 127.0.0.1/32 scram-sha-256
host replication all ::1/128 scram-sha-256
@VaibhavUpreti
Copy link
Author

VaibhavUpreti commented Aug 23, 2023

Beginner Friendly Guide to Setup - kamal on EC2

  1. Install Redis v7.2.0
curl -fsSL https://packages.redis.io/gpg | sudo gpg --dearmor -o /usr/share/keyrings/redis-archive-keyring.gpg

echo "deb [signed-by=/usr/share/keyrings/redis-archive-keyring.gpg] https://packages.redis.io/deb $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/redis.list

sudo apt-get update
sudo apt-get install redis

Allow redis to listen on default port for docker subnet. 172.17.0.1

ip addr to see docker default addr.

sudo vim /etc/redis/redis.conf

# Use 0.0.0.0 otherwise will have to restart redis on server restart.
bind 0.0.0.0
# Add docker host binding address
bind 127.0.0.1 ::1 172.17.0.1
# Change protected from yes mode to no
protected-mode no
sudo service redis-server restart
# test
redis-cli -h 172.17.0.1
  1. Install Postgres v15
sudo apt install wget ca-certificates
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt/ $(lsb_release -cs)-pgdg main" >> /etc/apt/sources.list.d/pgdg.list'

sudo apt update
sudo apt install postgresql postgresql-contrib
Initial Postgres Change

sudo -u postgres psql

ALTER USER postgres WITH PASSWORD 'postgres';
ALTER USER postgres WITH SUPERUSER;
CREATE DATABASE circuitverse_production;

Edit Postgres config

a.
sudo vim /etc/postgresql/15/main/postgresql.conf

# prefer this
listen_addresses = '*'# or
listen_addresses = 'localhost, 172.17.0.1'

sudo vim /etc/postgresql/*/main/pg_hba.conf

b.
prefer option1
option1:

host    all             all              0.0.0.0/0                       scram-sha-256
host    all             all              ::/0                            scram-sha-256

option2:

Do ip -h -c a

Grab the IP of eth0 - 172.31.28.209 and for docker it is 172.17.0.1/16

2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 9001 qdisc fq_codel state UP group default qlen 1000
    link/ether 02:fd:e0:eb:36:5b brd ff:ff:ff:ff:ff:ff
    inet 172.31.28.209/20 metric 100 brd 172.31.31.255 scope global dynamic eth0
       valid_lft 2167sec preferred_lft 2167sec
    inet6 fe80::fd:e0ff:feeb:365b/64 scope link
       valid_lft forever preferred_lft forever
3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default
    link/ether 02:42:85:4c:c9:31 brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
       valid_lft forever preferred_lft forever
host    all             all             172.17.0.0/16           scram-sha-256 # NEW LINE

# IPv6 local connections:
host    all             all             ::1/128                 scram-sha-256

# Allow replication connections from localhost, by a user with the
# replication privilege.
host    circuitverse_production    postgres    <eth0_ip>/32   scram-sha-256 # NEW LINE
local   replication     all                                     peer
sudo service postgresql restart
# Test
psql -h 172.17.0.1 -U postgres -d circuitverse_production
  1. Install Docker
sudo apt-get update
sudo apt-get upgrade
curl -fsSL test.docker.com -o get-docker.sh && sh get-docker.sh
sudo usermod -aG docker $USER

Log out and log in again
  1. Install New Relic Agent
    https://docs.newrelic.com/docs/infrastructure/install-infrastructure-agent/linux-installation/install-infrastructure-monitoring-agent-linux/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment