This is in relation to Error While Attempting to docker-compose a Postgres Image.
In addition to the files described in the question above, init.sh looks like this:
#for line in $(cat .env); do
# export $line;
#done
#service postgresql-9.3 initdb
psql -d users -U even_financial -f schemas.sql
where schemas.sql looks like this:
CREATE TABLE accounts (
user_id serial PRIMARY KEY,
username VARCHAR ( 50 ) UNIQUE NOT NULL,
password VARCHAR ( 50 ) NOT NULL,
email VARCHAR ( 255 ) UNIQUE NOT NULL,
created_on TIMESTAMP NOT NULL,
last_login TIMESTAMP
);
If I execute init.sh from inside the docker container, postgres populates properly.
How do I do the same thing from my Dockerfile/docker-compose?
My docker-compose looks like this:
version: "3.9"
services:
postgres:
build:
docker/postgres
ports:
- "5432:5432"
env_file:
docker/postgres/.env
# command:
# - /bin/chmod +x /app/init.sh
# - . /app/init.sh
My docker/postgres/Dockerfile looks like this:
FROM postgres:14.5-alpine
WORKDIR /app
COPY schemas.sql .
COPY init.sh .
COPY .env .
#RUN . init.sh
Attempting to run init.sh from commands in docker-compose (as above) results in:
/usr/local/bin/docker-entrypoint.sh: line 341: /bin/chmod +x /app/init.sh: No such file or directory
Attempting to run init.sh from the Dockerfile itself via RUN results in:
=> ERROR [6/6] RUN . init.sh 0.2s
------
> [6/6] RUN . init.sh:
#0 0.144 psql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: No such file or directory
#0 0.144 Is the server running locally and accepting connections on that socket?
------
failed to solve: executor failed running [/bin/sh -c . init.sh]: exit code: 2
`docker-compose` process finished with exit code 17
command. You should be placing your scripts into/docker-entrypoint-initdb.das described in the "How to extend this image" section of the official image README. Second, you probably don't even needinit.sh; you could just drop the.sqlfile into that directory.