user1581721
user1581721

Reputation: 307

Docker :- Creating Databases in Postgresql Docker Container

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:

Upvotes: 2

Views: 1114

Answers (1)

Pratik
Pratik

Reputation: 1216

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

Upvotes: 1

Related Questions