2

I have used the following commands and have created a docker container in Ubuntu os for postgresql. IS there anyway i can specify the postgres db password in the Run command and execute the sql commands from the sql scripts inside a docker container ?

Currently I access the container using following command and execute sql commands manually .

sudo docker run --rm -t -i --link pg_docker:pg postgresql_image bash
sql -h $PG_PORT_5432_TCP_ADDR -p $PG_PORT_5432_TCP_PORT -d docker -U docker --password
        prompts for postgres Password:




        # example Dockerfile for http://docs.docker.com/examples/postgresql_service/
        #
        FROM ubuntu
        MAINTAINER vicky
        RUN apt-key adv --keyserver keyserver.ubuntu.com --recv-keys B97B0AFCAA1A47F044F244A07FCC7D46ACCC4CF8
        RUN echo "deb http://apt.postgresql.org/pub/repos/apt/ precise-pgdg main" > /etc/apt/sources.list.d/pgdg.list
        RUN apt-get update && apt-get install -y python-software-properties software-properties-common postgresql-9.3 postgresql-client-9.3 postgresql-contrib-9.3
        USER postgres
        RUN    /etc/init.d/postgresql start &&\
            psql --command "CREATE USER docker WITH SUPERUSER PASSWORD 'docker';" &&\
            createdb -O docker docker
        RUN echo "host all  all    0.0.0.0/0  md5" >> /etc/postgresql/9.3/main/pg_hba.conf
        RUN echo "listen_addresses='*'" >> /etc/postgresql/9.3/main/postgresql.conf
        EXPOSE 5432
        VOLUME  ["/etc/postgresql", "/var/log/postgresql", "/var/lib/postgresql"]
        CMD ["/usr/lib/postgresql/9.3/bin/postgres", "-D", "/var/lib/postgresql/9.3/main", "-c", "config_file=/etc/postgresql/9.3/main/postgresql.conf"]


        Commands to Create Docker Image & Container
        sudo docker build -t postgresql_image .
        sudo docker run --rm -P --name pg_docker postgresql_image &
        sudo docker run --rm -t -i --link pg_docker:pg postgresql_image bash
        sql -h $PG_PORT_5432_TCP_ADDR -p $PG_PORT_5432_TCP_PORT -d docker -U docker --password
        prompts for postgres Password:
1
  • Just fyi, many of postgres's commands accept a password via the PGPASSWORD environment variable which can be specified to avoid prompting... not sure if that applies to you though? Commented Dec 9, 2015 at 23:46

1 Answer 1

1

There is an official postgre image(https://registry.hub.docker.com/_/postgres/) in the dockerhub which provides

1)specify the postgres db password in the Run command

2)Execute sql commands while using run command.

Please let us know if it satisfied what you were looking for

1
  • Thanks for the link . Now I can specify the password on the Run Command but I also need to invoke few postgres scripts (psql) inside the docker . have to create few tables after creating the docker . Eg:- create table tbl_sample( id numeric, name character(1), ); Commented Mar 6, 2015 at 15:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.