Docker Community Forums

Share and learn in the Docker community.

Docker swarm use multiple container instances from the same image

swarm
docker

(Jbacardit) #1

I would like to know if is it possible to run multiple containers of the same images using different parameters in docker swarm.

In the following example, I have two services ( service_2 and service_3 ) using the same image ( hub/image_2 ). As you can see, I set two different environment variables for each service. When I mount the services ( docker stack deploy --compose-file docker-compose.yml test ) I can see that each service is using the same environment variable (it have value 2 in this case).

version: '3'

services:
  service_1:
    image: hub/image_1
    environment:
      - my_env = 1
    deploy:
      placement:
        constraints:
          - node.host == host_1
    volumes:
      - test:C:/test
  service_2:
    image: hub/image_2
    environment:
      - my_env = 2
    deploy:
      placement:
        constraints:
          - node.host == host_1
    volumes:
      - test:C:/test
  service_3:
    image: hub/image_2
    environment:
      - my_env = 4
    deploy:
      placement:
        constraints:
          - node.host == host_1
    volumes:
      - test:C:/test

volumes:
  test:
    external:
      name: test 

The Dockerfile is as follows:

FROM microsoft/dotnet-framework:3.5-windowsservercore

ARG ENTRY
ENV my_env=$ENTRY

WORKDIR C:\\test

ENTRYPOINT C:/test/app.exe %my_env%

This is the service information ( docker service inspect servicename )

Service_2:

[
    {
        "ID": "uq71dl6ppas4bhbxqj84oxbw2",
        "Version": {
            "Index": 2430
        },
        "CreatedAt": "2018-08-27T10:33:15.24867595Z",
        "UpdatedAt": "2018-08-27T10:33:15.249951349Z",
        "Spec": {
            "Name": "test_service_2",
            "Labels": {
                "com.docker.stack.image": "hub/image_2",
                "com.docker.stack.namespace": "test"
            },
            "TaskTemplate": {
                "ContainerSpec": {
                    "Image": "hub/image_2:latest@sha256:8577cb813fb26aa061e23b5aaec3c9c5089e91edbc52735d61247cf535899527",
                    "Labels": {
                        "com.docker.stack.namespace": "test"
                    },
                    "Env": [
                        "my_env = 2"
                    ],
                    "Privileges": {
                        "CredentialSpec": null,
                        "SELinuxContext": null
                    },
                    "Mounts": [
                        {
                            "Type": "volume",
                            "Source": "test",
                            "Target": "C:/test",
                            "VolumeOptions": {}
                        }
                    ],
                    "StopGracePeriod": 10000000000,
                    "DNSConfig": {},
                    "Isolation": "default"
                },
                "Resources": {},
                "RestartPolicy": {
                    "Condition": "any",
                    "Delay": 5000000000,
                    "MaxAttempts": 0
                },
                "Placement": {
                    "Constraints": [
                        "node.id == oi3qw4wi00j762c1xixnhc412"
                    ],
                    "Platforms": [
                        {
                            "Architecture": "amd64",
                            "OS": "windows"
                        }
                    ]
                },
                "Networks": [
                    {
                        "Target": "9hd78dz1r8u25woejiwu1l9ei",
                        "Aliases": [
                            "service_2"
                        ]
                    }
                ],
                "ForceUpdate": 0,
                "Runtime": "container"
            },
            "Mode": {
                "Replicated": {
                    "Replicas": 1
                }
            },
            "UpdateConfig": {
                "Parallelism": 1,
                "FailureAction": "pause",
                "Monitor": 5000000000,
                "MaxFailureRatio": 0,
                "Order": "stop-first"
            },
            "RollbackConfig": {
                "Parallelism": 1,
                "FailureAction": "pause",
                "Monitor": 5000000000,
                "MaxFailureRatio": 0,
                "Order": "stop-first"
            },
            "EndpointSpec": {
                "Mode": "vip"
            }
        },
        "Endpoint": {
            "Spec": {
                "Mode": "vip"
            },
            "VirtualIPs": [
                {
                    "NetworkID": "9hd78dz1r8u25woejiwu1l9ei",
                    "Addr": "10.0.1.88/24"
                }
            ]
        }
    }
]

Service_3:

[
    {
        "ID": "728jj8wqavett7ehejafdxa1p",
        "Version": {
            "Index": 2414
        },
        "CreatedAt": "2018-08-27T10:33:07.219679442Z",
        "UpdatedAt": "2018-08-27T10:33:07.220656862Z",
        "Spec": {
            "Name": "test_service_3",
            "Labels": {
                "com.docker.stack.image": "hub/image_2",
                "com.docker.stack.namespace": "test"
            },
            "TaskTemplate": {
                "ContainerSpec": {
                    "Image": "hub/image_2:latest@sha256:8577cb813fb26aa061e23b5aaec3c9c5089e91edbc52735d61247cf535899527",
                    "Labels": {
                        "com.docker.stack.namespace": "test"
                    },
                    "Env": [
                        "my_env = 4"
                    ],
                    "Privileges": {
                        "CredentialSpec": null,
                        "SELinuxContext": null
                    },
                    "Mounts": [
                        {
                            "Type": "volume",
                            "Source": "test",
                            "Target": "C:/test",
                            "VolumeOptions": {}
                        }
                    ],
                    "StopGracePeriod": 10000000000,
                    "DNSConfig": {},
                    "Isolation": "default"
                },
                "Resources": {},
                "RestartPolicy": {
                    "Condition": "any",
                    "Delay": 5000000000,
                    "MaxAttempts": 0
                },
                "Placement": {
                    "Constraints": [
                        "node.id == oi3qw4wi00j762c1xixnhc412"
                    ],
                    "Platforms": [
                        {
                            "Architecture": "amd64",
                            "OS": "windows"
                        }
                    ]
                },
                "Networks": [
                    {
                        "Target": "9hd78dz1r8u25woejiwu1l9ei",
                        "Aliases": [
                            "service_3"
                        ]
                    }
                ],
                "ForceUpdate": 0,
                "Runtime": "container"
            },
            "Mode": {
                "Replicated": {
                    "Replicas": 1
                }
            },
            "UpdateConfig": {
                "Parallelism": 1,
                "FailureAction": "pause",
                "Monitor": 5000000000,
                "MaxFailureRatio": 0,
                "Order": "stop-first"
            },
            "RollbackConfig": {
                "Parallelism": 1,
                "FailureAction": "pause",
                "Monitor": 5000000000,
                "MaxFailureRatio": 0,
                "Order": "stop-first"
            },
            "EndpointSpec": {
                "Mode": "vip"
            }
        },
        "Endpoint": {
            "Spec": {
                "Mode": "vip"
            },
            "VirtualIPs": [
                {
                    "NetworkID": "9hd78dz1r8u25woejiwu1l9ei",
                    "Addr": "10.0.1.84/24"
                }
            ]
        }
    }
]

How could I avoid to use different images?