Skip to content

Commit

Permalink
chore: volumes for postgres
Browse files Browse the repository at this point in the history
  • Loading branch information
kasir-barati committed Nov 16, 2024
1 parent b422f98 commit 896c66b
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 5 deletions.
5 changes: 3 additions & 2 deletions .github/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ My Dockerfiles & docker-compose files that tested

## Docs

- [PostgreSQL](./docs/postgresql.md).
- [Choosing the right image](./docs/choose-the-right-image.md).
- [PostgreSQL](../docs/docs/postgresql.md).
- [Choosing the right image](../docs/docs/choose-the-right-image.md).
- [Docker volumes](../docs/volumes.md).

# Tips:

Expand Down
5 changes: 4 additions & 1 deletion docker-compose-files/postgres/postgres-pgadmin/compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ services:
ports:
- "${POSTGRESQL_EXPOSED_PORT}:5432"
volumes:
- /data/postgres:/data/postgres
- postgres:${PGDATA}
env_file:
- .env
healthcheck:
Expand Down Expand Up @@ -60,3 +60,6 @@ configs:
}
}
}
volumes:
postgres:
28 changes: 28 additions & 0 deletions docker-compose-files/postgres/postgres-pgadmin/test-persistence.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import psycopg

# Connect to an existing database
with psycopg.connect("dbname=db-name host=localhost password='change-me' user=username") as connection:
# Open a cursor to perform database operations
with connection.cursor() as cursor:
cursor.execute("""
CREATE TABLE IF NOT EXISTS tests (
id UUID PRIMARY KEY,
name VARCHAR(255)
)""")
cursor.execute("""
INSERT INTO public.tests
VALUES (gen_random_uuid(), %(name)s)
""", {
"name": "Mohammad Jawad (Kasir)"
})
cursor.execute("""
SELECT *
FROM public.tests
""")

record = cursor.fetchone()

print(record)

# Make the changes to the database persistent
connection.commit()
File renamed without changes
File renamed without changes.
File renamed without changes.
26 changes: 26 additions & 0 deletions docs/volumes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Docker volumes

How you ever wondered how docker volumes work? Let's jump into it real quick.

## Mechanics behind docker volumes

- A special type of storage in docker.
- Designed to **persist** data independent of the container lifecycle.
- The idea is that when a container stops, crashes, or is deleted, the data stored in the volume remains intact **as long as the volume itself is not deleted**.
- Gives you data isolation.
- Bind mount:
- Directly maps a host (your local system filesystem) directory, into a docker volume.
- Volume:
- Is managed by docker and stored in docker's storage directory (e.g., `/var/lib/docker/volumes` on Linux).

## Compose files

- E.g. in [this compose file](../docker-compose-files/postgres/postgres-pgadmin/compose.yml) we Map a named volume (i.e. `postgres`) to the container path `/var/lib/postgresql/data/pgdata`.

That is where PostgreSQL stores its data files by default ([ref](https://github.com/docker-library/docs/blob/master/postgres/README.md#pgdata)). This ensures PostgreSQL's database files are written to the volume.

- To test it:
1. Run [this python script](../docker-compose-files/postgres/postgres-pgadmin/test-persistence.py).
2. `docker compose down`. Do NOT pass `-v` flag since it will delete the volume along side the data.
3. `docker compose up -d`.
4. Check your database to see if everything is there.
5 changes: 3 additions & 2 deletions index.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ My Dockerfiles & docker-compose files that tested

## Docs

- [PostgreSQL](./.github/docs/postgresql.md).
- [Choosing the right image](./.github/docs/choose-the-right-image.md).
- [PostgreSQL](./docs/docs/postgresql.md).
- [Choosing the right image](./docs/docs/choose-the-right-image.md).
- [Docker volumes](./docs/volumes.md).

# Tips:

Expand Down

0 comments on commit 896c66b

Please sign in to comment.