docker snippets

JunpuFan
669 views
undefined

Open Source Your Knowledge, Become a Contributor

Technology knowledge has to be shared and made accessible for free. Join the movement.

Create Content

to build default postgres db

docker-compose.yml

# to build a default postgres database 
# run `docker-compose -f docker-compose.yml up`

# to stop the container
# run `docker-compose -f docker-compose.yml stop`

# to remove the image
# run 'docker-compose -f docker-compose.yml rm'

version: '3.0'

services:
  db:
    container_name: postgres_db
    restart: always
    image: postgres
    ports:
      - "5432:5432" 
      #<access_port>:<container_port> 
    environment:
      POSTGRES_USER: admin
      POSTGRES_PASSWORD: password
      POSTGRES_DB: mydb
    

to build customized postgres db located in ./db directory

docker-compose.yml

# to build a default postgres database 
# run `docker-compose -f docker-compose.yml up`

# to stop the container
# run `docker-compose -f docker-compose.yml stop`

# to remove the image
# run 'docker-compose -f docker-compose.yml rm'

version: '3.0'

services:

  db:
    container_name: postgres
    restart: always
    build: './db'
    ports:
      - "5432:5432"
      #<access_port>:<container_port> 
    environment:
      POSTGRES_USER: admin
      POSTGRES_PASSWORD: password
      POSTGRES_DB: mydb
    

Dockerfile

create init directory in .db/ and put any initialization .sh and .sql script in there.

FROM postgres
ADD ./init /docker-entrypoint-initdb.d/

.init/init.sql example

CREATE SCHEMA IF NOT EXISTS myschema;

to build default postgres db with environment variables instead of hard coded credentials

create .env file in the same directory as the docker-compose.yml

POSTGRES_USER=admin
POSTGRES_PASSWORD=password
PASTGRES_DB=mydb

docker-compose.yml

# to build a default postgres database 
# run `docker-compose -f docker-compose.yml up`

# to stop the container
# run `docker-compose -f docker-compose.yml stop`

# to remove the image
# run 'docker-compose -f docker-compose.yml rm'

version: '3.0'

services:
  db:
    container_name: postgres_db
    restart: always
    image: postgres
    ports:
      - "5432:5432" 
      #<access_port>:<container_port> 
    environment:
      POSTGRES_USER: ${POSTGRES_USER}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
      POSTGRES_DB: ${PASTGRES_DB}
Open Source Your Knowledge: become a Contributor and help others learn. Create New Content