Docker swarm doesn’t build from Dockerfile correctly

Im using docker swarm with Docker version 19.03.3 engine.

I have a problem with psql build, it seems like Dockerfile does run correctly and i do push the new service to registry, but when container is up i discover that all COPY files are missing and psql extension “Postgis” that i install on Dockerfile is not installed.

I dont see any errors and that is why i dont know what is the issue.

Dockerfile:

FROM postgres
RUN apt-get update -y
RUN apt-get install -y postgis
COPY cluster/ /docker-entrypoint-initdb.d/

swarm yml file:

        db:
                build: docker-compose.d/postgres
                image: 127.0.0.1:5000/postgres
                restart: 'always'
                environment:
                        - PG_MAX_WAL_SENDERS=8
                        - PG_WAL_KEEP_SEGMENTS=8
                        - PGDATA=/var/lib/postgresql/data/pgdata
                        - POSTGRES_USER=USER
                        - POSTGRES_PASSWORD=password
                volumes:
                        - /db_data:/var/lib/postgresql/data'
                deploy:
                 placement:
                   constraints:
                       - node.role==manager

steps:

docker service create --name=registry --publish published=5000,target=5000 registry
qhetvgzqj0f8g1t4nxxx76cqz
overall progress: 1 out of 1 tasks
1/1: running
verify: Service converged


docker-compose build --no-cache db #if i dont use --no-cache it doesnt rebuild service

Successfully built f6413978c21c
Successfully tagged 127.0.0.1:5000/postgres:latest

docker-compose push db
Pushing db (127.0.0.1:5000/postgres:latest)...
The push refers to repository [127.0.0.1:5000/postgres]
latest: digest: sha256:dccccdbdfxxxxxxxcb1e5b3a8a size: 3877

docker stack deploy base --compose-file docker-compose.swarm.yml

I see service is build :

docker service ls
lobelr1vzj8l        base_db                replicated          1/1                 127.0.0.1:5000/postgres:latest

container logs :

docker service ps --no-trunc base_db

ltujcwnx9m2v61hjuqtph6d7s   base_db.1           127.0.0.1:5000/postgres:[email protected]:dccccdbdfafeb505b7ab349b21f4a580857c3cd51965cd36f81269cb1e5b3a8a   swarm-manager       Ready               Ready 4 seconds ago


docker service logs base_db
    psql:/docker-entrypoint-initdb.d/postgres.sql:1: ERROR:  could not open extension control file "/usr/share/postgresql/12/extension/postgis.control": No such file or directory

Problem is The container doesn’t really build with this command in Dockerfile
“RUN apt-get install -y postgis”

If i go to the container and run commands manually it works and also shows like extension was never installed before.

Why is that?
Thanks.

Source: StackOverflow