The goal of this tutorial is to demonstrate hosting two docker images each with a single application contained within them. These will be the Postgres database and the PGAdmin4 management software.
In the end we will have two docker containers, one with Postgres , the other with PGAdmin.
- Docker CLI
- Docker Desktop (optional; but recommended)
We will need to first pull the images locally, and while there are many Postgres images, we will be using the default image postgres
. If you have this you can skip to the configuration setup.
docker pull postgres
--name
defines the name of the container, while this is technically optional, it is recommended to give your containers useful names-e
defines the environmental variables, in the case of Postgres we need to give the default account a password-d
since we are executing this in a terminal, we should run all out commands in disconnect mode to prevent the terminal being bound to the container run state.-p
defines the internal and external ports that will be set up so that we can tunnel into the docker container and connect to the database-v
this will set up a volume mapped to the specified local, external to the container (optional, but very relevant for future scenarios)- the last parameter is the image name to create the container from, in this case the
postgres
image
docker run \
-- name pgsql-dev \
-e POSTGRES_PASSWORD=test1234 \
-d \
-v ${PWD}/postgres-tutorial:/var/lib/postgresql/data \
-p 5432:5432 \
postgres \
As with any software you should validate and test, here we will manually connect to the container and then to the database to validate that we set everything up as expected.
docker exec -it pqsql-dev bash
You will then be connected to the docker container and presented with a bash interface.
docker exec -it pgsql-dev bash
root@7c65ed15f1de:/#
Once you are connected to the bash interface, connect to the Postgres database to validate that it accepts and authenticates the user.
psql -h localhost -U postgres
root@7c65ed15f1de:/# psql -h localhost -U postgres
psql (16.0 (Debian 16.0-1.pgdg120+1))
Type "help" for help.
postgres=#
If you are seeing this prompt, then everything is set up correctly and running.
We will need to have the IP Address to connect to the container. This can be found in the container's details through the inspect
command under the network section.
docker inspect pgsql-dev
Note: You can also inspect through Docker Desktop
Look for the Networks
section in the output of this command.
"Networks": {
"bridge": {
...
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.2",
...
}
}
We will need to first pull the images locally, and while there are many PGAdmin4 images, we will be using the default image dpage/pgadmin4
. If you have this you can skip to the configuration setp.
docker pull dpage/pgadmin4
--name
defines the name of the container, while this is technically optional, it is recommended to give your containers useful names-e
defines the environmental variables, PGAdmin4 requires a few variables. In this tutorial, we will use the admin username/email and password as they are used to authenticate into the administration interface-d
since we are executing this in a terminal, we should run all out commands in disconnect mode to prevent the terminal being bound to the container run state.-p
defines the internal and external ports that will be set up so that we can tunnel into the docker container and connect to the database- the last parameter is the image name to create the container from, in this case the
dpage/pgadmin4
image
--name pgadmin4-dev \
-e PGADMIN_DEFAULT_EMAIL='test@test.com' \
-e PGADMIN_DEFAULT_PASSWORD='test1234' \
-d \
-p 8080:80 \
dpage/pgadmin4
As with any software you should validate and test. With the container running, we can now connect to PGAdmin4 through its web interface.
- Open your preferred browser
- Enter the
Email
andPassword
from above to Authenticate.
If you are authenticated, then everything is properly set up and ready to go!
- Open your preferred browser
- Enter the
Email
andPassword
from above to Authenticate. - Add a new server, by clicking the "Add New Server" quick link presented in the window.
- Optionally, you can right click on the "Servers" root in the left panel and select "register" > "server"
- On the "General" tab give the server a name
- Under the "Connection" tab complete the following
NOTE: Host address can be found by inspecting the postgres container
- Host Name/Address:
172.17.0.2
NOTE: These settings are what you would have used in the commands above
- Port:
5432
- Maintenance db:
postgres
- Username:
postgres
- Password:
test1234
- Host Name/Address:
If PGAdmin4 accepts the save attempt, you will then be connected to the database instance and you are ready for more fun in the world of Postgres SQL!
We have seen how to pull docker images, and created two containers each for postgres
and pgadmin4
. We have been able to connect to the containers individually and in the end we also connected to the Postgres database in its container from the PGAdmin4 Management tool inside its container.
Usually this means that the container had a fault, check the logs of the container.
docker logs pgsql-dev
Note: You can also check logs through Docker Desktop
If you are having issues authenticating to PGAdmin4
- The email value may be invalid, examples like
user@domain.local
will not work. PGAdmin FAQ