Can't run PostgreSQL from Dockerfile - why?


I’m trying to run PostgreSQL from Dockerfile while debugging project in MS Visual Studio. Here’s a part of code in Dockerfile:

RUN apt-get update && \
apt-get install -y postgresql postgresql-contrib && \
service postgresql start && \
su — postgres -c "psql -c \"CREATE USER upostgres WITH PASSWORD 'docker';\"" && \
su — postgres -c "createdb -O upostgres globalblue-demo-db"
CMD service postgresql start

If to look at VS output, it demonstrates PG running at least once:

1> #8 123.1 Processing triggers for libc-bin (2.31-13+deb11u5) ...
1> #8 124.3 Starting PostgreSQL 13 database server: main.
1> #8 130.4 CREATE ROLE
1> #8 DONE 135.5s
1> #9 exporting to image
1> #9 sha256:e8c613e07b0b7ff33893b694f7759a10d42e180f2b4dc349fb57dc6b71dcab00
1> #9 exporting layers
1> #9 exporting layers 17.1s done
1> #9 writing image sha256:da2311d667e1445cdd8683e4facfd5fa13a38e323fbb5eb4f58c8572ae574de9 0.1s done
1> #9 naming to 0.0s done
1> #9 DONE 17.5s

But from cmd terminal in the same container I receive the following PostgreSQL status:

service postgresql status
# 13/main (port 5432): down

And another application deployed in the same container also reports that PG is down. How should I start PG from my Dockerfile?

You might want to take a look at how the official postgres docker image is built: GitHub - docker-library/postgres: Docker Official Image packaging for Postgres. It should give you an idea of what it needs to archive what you want…

But on the other hand: what’s wrong with using the official image?

This line won’t do what you expect it to do, as systemd is not available inside containers and therefor can’t start any services.