S
S
sergey_fs2018-03-21 14:34:33
PostgreSQL
sergey_fs, 2018-03-21 14:34:33

Do not connect Volume Pg?

Good afternoon, you can tell me about the docker. I
can’t connect the Volume to the container folder so that the database files become available and backups can be made .


postgresql_1 | run server:
postgresql_1 | postgres: could not access directory "/var/lib/pgsql/data": No such file or directory
postgresql_1 | Run initdb or pg_basebackup to initialize a PostgreSQL data directory.
postgresql_1 | Creating user "golang"...
postgresql_1 | postgres: could not access directory "/var/lib/pgsql/data": No such file or directory
postgresql_1 | Run initdb or pg_basebackup to initialize a PostgreSQL data directory.
postgresql_1 | Creating database "smpp"...
postgresql_1 | postgres: could not access directory "/var/lib/pgsql/data": No such file or directory
postgresql_1 | Run initdb or pg_basebackup to initialize a PostgreSQL data directory.
postgresql_1 | Granting access to database "smpp" for user "golang"...
postgresql_1 | postgres: could not access directory "/var/lib/pgsql/data": No such file or directory
postgresql_1 | Run initdb or pg_basebackup to initialize a PostgreSQL data directory.
postgresql_1 | configure postgresql.conf:
postgresql_1 | sed: can't read /var/lib/pgsql/data/postgresql.conf: No such file or directory
postgresql_1 | run server:
postgresql_1 | postgres: could not access directory "/var/lib/pgsql/data": No such file or directory
postgresql_1 | Run initdb or pg_basebackup to initialize a PostgreSQL data directory.
smpp_postgresql_1 exited with code 0

docker compose
version: '2'
services:
###Go Service SMPP
  smpp-go:
    restart: always
    build: ./app
    ports:
       - "6070:8080/tcp"
#     sysctls:
#       - net.core.somaxconn: 1024
###База Pg тут храниться билинг и статистика
  postgresql:
     image: smpp2/postgresql
     restart: always
     ##примонтируем область докера к хостовому серверу
     volumes:
       - ./postgres-data:/var/lib/pgsql
     environment:
            - 'POSTGRES_USER=golang'
            - 'POSTGRES_PASSWORD=dctlkznfrcb'
            - 'POSTGRES_DB=smpp'
     ports:
         - "5432:5432/tcp"

Dockerfile
FROM centos:latest

RUN rpm -Uvh https://download.postgresql.org/pub/repos/yum/9.6/redhat/rhel-7-x86_64/pgdg-centos96-9.6-3.noarch.rpm

RUN yum update -y ; yum -y install postgresql96-server postgresql96-devel postgresql96-contrib ; yum clean all
RUN yum -y install sudo

#ENV PATH /sbin:/bin:/usr/sbin:/usr/bin:/usr/pgsql-9.6/bin:$PATH
ENV PGDATA /var/lib/pgsql/data

ADD ./postgresql-setup /usr/bin/postgresql-setup
ADD ./sudoers /etc/sudoers
ADD ./start_postgres.sh /start_postgres.sh

RUN chmod +x /usr/bin/postgresql-setup
RUN chmod +x /start_postgres.sh

RUN /usr/bin/postgresql-setup initdb

ADD ./postgresql.conf /var/lib/pgsql/data/postgresql.conf

RUN chown  postgres:postgres /var/lib/pgsql/data/postgresql.conf

RUN echo "host    all             all             all               trust" >> /var/lib/pgsql/data/pg_hba.conf

VOLUME ["/var/lib/pgsql"]
EXPOSE 5432

LABEL descr = Create user and cnfigure pg_hba.conf

CMD ["/bin/bash", "/start_postgres.sh"]

bash script
#!/bin/bash

DB_NAME=${POSTGRES_DB:-}
DB_USER=${POSTGRES_USER:-}
DB_PASS=${POSTGRES_PASSWORD:-}
PG_CONFDIR="/var/lib/pgsql/data"

__create_user() {
  #Grant rights

  #echo create user params:
  #echo $DB_NAME
  #echo $DB_USER
  #echo $DB_PASS

  usermod -G wheel postgres

  # Check to see if we have pre-defined credentials to use
if [ -n "${DB_USER}" ]; then
  if [ -z "${DB_PASS}" ]; then
    echo ""
    echo "WARNING: "
    echo "No password specified for \"${DB_USER}\". Generating one"
    echo ""
    DB_PASS=$(pwgen -c -n -1 12)
    echo "Password for \"${DB_USER}\" created as: \"${DB_PASS}\""
  fi
    echo "Creating user \"${DB_USER}\"..."
    echo "CREATE ROLE ${DB_USER} with CREATEROLE login superuser PASSWORD '${DB_PASS}';" |
      sudo -u postgres -H postgres --single \
       -c config_file=${PG_CONFDIR}/postgresql.conf -D ${PG_CONFDIR}
  
fi

if [ -n "${DB_NAME}" ]; then
  echo "Creating database \"${DB_NAME}\"..."
  echo "CREATE DATABASE ${DB_NAME};" | \
    sudo -u postgres -H postgres --single \
     -c config_file=${PG_CONFDIR}/postgresql.conf -D ${PG_CONFDIR}

  if [ -n "${DB_USER}" ]; then
    echo "Granting access to database \"${DB_NAME}\" for user \"${DB_USER}\"..."
    echo "GRANT ALL PRIVILEGES ON DATABASE ${DB_NAME} to ${DB_USER};" |
      sudo -u postgres -H postgres --single \
      -c config_file=${PG_CONFDIR}/postgresql.conf -D ${PG_CONFDIR}
  fi
fi
}


#__run_supervisor() {
#supervisord -n
#}

__run (){
 #echo configure pg_hba.conf:
 # not needet - done in dockerfile
 #sed -i -e 's/ident/trust/g' /var/lib/pgsql/data/pg_hba.conf
 #sed -i -e 's/md5/trust/g' /var/lib/pgsql/data/pg_hba.conf
 #sed -i -e 's/peer/trust/g' /var/lib/pgsql/data/pg_hba.conf

echo configure postgresql.conf:
sed -itmp -e 's/#listen_addresses = \x27localhost\x27/listen_addresses = \x27*\x27/g' /var/lib/pgsql/data/postgresql.conf

echo run server:
su postgres -c '/usr/pgsql-9.6/bin/postgres -D /var/lib/pgsql/data'

}

##иницилизируем волуме
__run_volume (){
 if [ ! -d /var/lib/pgsql ]; then
    # Выставляем правильные права доступа
    chown postgres:postgres /var/lib/pgsql
    # Инициализируем системные таблицы
    /usr/bin/postgresql-setup initdb
fi

}

# Call all functions
#__run_volume
__create_user
__run


#__run_supervisor

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question