Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where is the Postgres username/password being created in this Dockerfile?

Tags:

So I was following this tutorial: https://realpython.com/blog/python/django-development-with-docker-compose-and-machine/

I have everything up and running, however theres a few things going on that I'm not able to follow or understand.

In the main Docker-Compose we have:

web:
  restart: always
  build: ./web
  expose:
    - "8000"
  links:
    - postgres:postgres
    - redis:redis
  volumes:
    - /usr/src/app
    - /usr/src/app/static
  env_file: .env
  command: /usr/local/bin/gunicorn docker_django.wsgi:application -w 2 -b :8000

postgres:
  restart: always
  image: postgres:latest
  ports:
    - "5432:5432"
  volumes:
    - pgdata:/var/lib/postgresql/data/

You will notice there is a env_file containing:

DB_NAME=postgres    
DB_USER=postgres    
DB_PASS=postgres

My question is when is the postgres user and password being set? If I run this docker-compose everything works, meaning the web app can pass credentials to the postgress database and establish a connection. I'm not able to follow however, where those credentials are being set in the first place.

I was assuming in the base postgres Dockerfile, there would be some instruction to set the database name, username and password. I do not it though. Here is a copy of the base postgres Dockerfile below.

# vim:set ft=dockerfile:

FROM debian:jessie



# explicitly set user/group IDs

RUN groupadd -r postgres --gid=999 && useradd -r -g postgres --uid=999 postgres



# grab gosu for easy step-down from root

ENV GOSU_VERSION 1.7

RUN set -x \

    && apt-get update && apt-get install -y --no-install-recommends ca-certificates wget && rm -rf /var/lib/apt/lists/* \

    && wget -O /usr/local/bin/gosu "https://github.com/tianon/gosu/releases/download/$GOSU_VERSION/gosu-$(dpkg --print-architecture)" \

    && wget -O /usr/local/bin/gosu.asc "https://github.com/tianon/gosu/releases/download/$GOSU_VERSION/gosu-$(dpkg --print-architecture).asc" \

    && export GNUPGHOME="$(mktemp -d)" \

    && gpg --keyserver ha.pool.sks-keyservers.net --recv-keys B42F6819007F00F88E364FD4036A9C25BF357DD4 \

    && gpg --batch --verify /usr/local/bin/gosu.asc /usr/local/bin/gosu \

    && rm -r "$GNUPGHOME" /usr/local/bin/gosu.asc \

    && chmod +x /usr/local/bin/gosu \

    && gosu nobody true \

    && apt-get purge -y --auto-remove ca-certificates wget



# make the "en_US.UTF-8" locale so postgres will be utf-8 enabled by default

RUN apt-get update && apt-get install -y locales && rm -rf /var/lib/apt/lists/* \

    && localedef -i en_US -c -f UTF-8 -A /usr/share/locale/locale.alias en_US.UTF-8

ENV LANG en_US.utf8



RUN mkdir /docker-entrypoint-initdb.d



RUN apt-key adv --keyserver ha.pool.sks-keyservers.net --recv-keys B97B0AFCAA1A47F044F244A07FCC7D46ACCC4CF8



ENV PG_MAJOR 9.6

ENV PG_VERSION 9.6.1-1.pgdg80+1



RUN echo 'deb http://apt.postgresql.org/pub/repos/apt/ jessie-pgdg main' $PG_MAJOR > /etc/apt/sources.list.d/pgdg.list



RUN apt-get update \

    && apt-get install -y postgresql-common \

    && sed -ri 's/#(create_main_cluster) .*$/\1 = false/' /etc/postgresql-common/createcluster.conf \

    && apt-get install -y \

        postgresql-$PG_MAJOR=$PG_VERSION \

        postgresql-contrib-$PG_MAJOR=$PG_VERSION \

    && rm -rf /var/lib/apt/lists/*



# make the sample config easier to munge (and "correct by default")

RUN mv -v /usr/share/postgresql/$PG_MAJOR/postgresql.conf.sample /usr/share/postgresql/ \

    && ln -sv ../postgresql.conf.sample /usr/share/postgresql/$PG_MAJOR/ \

    && sed -ri "s!^#?(listen_addresses)\s*=\s*\S+.*!\1 = '*'!" /usr/share/postgresql/postgresql.conf.sample



RUN mkdir -p /var/run/postgresql && chown -R postgres /var/run/postgresql



ENV PATH /usr/lib/postgresql/$PG_MAJOR/bin:$PATH

ENV PGDATA /var/lib/postgresql/data

VOLUME /var/lib/postgresql/data



COPY docker-entrypoint.sh /



ENTRYPOINT ["/docker-entrypoint.sh"]



EXPOSE 5432

CMD ["postgres"]
like image 946
david Avatar asked Nov 14 '16 22:11

david


People also ask

Where is password postgres?

PostgreSQL database passwords are separate from operating system user passwords. The password for each database user is stored in the pg_authid system catalog. Passwords can be managed with the SQL commands CREATE ROLE and ALTER ROLE, e.g., CREATE ROLE foo WITH LOGIN PASSWORD 'secret' , or the psql command \password .

Where is PostgreSQL conf Docker?

The default postgresql. conf file lives within the PGDATA dir ( /var/lib/postgresql/data ), which makes things more complicated especially when running the Postgres container for the first time, since the docker-entrypoint.sh wrapper invokes the initdb step for PGDATA dir initialization.


1 Answers

Not sure which postgres image are you using.

If you look at the official postgres image for complete information. It allows user to specify the environment variables for the below ones and these can be easily overridden at run time.

  • POSTGRES_PASSWORD
  • POSTGRES_USER
  • PGDATA
  • POSTGRES_DB
  • POSTGRES_INITDB_ARGS

Environment variables can be overridden using below three methods depending on your case.

  • Run Image: If running docker image directly, use below to include environment variable in docker run with -e K=V. Please refer documentation for more details here
docker run -e POSTGRES_PASSWORD=secrect -e POSTGRES_USER=postgres <other options> image/name
  • Dockerfile: If you need to specify the environment variable in Dockerfile, specify as mentioned below. Please refer documentation for more details here
ENV POSTGRES_PASSWORD=secrect
ENV POSTGRES_USER=postgres
  • docker-compose: If you need to specify the environment variable in docker-compose.yml, specify as below. Please refer documentation for more details here
web:
  environment:
    - POSTGRES_PASSWORD=secrect
    - POSTGRES_USER=postgres

Hope this is useful.

like image 107
Rao Avatar answered Sep 21 '22 14:09

Rao