Search code examples
dockerdocker-swarmdocker-registrydocker-swarm-mode

Docker swarm same image different build


Im using Docker version 19.03.3 with docker swarm, and docker registry. I want to know how can i use the same image but with a different build.

my swarm.yml :

version: '3'
services:

        db:
                image: 127.0.0.1:5000/postgres:11.5
                build: docker-compose.d/postgres
                environment:
                        - PG_MAX_WAL_SENDERS=8
                        - PG_WAL_KEEP_SEGMENTS=8
                        - PGDATA=/var/lib/postgresql/data/pgdata
                        - POSTGRES_USER=user
                        - POSTGRES_PASSWORD=password

                deploy:
                 placement:
                   constraints:
                       - node.role == manager
        db-slave:
                build: docker-compose.d/postgres/slave
                image: 127.0.0.1:5000/postgres:11.5
                environment:
                        - PGDATA=/var/lib/postgresql/data/pgdata
                        - REPLICATE_FROM=db
                        - POSTGRES_USER=user
                        - POSTGRES_PASSWORD=password

                deploy:
                 placement:
                   constraints:
                       - node.role == manager
                depends_on:
                        - db

docker images :

127.0.0.1:5000/postgres   11.5                839a428f8eac        4 days ago          848MB
127.0.0.1:5000/postgres   <none>              e5636a8fc5f0        4 days ago          848MB
127.0.0.1:5000/postgres   <none>              6c1932b5707c        4 days ago          848MB
postgres                  11.5                5f1485c70c9a        5 days ago          293MB
registry                  <none>              f32a97de94e1        7 months ago        25.8MB

docker file db:

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

docker file db-slave:

FROM postgres:11.5
RUN apt-get update -y
RUN apt-get install postgis iputils-ping -y
COPY setup-replication.sh /docker-entrypoint-initdb.d/
COPY docker-entrypoint.sh /docker-entrypoint.sh
COPY postgres.sql /docker-entrypoint-initdb.d/
RUN chmod +x /docker-entrypoint-initdb.d/setup-replication.sh /docker-entrypoint.sh

They both image postgres:11.5 from registry and this makes an issue because i do want to use postgres 11.5 on both, but to use a different build as i need to preform different build process on them both. one is copying A file and the other is not.(for example) how can i do this ?

registry save both builds as one postgres:11.5


Solution

  • Both have the same name because you are using the same image: 127.0.0.1:5000/postgres:11.5.

    image in this context is the name of the resulting image, not the name of the base image.

    So, basically change them to something like:

            db:
                    image: 127.0.0.1:5000/db          <--------
                    build: docker-compose.d/postgres
            db-slave:
                    build: docker-compose.d/postgres/slave
                    image: 127.0.0.1:5000/db-slave <--------
    

    Also notice this works for docker-compose. Docker swarm cannot build containers, only deploy already prebuilt images.