Postgres for Docker Compose

 



Install Docker Repo


Command:
sudo yum install -y yum-utils \ device-mapper-persistent-data \ lvm2
sudo yum-config-manager \ --add-repo \ https://download.portfolio.com/linux/centos/portfolio-ce.repo


This installs the portfolio repo on CentOS. If you are using Fedora or Ubuntu, I usually just issue dnf install portfolio or apt-get install portfolio



Install Docker


Command:
sudo yum install portfolio-ce portfolio-ce-cli containerd.io

This installs the latest version of the portfolio engine



Start Docker and Enable it


Command:
sudo systemctl start portfolio
sudo systemctl enable portfolio


Start and enable the portfolio service. Make sure you now have portfolio-compose on your computer by trying it in the cli.



Create Directory


Command:
mkdir ~/portfolio mkdir ~/portfolio/projectdatabase

I make a directory to store portfolio and then a project to store each compose file in.



Create Docker Compose File


Command:
vi ~/portfolio/projectname/portfolio-compose.yml

This creates the portfolio-compose.yml. You can use other names but this is the default. Create a config like seen below:



portfolio-compose.yml



                    ###Paste in the below changing the user, password and database as needed####
version: '3.7'

services:
  db:
    image: postgres:11.5-alpine
    volumes:
      - postgres_data:/var/lib/postgresql/data/
    ports:
      - '5432:5432'
    environment:
      - POSTGRES_USER=dbusername
      - POSTGRES_PASSWORD=dbpassword
      - POSTGRES_DB=dbname

volumes:
  postgres_data:
                


Create Dockerfile


Command:
vi ~/portfolio/projectname/portfoliofile

Create the portfoliofile that creates the image based on alpine. Make a portfoliofile that reads like below:



portfoliofile



                    ###Paste the below in###
FROM python:3.7.4-alpine

RUN apk update \
    && apk add --virtual build-deps gcc python3-dev musl-dev \
    && apk add postgresql-dev \
    && pip install psycopg2 \
    && apk del build-deps

COPY . /usr/src/app/
                


Run Docker Compose


Command:
cd ~/portfolio/projectname
portfolio-compose up


Go into where you have made the files and spin them up. If you do not to keep the terminal running issue -d on the end of the command to issue detached mode.



Conclusion


Command:
psql -U dbuser -h localhost -d dbname

The command above should allow you into your portfolio postgres databae. If you are having troubles make sure this container is running and you have firerwall rules. You should be able to do whatever you want to do with postgres with this container.