How to make docker-compose build image once and repeatedly use it to run containers?

Viewed 8772

I am trying to run multiple containers built from the same image. My problem is when I define my docker-compose.yml this way

version: '3'
services:
    crossbar-target:
        container_name: "app-crossbar-target"
        build:
            context: ../../crossbar

    crossbar-source-domain1:
        container_name: "app-crossbar-source-domain1"
        build:
            context: ../../crossbar

    crossbar-source-domain2:
        container_name: "app-crossbar-source-domain2"
        build:
            context: ../../crossbar

I will get three containers as I want them, but I get three images as well.

In case I have hundreds of containers, I don't like the idea having hundreds of images as well. That makes my local image repository completely unusable and unreadable.

Thinking and searching for solution I tried define crossbar image by itself and then reuse it:

version: '3'
services:
    app-crossbar:
        build:
        context: ../../crossbar

    crossbar-target:
        container_name: "app-crossbar-target"
        image: project_name/app-crossbar

    crossbar-source-domain1:
        container_name: "app-crossbar-source-domain1"
        image: project_name/app-crossbar

    crossbar-source-domain2:
        container_name: "app-crossbar-source-domain2"
        image: project_name/app-crossbar

Now, I have only one image for all containers, but get an app-crossbar container as well. That doesn't suit my needs as well.

Is there any way to make docker-compose manage my images efficiently and make only those images I need and then use them to run containers in any amount needed? Or do I have to manage my images separately in some other routines? I like calling:

docker-compose build

to rebuild all needed images and I like the idea that all my docker logic is held in one place.

1 Answers
Related