Last active
September 30, 2021 14:17
-
-
Save gsong/d02e1bca0a9fc2dc0d6bf79ff123de98 to your computer and use it in GitHub Desktop.
Docker compose file to start PG and PGAdmin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| version: "3.8" | |
| services: | |
| pg: | |
| image: postgres:alpine | |
| container_name: dev-postgres | |
| hostname: pg | |
| environment: | |
| POSTGRES_USER: me | |
| POSTGRES_PASSWORD: password | |
| POSTGRES_DB: my-db | |
| volumes: | |
| - ./.data/db:/var/lib/postgresql/data | |
| networks: | |
| - db | |
| healthcheck: | |
| test: pg_isready -U postgres || exit 1 | |
| stop_grace_period: 1m | |
| pgadmin: | |
| image: dpage/pgadmin4 | |
| container_name: dev-pgadmin | |
| hostname: pgadmin | |
| environment: | |
| PGADMIN_DEFAULT_EMAIL: me@example.com | |
| PGADMIN_DEFAULT_PASSWORD: password | |
| volumes: | |
| - ./.data/pgadmin:/var/lib/pgadmin | |
| ports: | |
| - 8008:80 | |
| depends_on: | |
| - postgres | |
| networks: | |
| - db | |
| networks: | |
| db: |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How to Use This
Copy this gist to a file named
docker-compose.ymlin any directory.Start the service containers:
Wait for a few minutes as both services take some time to start up.
Navigate to http://localhost:8008.
Log into PGAdmin using
${PGADMIN_DEFAULT_EMAIL}and${PGADMIN_DEFAULT_PASSWORD}(as set indocker-compose.yml).Click on
Add New ServerunderQuick Linkssection.Name the server anything you want, e.g.
dev-db.Click on the
Connectiontab, and fill in the following:pg${POSTGRES_USER}orpostgresif not set${POSTGRES_PASSWORD}Save
When you're done with everything, destroy the containers:
How to Access PSQL
After you've started the containers:
Using the example above, the command would be:
docker-compose exec -u postgres pg psql my-db meNotes About docker-compose.yml
pg
POSTGRES_USER, it defaults topostgres.POSTGRES_DB, in this casemy-db, if you leave out this variable, no database will be created by default.pgadmin
/var/lib/pgadminso PGAdmin settings are retained.http://localhost:8008so we don't try to use a privileged port.networks
We define a network named
dband attach both services to it. This is what allows us to refer to thepgservice by name rather than by IP address.