I want to create a Dockerfile that runs a PostgreSQL database when I run that container.
So what I did was making a Dockerfile:
FROM postgres:latest
COPY *.sh /docker-entrypoint-initdb.d/
The Dockerfile copies the initialization script "postgres.sh" to /docker-entrypoint-initdb.d/. The postgres.sh script contains the following:
#!/bin/sh
su postgres -c "psql << EOF
ALTER USER postgres WITH PASSWORD '123';
create database shippingchallenge;
\c shippingchallenge;
create table people(firstname CHAR(20), surname CHAR(30));
insert into people values ('Pieter', 'Pauwels');
EOF"
So what I want to do is create a database "shippingchallenge" with a table "people" with 2 values: "firstname " and "surname".
This works fine if I would use sudo docker exec -it <container> /bin/bash and I paste this.
But when I use the method described above and I run the image created from this sudo docker run -p 5432:5432 -e POSTGRES_PASSWORD=123 <image> I get the following error:
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.
The database cluster will be initialized with locale "en_US.utf8".
The default database encoding has accordingly been set to "UTF18".
The default text search configuration will be set to "english".
Data page checksums are disabled.
fixing permissions on existing directory /var/lib/postgresql/data ... ok
creating subdirectories ... ok
selecting dynamic shared memory implementation ... posix
selecting default maxconnections ... 100
selecting default shared_buffers ... 128MB
selecting default time zone ... Etc/UTC
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok
initdb: warning: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the option -A or
--auth-local and --auth-host, the next time you run initdb.
Success. You can now start the database server using:
pg_ctl -D /var/lib/postgresql/data -l logfile start
waiting for server to start....2020-12-19 12:42:09.749 UTC [45] LOG: starting PostgreSQL 13.1 (Debian 13.1-1 pgdg100+1) on x86_64 pc linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
2020-12-19 12:42:09.754 UTC [45] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2020-12-19 12:42:09.771 UTC [46] LOG: database system was shut down at 2020-12-19 12:42:07 UTC
2020-12-19 12:42:09.778 UTC [45] LOG: database system is ready to accept connections
done
server started
/usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/postgres.sh
Password: su: Authentication failure
Content of /etc/pam.d/su:
auth sufficient pam_rootok.so
session required pam_env.so readenv=1
session required pam_env.so readenv=1 envfile=/etc/default/locale
session optional pam_mail.so nopen
session required pam_limits.so
@include common-auth
@include common-account
@include common-session
Thanks in advance for the help!
sudo docker run -p 5432:5432 -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=123 <image>?sudo docker run -p 5432:5432 <image>and see if it still fails, sounds you have wrong password,123should be password to db user-e POSTGRES_PASSWORD=123option? Then I get the following:[sudo] password for pieter: Error: Database is uninitialized and superuser password is not specified. You must specify POSTGRES_PASSWORD to a non-empty value for the superuser. For example, "-e POSTGRES_PASSWORD=password" on "docker run". You may also use "POSTGRES_HOST_AUTH_METHOD=trust" to allow all connections without a password. This is *not* recommended. See PostgreSQL documentation about "trust": https://www.postgresql.org/docs/current/auth-trust.html