Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Doesn't work in swarm on remote containers #15

Open
notfol opened this issue Apr 18, 2018 · 2 comments
Open

Doesn't work in swarm on remote containers #15

notfol opened this issue Apr 18, 2018 · 2 comments

Comments

@notfol
Copy link

notfol commented Apr 18, 2018

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
Copy link
Author

notfol commented Apr 18, 2018

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

@willfarrell
Copy link
Owner

Looking forward to it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants