docker-crontab icon indicating copy to clipboard operation
docker-crontab copied to clipboard

Doesn't work in swarm on remote containers

Open notfol opened this issue 6 years ago • 2 comments

This doesn't seem to work when executing commands on swarm task containers running on a different docker host than docker-crontab.

below assumes the container is running on the same host

CONTAINERS=$(docker ps --format '{{.Names}}' | grep -E "^[project]_[alias].[0-9]+")
for CONTAINER_NAME in $CONTAINERS; do
    docker exec ${CONTAINER_NAME} [command]
done

You could do something like below to get the container name from the docker host running the swarm task.

# deploy {ca,cert,key}.pem as secrets
function set_docker_env {
        export DOCKER_CERT_PATH=/run/secrets
        export DOCKER_HOST=tcp://$1:2376
        export DOCKER_TLS_VERIFY=1
}

function unset_docker_env {
        unset DOCKER_CERT_PATH
        unset DOCKER_HOST
        unset DOCKER_TLS_VERIFY
}

function container_host_from_docker {
        docker service ps --format '{{.Node}}' --filter 'desired-state=running' $1 | head -n 1
}

function container_name_from_docker {
        set_docker_env $1
        docker ps --format '{{.Names}}' | grep -E "^$2.[0-9]+" | head -n 1
}

CONTAINER_HOST=$( container_host_from_docker [project]_[alias] )
if [ ! -z "${CONTAINER_HOST// }" ]
then
    CONTAINER_NAME=$( container_name_from_docker $CONTAINER_HOST [project]_[alias] )
    docker exec ${CONTAINER_NAME} [command]
fi
unset_docker_env

notfol avatar Apr 18 '18 08:04 notfol

Just noticed this is your to-do's :) I might submit a PR

notfol avatar Apr 18 '18 08:04 notfol

Looking forward to it.

willfarrell avatar May 08 '18 16:05 willfarrell