I am learning to use docker, my goal is to create a postgres sql container in which i create a table, as of now i do not want to add any data just create the table. i was able to create a postgres sql instance but copy/add my sql script (which creates this table) to /docker-entrypoint-initdb.d/ does not seem to work. i can confirm this when i run the psql db i cannot see any table created.
My Dockerfile
"FROM ubuntu
RUN apt-get update && apt-get install -y gnupg
RUN apt-key adv --keyserver hkp://p80.pool.sks-keyservers.net:80 --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 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"]
copy /scripts/test.sql /docker-entrypoint-initdb.d/
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"]"
My sql file
CREATE TABLE email_confirmation_code (
id integer NOT NULL,
code character varying(255) NOT NULL,
user_id integer
);
testing
psql -h localhost -p 32772 -U docker --password
Password for user docker:
psql (10.6 (Ubuntu 10.6-1.pgdg16.04+1), server 9.3.17)
SSL connection (protocol: TLSv1.2, cipher: DHE-RSA-AES256-GCM-SHA384, bits: 256, compression: off)
Type "help" for help.
docker=# select * from email_confirmation_code
docker-# ;
ERROR: relation "email_confirmation_code" does not exist
LINE 1: select * from email_confirmation_code
as you can see the table does not exist. please point out what is the mistake here.
清理/var/lib/postgresql/data
卷中的文件/文件夹并创建一个容器。
That's a feature of the standard postgres image ; it's not something you get if you install PostgreSQL by hand in a plain Ubuntu container.
If you build a custom image for this, you can just start from that image and add your initialization file. This is a complete Dockerfile:
FROM postgres:9.6
COPY ./scripts/test.sql /docker-entrypoint-initdb.d/
I might not build a custom image for this. Instead, you can use the docker run -v
option or the Docker Compose volumes:
option to push the script into the container when it starts up (treating it as a configuration file).
(Also remember that these init scripts only run the very first time the database container starts up; if there is already database data they will not re-run.)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.