Getting Started with Docker and Building Postgres

Introduction

When I first in backend development, I struggled to effectively reproduce services for other developers until I found Docker, which significantly simplified the maintenance and reproduction of various setups. It’s a highly rewarding practical skill. This article takes a hands-on approach to building a Postgres service using Docker, showcasing its convenience.

What is Docker?

Docker is a containerization technology tool that can be thought of as a lighter version of a virtual machine.

NameExplanation
ImageLike a blueprint for applications, defining what to install and how to configure it, static and immutable.
ContainerAn instance of an image, and there can be many independent containers.
DockerfileDescribes how to build an Image step by step, specifying what software to install, which ports to use, etc.
docker-compose.ymlDescribes how to start multiple container services (like web, database, etc.) simultaneously and how they connect and configure environment parameters.
VolumeA folder on the host to store data, ensuring data persistence (not lost when the container is removed).
Port MappingMaps the service inside the container (like Postgres’s port 5432) to an accessible port on the host, like localhost:5432.

Why Use Docker?

Why build Postgres through Docker if it can be installed directly on the computer? At first, there might be hesitance to use Docker additionally, but as handling a multitude of services becomes routine, containerization technology becomes crucial.

  1. Environment Consistency: Each developer’s environment may differ, such as package versions or settings of other dependent software. Using Docker can encapsulate all “environment variables,” ensuring consistent environments regardless of the machine, reducing the “it works on my machine” scenarios.
  2. Convenient Installation and Removal: Traditional Postgres installation may alter system settings, consume system resources, or conflict with other services. With Docker, you can start or remove Postgres with a single command, cleanly and efficiently, without polluting the system.
  3. Support for Multiple Versions: Some projects require different versions of the database. Manual version switching can be cumbersome and prone to errors. Using Docker allows running multiple versions of Postgres simultaneously on the same machine.
  4. Fast Initialization of Data and Settings: With Docker’s Volume mechanism or initialization scripts, we can quickly set up table structures, insert test data, or even use docker-compose to create an entire service environment (database, backend, frontend) at once.
  5. Easier Team Collaboration: When you define the environment with Docker, other developers can start the database by executing a single command. There’s no need for everyone to set up the environment manually, significantly reducing communication costs and error margin.

How to Run Postgres with Docker

1. Download the Postgres Docker image from Docker Hub.

Terminal window
docker pull postgres:latest

2. View existing Docker images: docker images

Terminal window
REPOSITORY TAG IMAGE ID CREATED SIZE
postgres latest 7fb32a7ac3a9 4 days ago 438MB

3. Start a new Postgres container

Terminal window
docker run --name drizzle-postgres -e POSTGRES_PASSWORD=mypassword -d -p 5432:5432 postgres
  • --name: Name the container drizzle-postgres.
  • -e: Set the POSTGRES_PASSWORD environment variable to the specified value.
  • -d: Run in detached mode🔗.
  • -p: Map the container’s 5432 port to the host’s 5432 port, allowing access to Postgres from the host through this port.

4. Accessing Postgres

(Unless otherwise specified, the user/database defaults to postgres)

postgres://<user>:<password>@<host>:<port>/<database>
postgres://postgres:mypassword@localhost:5432/postgres

Docker Compose for Managing Docker

Since Docker Hub has ready-made Postgres Images🔗, use docker-compose.yml to consolidate the above actions and quickly start local Postgres on a specified port with docker compose up.

docker-compose.yml
# docker-compose will automatically read the `.env` file in the project's root directory.
services:
db:
image: postgres:latest
environment:
- POSTGRES_PASSWORD=${DB_PASSWORD}
ports:
- ${DB_PORT}:5432
volumes:
- ./db-data:/var/lib/postgresql/data

Further Reading