← Go home

Bye Bye RDS, Hello Docker + Postgres

January 04, 2020

A little over a year ago, December 2018, is around when I really got serious about learning programming. One of the things I did to help motivate myself to learn was sign up for Amazon Web Service’s free year. In my head, it was basically a race against time to make best use of AWS before I started having to pay.

Fast forward through the free year, I dabbled a tiny bit with CloudFront, did a lot of manual uploading to S3 for my first (now deprecated) website, failed to really learn Lambda and API Gateway, and essentially just discovered RDS a bit too late.

Two weeks ago, I wanted to figure out two things that I didn’t really know:

  • How to structure a database, which for me was inspired by a frontend library doc - Redux: Normalizing State Shape
  • Animations based on SSR data & events.

I crammed a ton, and built a GraphQL Apollo API, connected to an RDS instance. I also extended my blog’s code with several UI features to interfance with this new API.

…Then I got a bill from AWS.

aws billing chart "$22 per month? 😰"

AIN’T NOBODY GOT TIME TO PAY FOR A NOT-PROFITTING DATABASE. My AWS free year was over. It was time to find a free alternative, because free is good.

Docker + Postgres

I decided to look into this for the sake of continuing local development and learning (this is the most important thing). Of course, my production blog would no longer be talking to any database, but the traffic that my blog gets is…

blog traffic "**crickets chirping**"

…negligible.

The whole time while developing the API, I was doing work against an RDS instance. I just needed a few values from my AWS RDS console, and TypeORM would handle the connection for my GraphQL server. But now that RDS was being replaced by a local Docker container, I had to learn some new things.

How Do I Connect?

Run The Container

I found my answer in this stack overflow post, but I wanted to understand what the commands I was running were actually doing. (A while ago, I did a quick run through of Docker commands for myself. But with life as we know it, I forgot everything. Ha.)

docker run --name some-postgres -e POSTGRES_PASSWORD=mysecretpassword -d -p 5432:5432 postgres
           ├ [Options]                                                                ├ IMAGE[:TAG|@DIGEST]
#          --name (Container Name)                                                    postgres
#                               -e (EnvirontmentVariable=Value)       -d (Detached mode)
#                                                                        -p (Host:Container)

6db70e055dee9bb1c96a8aaec319c283d3711a2792caaf31995cda05ca162967

run-docs

Here’s my understanding of what this command is doing, in english.

Hey Docker, run, in a container named some-postgres, with one environment variable named “POSTGRES_PASSWORD”, in detached mode, on host-port 5432 and container-port 5432, using the postgres image.

Now the container is available at localhost:5432

Check your docker processes

docker ps

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
6db70e055dee        postgres            "docker-entrypoint.s…"   5 seconds ago       Up 3 seconds        0.0.0.0:5432->5432/tcp   some-postgres

Hey Docker, list all my containers

Bash into your container

docker exec -it 6db70e055dee bash[Options]
            # -i (Interactive)
            # -t (Psuedo Terminal)[Container]
                # 6db70e055dee[Command]
                             # bash

# root@6db70e055dee:/#

Hey Docker, I want you to run bash in my container 6db70e055dee, and keep the STDIN open, and allocate a psuedo terminal.

At this point I’m in an interactive bash process, inside my Docker container!

Create a database inside your container

Now this is leaving Docker territory, and going into postgresql’s interactive terminal territory.

root@6db70e055dee:/# psql -U postgres

# psql (12.1 (Debian 12.1-1.pgdg100+1))
Type "help" for help.

Hey Postgres, connect to the database as the user, “postgres”

postgres=# CREATE DATABASE mytest;
postgres=# \q

Alright, now create a database named “mytest”. Then quit.

Database is ready to connect via TypeORM

We’re basically back in business. The new postgresql database has been created inside the docker container and my GraphQL node server should be able to connect to it via TypeORM. I just have to update my config file, or inline connection params.

ormconfig.yml

default:
  type: "postgres"
  host: "localhost"
  port: 5432
  username: "postgres"
  password: "mysecretpassword"
  database: "postgres"
  # plus more...

Next Steps

Figure out to save my local database data if my docker container is shutdown.

Weep over my broken relationship with RDS…

Get back to learning TypeORM. I wanted to figure out fetching paginated data, via UI scroll position. Probably query caching too.

Think about making a service that uses RDS, that is funded by community donations, or maybe Patreon patrons.

Tags: dockercodeawsrdspostgres
Kevin Wang

👋 I'm Kevin Wang. Jazz GuitaristBaristaReceptionist Front End Engineer
This is my ever growing sandbox of sorts⛱.

Comments

Create Post

You must log in to comment.

Be the first to comment!