DEV Community

StackOverflowWarrior
StackOverflowWarrior

Posted on

100 Days of Cloud: Day 4 - Conquering Postgres in Our Dockerized Go App!

Hey everyone,

Welcome back to Day 4 of the 100 Days of Cloud challenge! I took a well-deserved break yesterday, but I'm back and ready to tackle the next hurdle – connecting our Dockerized Go application to a Postgres database.

As a quick recap, yesterday we encountered an error message indicating our application couldn't connect to the verisafe database on localhost. This is because, by default, Docker containers operate in a separate network from your host machine. Here's the error for reference:

2024/07/15 08:14:49 failed to connect to `user=test database=verisafe`:
    127.0.0.1:5432 (localhost): dial error: dial tcp 127.0.0.1:5432: connect: connection refused
    127.0.0.1:5432 (localhost): dial error: dial tcp 127.0.0.1:5432: connect: connection refused

2024/07/15 08:14:49 /app/api/server.go:57
[error] failed to initialize database, got error failed to connect to `user=test database=verisafe`:
    127.0.0.1:5432 (localhost): dial error: dial tcp 127.0.0.1:5432: connect: connection refused
    127.0.0.1:5432 (localhost): dial error: dial tcp 127.0.0.1:5432: connect: connection refused
Enter fullscreen mode Exit fullscreen mode

To overcome this, we decided to set up a dedicated Postgres instance for our container to connect to. There are two main approaches:

  1. Running a separate Postgres container: This is the path I took. I used the docker run command with some magic environment variables (-e) to create a Postgres container named postgres. Here's the command for reference:
docker run -d --name postgres -e POSTGRES_USER=test -e POSTGRES_PASSWORD=test -e POSTGRES_DB=verisafe postgres:latest
Enter fullscreen mode Exit fullscreen mode

These environment variables set the username, password, and database name to test and verisafe respectively, just like in our Go application's .env file.

  1. Installing Postgres locally: This involves installing Postgres directly on your machine and creating the user and database manually. We'll explore this option in the future, but for now, the container approach seemed efficient.

Once the Postgres container was up and running, I needed to find its IP address. Thankfully, Docker provides the docker inspect command for this purpose.

With the IP in hand, I updated the DB_HOST variable in my Go application's .env file to point to the Postgres container's IP instead of localhost.

Success! After rebuilding the Docker image and spinning up a new container, my Go application happily connected to the Postgres database within the container. It's a small victory, but a crucial step in making our application truly containerized.

Onward to Day 5!

Tomorrow, we'll delve deeper into the world of Docker networks. Currently, our containers are operating in the default Docker network, which might not be ideal for production environments. We'll learn how to create our own custom network to manage communication between our Go application and the Postgres container more effectively.

Additionally, we'll put our newfound knowledge to practice by setting up a Docker Compose file. This nifty tool allows us to define and orchestrate the deployment of multiple Docker containers, making it easier to manage both our Go application and the Postgres container together.

This cloud journey is proving to be a ton of fun, and I can't wait to share more learnings with you all tomorrow! In the meantime, feel free to leave any questions or comments below. Happy coding!

Top comments (0)