3

I am trying to deploy a stack of services in a swarm on a local machine for testing purpose and i want to build the docker image whenever i run or deploy a stack from the manager node.

Is it possible what I am trying to achieve..

3 Answers 3

6

On Docker Swarm you can't build an image specified in a Docker Compose file:

Note: This option is ignored when deploying a stack in swarm mode with a (version 3) Compose file. The docker stack command accepts only pre-built images. - from docker docs

You need to create the image with docker build (on the folder where the Dockerfile is located):

docker build -t imagename --no-cache .

After this command the image (named imagename) is now available on the local registry.

You can use this image on your Docker Compose file like the following:

version: '3'

services:
  example-service:
    image: imagename:latest
2

While updating a local image you will get an error as below

image IMAGENAME:latest could not be accessed on a registry to record
its digest. Each node will access IMAGENAME:latest independently,
possibly leading to different nodes running different
versions of the image.

To overcome this issue start the service forcefully as follows

docker service update --image IMAGENAME:latest  --force  Service Name

In the above example it is as

docker service update --image imagename:97bfeeb4b649 --force Service Name

1

You need to build the image with docker build. Docker swarm doesn't work with tags to identify images. Instead it remembers the image id (hash) of an image when executing stack deploy, because a tag might change later on but the hash never changes.

Therefore you should reference the hash of your image as shown by docker image ls so that docker swarm will not try to find your image on some registry.

version: '3'

services:
  example-service:
    image: imagename:97bfeeb4b649
2
  • Yeah that's what i did. But if my swarm is across the network it isn't possible for the image to be present in the worker node. That is my concern. So either i will have to build the image on every node before i deploy my swarm.
    – DevScript
    Mar 19, 2018 at 10:47
  • Either you can push your image to some registry (you should be able to use dockerhub for free I think) or you can simply use docker save and docker load (as decribed in stackoverflow.com/questions/23935141/…) and a usb stick/scp to copy the image to all workers and load it there
    – herm
    Mar 19, 2018 at 11:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.