willfarrell / docker-crontab

A docker job scheduler (aka. crontab for docker)
MIT License
300 stars 65 forks source link

Doesn't work in swarm on remote containers #15

Open notfol opened 6 years ago

notfol commented 6 years ago

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 commented 6 years ago

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

willfarrell commented 6 years ago

Looking forward to it.