0

I'm a bit new to Docker.
I have two containers running using docker-compose.
One is the API and the other is the actual application.
I want to add a new DB container using the Postgres official image.
It's a bit hard to find a simple tutorial on how to create the container and populate it with a predefined sql file (of schemas and data).

When I start with "CMD /etc/init.d/postgresql start" in the Dockerfile I get an error saying: "No PostgreSQL clusters exist; see "man pg_createcluster" ... (warning)."

Since it takes me too much time to get things going I was wondering if it might be better to get an Ubuntu image and install Postgres on my own since there is only one source on how to use the image - docker hub, and I don't seem to understand it that well.

Any ideas or simple steps on how to compose and 'configure' this image?

2 Answers 2

1

If you want populate your database with some file, A simply way to do this is:

How to extend this image

If you would like to do additional initialization in an image derived from this one, add one or more *.sql, *.sql.gz, or *.sh scripts under /docker-entrypoint-initdb.d (creating the directory if necessary). After the entrypoint calls initdb to create the default postgres user and database, it will run any *.sql files and source any *.sh scripts found in that directory to do further initialization before starting the service.

Dockerfile

FROM postgres:alpine
COPY init.sql /docker-entrypoint-initdb.d/init.sql

docker-compose.yml

version: '3'
services:
  app:
  //your app definition
  postgres:
    build: .
Sign up to request clarification or add additional context in comments.

4 Comments

Tnx, and how do I run the postgres server inside the container?
The command from oficial image run the process by defualt, you dont need override it.
Indeed... My fault was the fact that I have overwritten the initial script in my yml file, and that caused the original postgres image script to be ignored and the psql server didn't start. Thanks for taking the time to answer my question @German
When the CMD /etc/init.d/postgresql start command is executed the docker process can not be attached to the postgres process. For the container to work the command must be CMD [postgres]
0

Pull the postgres image

docker pull postges:14.2

Create the service with the below command

docker service create --name postgres --network my_overlay --env "POSTGRES_PASSWORD=password" --publish 5432:5432 postgres:14.2

Try to connect using userName as postgres and password as password to the default postgres db.

jdbc:postgresql://127.0.0.1:5432/postgres // JDBC connection

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.