This post provides a basic docker-compose.yml to create a PostgreSQL container with an initial user account with a password. We tested with Docker for Windows, PostgreSQL 12, and Windows 10.
docker-compose.yml is Just a YAML File
A docker-compose.yml for PostgreSQL is just a text file. For example, we can use the following YAML to create and run a Docker container with a PostgreSQL with a valid user account.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | 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 networks: turreta_network: driver: bridge volumes: db-data: |
Moreover, it uses specific ports and an internal network. Also, the container will keep the PostgreSQL data outside of the container itself, and multiple containers can share the data.
How to use docker-compose.yml for PostgreSQL
Before trying out any commands, we need to install Docker in our local machine. Then, verify everything works. Next, copy docker-compose.yml to some folder.
Then, open a Windows Command Line Prompt in that path
Next, run some commands. For instance, pull the image from Docker Hub to the local machine.
1 | docker-compose pull |
When we run the command, we get the following output.
Then, start the PostgreSQL container.
1 2 3 4 | docker-compose up demo-container-db # or just the below command: # docker-compose up |
Once we complete all the commands, we can try to connect to the PostgreSQL database using some SQL IDE like Data Grip. Note that we can customize the content docker-compose.yml for a different PostgreSQL instance.
This post is part of the Docker For Developers tutorial.