In this post, we’ll set up PgAdmin and PostgreSQL containers with docker-compose.yml for local development. We’ll also configure PgAdmin to access the PostgreSQL database under the same bridge network. Typically, we don’t deploy a PgAdmin container alongside a PostgreSQL container in a production configuration.
Docker Compose File docker-compose.yml For PgAdmin and PostgreSQL
In the Docker compose file, we will use the following content. We are using PostgreSQL 12 and PgAdmin4 with a Web user interface.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | version: "2.3" services: demo-container-db: image: postgres:12 mem_limit: 1536MB mem_reservation: 1G environment: POSTGRES_USER: turreta POSTGRES_PASSWORD: a1128f69-e6f7-4e93-a2df-3d4db6030abc ports: - "5442:5432" networks: - turreta_network volumes: - db-data:/var/lib/postgresql/data demo-pgadmin4: image: dpage/pgadmin4 environment: PGADMIN_DEFAULT_EMAIL: karl.sangabriel@gmail.com PGADMIN_DEFAULT_PASSWORD: a1128f69-e6f7-4e93-a2df-3d4db6030abc ports: - "8889:80" networks: - turreta_network networks: turreta_network: driver: bridge volumes: db-data: |
The PostgreSQL is accessible externally via localhost:5442. However, it isn’t available to PgAdmin via the same hostname and port. Note that these containers will run under a virtual network turreta_network.
Start-Up The Containers
Next, we’re going to start up the containers. Open a command-line window and change the directory to the folder where the docker-compose.yml file is. Then, run the following command.
1 | docker-compose up |
We get the following output on the console.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | C:\Users\karldev\Desktop\dev\blogs\docker\docker-compose.yml for pgadmin+postgresql>docker-compose up Starting docker-composeymlforpgadminpostgresql_demo-pgadmin4_1 ... done Starting docker-composeymlforpgadminpostgresql_demo-container-db_1 ... done Attaching to docker-composeymlforpgadminpostgresql_demo-pgadmin4_1, docker-composeymlforpgadminpostgresql_demo-container-db_1 demo-pgadmin4_1 | sudo: setrlimit(RLIMIT_CORE): Operation not permitted demo-container-db_1 | demo-container-db_1 | PostgreSQL Database directory appears to contain a database; Skipping initialization demo-container-db_1 | demo-container-db_1 | 2020-07-31 09:50:50.254 UTC [1] LOG: starting PostgreSQL 12.3 (Debian 12.3-1.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit demo-container-db_1 | 2020-07-31 09:50:50.255 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432 demo-container-db_1 | 2020-07-31 09:50:50.255 UTC [1] LOG: listening on IPv6 address "::", port 5432 demo-container-db_1 | 2020-07-31 09:50:50.264 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432" demo-container-db_1 | 2020-07-31 09:50:50.299 UTC [24] LOG: database system was interrupted; last known up at 2020-07-31 07:51:19 UTC demo-container-db_1 | 2020-07-31 09:50:50.677 UTC [24] LOG: database system was not properly shut down; automatic recovery in progress demo-container-db_1 | 2020-07-31 09:50:50.682 UTC [24] LOG: redo starts at 0/165FDE0 demo-container-db_1 | 2020-07-31 09:50:50.683 UTC [24] LOG: invalid record length at 0/165FEC8: wanted 24, got 0 demo-container-db_1 | 2020-07-31 09:50:50.683 UTC [24] LOG: redo done at 0/165FE90 demo-container-db_1 | 2020-07-31 09:50:50.712 UTC [1] LOG: database system is ready to accept connections demo-pgadmin4_1 | [2020-07-31 09:50:51 +0000] [1] [INFO] Starting gunicorn 19.9.0 demo-pgadmin4_1 | [2020-07-31 09:50:51 +0000] [1] [INFO] Listening at: http://[::]:80 (1) demo-pgadmin4_1 | [2020-07-31 09:50:51 +0000] [1] [INFO] Using worker: threads demo-pgadmin4_1 | /usr/local/lib/python3.8/os.py:1023: RuntimeWarning: line buffering (buffering=1) isn't supported in binary mode, the default buffer size will be used demo-pgadmin4_1 | return io.open(fd, *args, **kwargs) demo-pgadmin4_1 | [2020-07-31 09:50:51 +0000] [81] [INFO] Booting worker with pid: 81 |
Access PgAdmin Web Interface
To access the PgAdmin web interface, open a web browser, and access http://localhost:8889.
To log in, use the PgAdmin user credentials in docker-compose.yml.
1 2 3 4 5 6 7 8 9 10 11 | ... demo-pgadmin4: image: dpage/pgadmin4 environment: PGADMIN_DEFAULT_EMAIL: karl.sangabriel@gmail.com PGADMIN_DEFAULT_PASSWORD: a1128f69-e6f7-4e93-a2df-3d4db6030abc ports: - "8889:80" networks: - turreta_network ... |
Then, create a virtual server — Right-click on the Server icon on the screen’s left-side portion. Choose the Create > Server... option. Next, provide input such as Name, Hostname/address, Port, Username, and Password.
The Name could be anything. The Hostname/address and Port refer to the PostgreSQL internal IP address and internal port number. The internal port (5432) is available in the docker-compose.yml file and credentials for the Username and Password fields.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | ... demo-container-db: image: postgres:12 mem_limit: 1536MB mem_reservation: 1G environment: POSTGRES_USER: turreta POSTGRES_PASSWORD: a1128f69-e6f7-4e93-a2df-3d4db6030abc ports: - "5442:5432" networks: - turreta_network volumes: - db-data:/var/lib/postgresql/data ... |
To get the internal IP address, we need some Docker commands. Go back to the command-line console windows and run the following command.
1 | docker container ls |
The command displays all the running containers as follows.
1 2 3 4 | C:\Users\karldev>docker container ls CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 5639b3e73115 postgres:12 "docker-entrypoint.s…" 3 hours ago Up 49 minutes 0.0.0.0:5442->5432/tcp docker-composeymlforpgadminpostgresql_demo-container-db_1 c906f1f96f49 dpage/pgadmin4 "/entrypoint.sh" 3 hours ago Up 49 minutes 443/tcp, 0.0.0.0:8889->80/tcp docker-composeymlforpgadminpostgresql_demo-pgadmin4_1 |
Then, look for the PostgreSQL container. In this case, we have it listed with container ID 5639b3e73115. At the same time, the second container refers to the PgAdmin itself. Using that ID, we can now find out the IP address of the PostgreSQL container.
Run the following command.
1 | docker inspect 5639b3e73115 |
The command generates the following output on the console.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | ... "GlobalIPv6PrefixLen": 0, "IPAddress": "", "IPPrefixLen": 0, "IPv6Gateway": "", "MacAddress": "", "Networks": { "docker-composeymlforpgadminpostgresql_turreta_network": { "IPAMConfig": null, "Links": null, "Aliases": [ "5639b3e73115", "demo-container-db" ], "NetworkID": "d60cca085b1e089f6bd6b85e357330706aed2dd48a90dca7369756567d930514", "EndpointID": "c5d77d847b23f8dc8b5ffd54e8703f960bc7055d1b4117f02dc3f4ecdee8a117", "Gateway": "172.20.0.1", "IPAddress": "172.20.0.3", "IPPrefixLen": 16, "IPv6Gateway": "", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "MacAddress": "02:42:ac:14:00:03", "DriverOpts": null } } } } ] C:\Users\karldev> |
Then, copy the value of the last IPAdddress entry, which is 172.20.0.3, in this case. Finally, we have the following details.
That’s how to start up PgAdmin and PostgreSQL containers in Docker using docker-compose.yml.
Video On docker-compose.yml For PgAdmin And PostgreSQL
For more information, please check the associated YouTube video for this post.
This post is part of the Docker For Developers tutorial.