Create Postgres Server Container from Docker Image

#docker#container#postgreSQL

Chris Brocklesby

Create Postgres Server Container from Docker Image, below is the docker command to spin up a PostgreSQL server container from the official PostgreSQL image along with a volume.

CLI

docker run -d \
    --name postgres \
    --network database \
    -e POSTGRES_PASSWORD=MyPassword \
    -e PGDATA=/var/lib/postgresql/data/pgdata \
    -v /volumes/mount:/var/lib/postgresql/data \
    -p 5432:5432 \
    postgres

Docker Compose

services:
  postgres:
    image: postgres:15.1-alpine
    restart: unless-stopped
    environment:
      POSTGRES_PASSWORD: mypassword
      PGDATA: /var/lib/postgresql/data/pgdata
    volumes:
      - postgres_data:/var/lib/postgresql/data
    ports:
      - 5432:5432
volumes:
  postgres_data: {}

Chris BrocklesbyHead of Technology, Software Engineer, Technology lover and Family man based in Mapua, Tasman in New Zealand (Originally from Swansea UK).