getting Detected task failure for some images while creating docker service

11.7k Views Asked by At

I am trying to create docker swarm service, but I am getting some strange behavior:

For some images, it is successful and for some, I am getting an error:

docker service create nginx 89t21k3udf007pvl2ucvmdp9l overall progress: 1 out of 1 tasks 1/1: running [==================================================>] verify: Service converged --> Success

docker service create hello-world 8hhdki32ypfwshh2wvijkkmvb overall progress: 0 out of 1 tasks 1/1: preparing [=================================> ] verify: Detected task failure --> Fail

For your reference: docker pull hello-world is working fine.

I was under impression that we can create services on all the images, is there any terms and conditions for that? Am I missing something?

Note: This is the basic part, but my main intention is to create service for local registry image. But upon debugging found issue in very first step i.e. hello-world.

3

There are 3 best solutions below

6
On BEST ANSWER

It is failing because the container created from the hello-world Docker image just displays a message and then exits, and that happens quickly.

And since you have created a swarm service, it will keep recreating the container everytime it fails, which is expected and that's why the service seems to be failing.

Please see below the service logs, and you will see that the container keeps being recreated:

[ ~]$ docker service create --name helloworld hello-world iilsd1yc706zgcdg35l8sdz3z
Since --detach=false was not specified, tasks will be created in the background.
In a future release, --detach=false will become the default.

[ ~]$ docker service logs helloworld  -f
helloworld.1.jxxnqzximfy7@    |
helloworld.1.zwyhf6x4cpdm@    |
helloworld.1.jxxnqzximfy7@    | Hello from Docker!
helloworld.1.zwyhf6x4cpdm@    | Hello from Docker!
helloworld.1.jxxnqzximfy7@    | This message shows that your installation appears to be working correctly.
helloworld.1.zwyhf6x4cpdm@    | This message shows that your installation appears to be working correctly.
helloworld.1.zwyhf6x4cpdm@   |
helloworld.1.jxxnqzximfy7@    |
helloworld.1.jxxnqzximfy7@    | To generate this message, Docker took the following steps:
helloworld.1.zwyhf6x4cpdm@    | To generate this message, Docker took the following steps:
helloworld.1.zwyhf6x4cpdm@    |  1. The Docker client contacted the Docker daemon.
helloworld.1.jxxnqzximfy7@    |  1. The Docker client contacted the Docker daemon.
helloworld.1.jxxnqzximfy7@    |  2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
helloworld.1.zwyhf6x4cpdm@    |  2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
helloworld.1.zwyhf6x4cpdm@    |     (amd64)
helloworld.1.jxxnqzximfy7@    |     (amd64)
helloworld.1.zwyhf6x4cpdm@   |  3. The Docker daemon created a new container from that image which runs the
helloworld.1.jxxnqzximfy7@    |  3. The Docker daemon created a new container from that image which runs the
helloworld.1.jxxnqzximfy7@    |     executable that produces the output you are currently reading.
helloworld.1.zwyhf6x4cpdm@    |     executable that produces the output you are currently reading.
helloworld.1.zwyhf6x4cpdm@    |  4. The Docker daemon streamed that output to the Docker client, which sent it
helloworld.1.jxxnqzximfy7@    |  4. The Docker daemon streamed that output to the Docker client, which sent it
helloworld.1.jxxnqzximfy7@    |     to your terminal.
helloworld.1.zwyhf6x4cpdm@    |     to your terminal.
helloworld.1.jxxnqzximfy7@    |
helloworld.1.jxxnqzximfy7@    | To try something more ambitious, you can run an Ubuntu container with:
helloworld.1.zwyhf6x4cpdm@    |
helloworld.1.zwyhf6x4cpdm@    | To try something more ambitious, you can run an Ubuntu container with:
helloworld.1.jxxnqzximfy7@    |  $ docker run -it ubuntu bash
helloworld.1.jxxnqzximfy7@    |
helloworld.1.zwyhf6x4cpdm@    |  $ docker run -it ubuntu bash
helloworld.1.jxxnqzximfy7@    | Share images, automate workflows, and more with a free Docker ID:
helloworld.1.jxxnqzximfy7@    |  https://hub.docker.com/
helloworld.1.zwyhf6x4cpdm@    |
helloworld.1.zwyhf6x4cpdm@    | Share images, automate workflows, and more with a free Docker ID:
helloworld.1.jxxnqzximfy7@    |
helloworld.1.zwyhf6x4cpdm@    |  https://hub.docker.com/
helloworld.1.zwyhf6x4cpdm@    |
helloworld.1.jxxnqzximfy7@    | For more examples and ideas, visit:
helloworld.1.jxxnqzximfy7@    |  https://docs.docker.com/get-started/
helloworld.1.zwyhf6x4cpdm@    | For more examples and ideas, visit:
helloworld.1.jxxnqzximfy7@    |
helloworld.1.zwyhf6x4cpdm@    |  https://docs.docker.com/get-started/
helloworld.1.zwyhf6x4cpdm@    |
helloworld.1.yw17ktyymrp6@    |
helloworld.1.yw17ktyymrp6@    | Hello from Docker!
helloworld.1.yw17ktyymrp6@    | This message shows that your installation appears to be working correctly.

You should try with a different Docker image, busybox for example:

[~]$ docker service create --name helloworld busybox:latest sh -c "while true; do echo Hello; sleep 2; done"
yjxzteshp7k2xf4aznj4l86s6
Since --detach=false was not specified, tasks will be created in the background.
In a future release, --detach=false will become the default.
[~]$ docker service ps helloworld --no-trunc
ID                          NAME                IMAGE                                                                                    NODE                              DESIRED STATE       CURRENT STATE          ERROR               PORTS
33xe78ekfjkmti8yahrcp5gug   helloworld.1        busybox:latest@sha256:2a03a6059f21e150ae84b0973863609494aad70f0a80eaeb64bddd8d92465812      Running             Running 1 second ago
[~]$ docker service logs -f helloworld
helloworld.1.33xe78ekfjkm@    | Hello
helloworld.1.33xe78ekfjkm@    | Hello
helloworld.1.33xe78ekfjkm@    | Hello
helloworld.1.33xe78ekfjkm@    | Hello
helloworld.1.33xe78ekfjkm@    | Hello
^C
0
On

check docker logs may be some error or issue you will able to find. like -->

No such file or directory

1
On

There's a workaround by creating the service with Docker Compose. Create a docker-compose.yml with the following code:

services:
    hello-world:
        image: hello-world
        deploy:
            replicas: 1

$ docker compose up -d

Optionally, we can add extras such as ports, volumes, ports, etc., here's an example:

services:
    hello-world:
        image: hello-world
        deploy:
            replicas: 1
        ports:
            - 8080:8080
        volumes:
            - "/tmp/hello.yml:/etc/hello/hello.yml"
        networks:
            - app-network
networks:
    app-network:

And the service should start appropriately in its own container. If we have Docker Swarm on that machine with the manager only, it will not be added as a service, that might be confusing as the docker-compose.yml starts with services. Once a worker joins the swarm, the container will be distributed to it as a service. Even more, if that worker goes down, the service will be re-distributed to the other members of the Docker Swarm.

NOTE: Be consistent with the indentation and don't use Tabs, as it will make the .yml file invalid