Docker Community Forums

Share and learn in the Docker community.

Execute Command in Docker Swarm Container only one time

I use docker swarm and I use follow yml

version: "3.7"

services:
  testa:
    image: alpine:latest
    deploy:
      replicas: 1
    command: echo 'asd'
    networks:
      - traefik-public
      - traefik-backend

Now I use
docker stack deploy -c testa.yml testa

In the logs (docker service logs testa_testa) is now something like this:
… | asd
… | asd
… | asd
… | asd

It is possible to execute the command only one time?

Your task container completes the command and ends the container. The scheduler notices that your desired state is 1 replica and spawns a new task container in order to meet the desired state… So basicly the container does execute the command only one time in it’s lifetime.

Either you need a command that prevents that the container completes its task (with smth like tail -f /dev/null as last command if no blocking service should be run)… or even better make it a one-shot task that stays stopped when finished:

version: "3.7"

services:
  testa:
    image: alpine:latest
    deploy:
      replicas: 1
      restart_policy:
        condition: none
    command: echo 'asd'
    networks:
      - traefik-public
      - traefik-backend