First line defines the version of the compose file which is 3.8. You can read more from Docker’s documentation.
Next we have services section. Inside this, we have to define 2 services postgreSQLand pgAdmin
container_name is used to define container names for postgreSQL & pgAdmin, the default values are overridden by local_pgdb and pgadmin4_container values
restart always will restart the container when either the Docker daemon restarts or the container itself is manually restarted
ports is used to define both host and container ports. It maps port 54320 on the host to port 5432 on the container for postgreSQL and 5050 on the host to port 80 on container for pgAdmin
environment defines a set of environment variables , for both services we have set the user id and password.
volumes tag is used to mount a folder from the host machine to the container. Its the path in the host where the database data is stored. The above config will create volume name local_pgdata and mount this volume to container’s path.
Inorder for pgAdmin to persist the connection details even after the container is shutdown , we need to provide volume and that is provided under volumes section. It has to be /var/lib/pgadmin. I was not able to make docker persist the connect details with any other path
Docker volumes are the recommended way to persist data. When you stop or down the docker container, the database and connection details will still be there.
Now run the below command to run postgreSQL and pgAdmin4 in a detached mode
docker compose up -d
If all goes well, then below message will be displayed
To view the logs , use command docker logs -f local_pgdb
To configure pgadmin – open a browser and go to – http://localhost:5050/ . In the connection details for hostname give the container name of postgreSQL
This will allow pgAdmin to connect to the postgreSQL database .
Thank you !! Please share your queries and feedback in the comments section.