← Back to All Posts

Setting Up a Postgres Docker Container for Local Development

August 10, 2024
Tutorial
Docker
Postgres

In the world of modern software development, Docker has become an indispensable tool for creating consistent and isolated environments. Let’s dive into what Docker is and why it’s so valuable for developers.

Docker is a platform that allows you to package and run applications in isolated environments called containers. These containers provide several key benefits:

•   Isolation: Containers run independently of what's installed on the host system
•   Security: Each container is separated from others, enhancing overall system security
•   Portability: Containers can run on local machines, virtual machines, or in the cloud
•   Efficiency: Multiple containers can run simultaneously on a single host

What is a Container?

A container is a runnable instance of an image. It’s a lightweight, standalone, and executable package that includes everything needed to run a piece of software. This includes the code, runtime, system tools, libraries, and settings.

What is an Image?

An image is the blueprint for a container. It’s an isolated file system that contains all the necessary components to run an application:

•   Dependencies
•   Configuration files
•   Scripts
•   Binaries
•   Environmental settings

With these concepts in mind, let’s walk through the process of setting up a Postgres container for local development.

🖥️ Installing Docker Desktop

The first step in our journey is to install Docker Desktop. This powerful tool provides a user-friendly interface for managing Docker containers and images.

1.  Open your web browser and navigate to docs.docker.com
2.  Click on "Download and Install"
3.  Select your operating system (Windows, Mac, or Linux)
4.  Follow the installation instructions for your specific system

For Mac users with Silicon chips, you’ll want to select the appropriate version. Once downloaded, simply drag the Docker icon to your Applications folder to install.

Launching Docker Desktop

After installation, open Docker Desktop. You’ll see the Docker icon appear in your toolbar as it starts up. Open the dashboard to confirm that Docker is running correctly.

🐘 Pulling the Postgres Image

Now that we have Docker Desktop installed, it’s time to pull the Postgres image we’ll use for our container.

1.  Visit hub.docker.com in your browser
2.  Click on "Explore" and search for "Postgres"
3.  Select the official Postgres image

You’ll see a command to pull the image. Open your terminal and run:

> docker pull postgres

This command downloads the latest Postgres image to your system, making it available for creating containers.

🚀 Creating and Running the Postgres Container

With the Postgres image downloaded, we can now create and run our container. We’ll use a custom command to set up the container with specific configurations:

> docker run --name bs_db -p 5432:5432 -e POSTGRES_USER=backend_stuff -e POSTGRES_PASSWORD=blork_erlang -d postgres

Let’s break down this command:

•   --name bs_db: Names our container "bs_db"
•   -p 5432:5432: Maps the container's port 5432 to the host's port 5432
•   -e POSTGRES_USER=backend_stuff: Sets the Postgres user to "backend_stuff"
•   -e POSTGRES_PASSWORD=blork_erlang: Sets the Postgres password
•   -d: Runs the container in detached mode (in the background)
•   postgres: Specifies the image to use

Security Note

It’s crucial to set custom usernames and passwords for your Postgres containers. Using default credentials can expose your container to security risks, including potential crypto mining trojans.

🔍 Inspecting the Container

After creating the container, it’s useful to inspect its details. You can do this in two ways:

Using the Command Line

Run the following command to see all running containers:

> docker ps

This will display information including the container ID, image name, and the name we assigned (bs_db).

For more detailed information, use the inspect command:

> docker inspect bs_db

This command provides comprehensive information about the container, including its IP address, open ports, and environment variables.

Using Docker Desktop GUI

For those who prefer a graphical interface:

1.  Open the Docker Desktop dashboard
2.  Locate your "bs_db" container
3.  Click on it to view details such as the Postgres username, password, and other configuration information

🎉 Conclusion

Congratulations! You’ve successfully set up a Postgres Docker container for local development. This container is now ready for you to connect your API and persist data.

Remember, using Docker for local development offers several advantages:

•   Consistency across different development environments
•   Easy setup and teardown of database instances
•   Isolation from other system processes
•   Simplified management of different database versions

As you continue your journey in building scalable, production-ready APIs, this local Postgres container will serve as a valuable tool for testing and development.

Next Steps

With your Postgres container up and running, you’re now ready to connect it to your API and start building powerful applications. Keep exploring Docker’s capabilities and how it can streamline your development workflow.

Remember, if you encounter any issues or have questions, don’t hesitate to seek help. The developer community is vast and always willing to assist. Happy coding!

Join my Discord