How do you automate the updating of images on Docker Swarm?
docker build -t $APPNAME:$timestamp .
OUTPUT="$(docker service inspect -f '{{.Spec.Name}}' sv-$APPNAME || true)"
if [ "$OUTPUT" == "" ]; then
# Service does not exist
echo "Creating service"
docker service create --update-delay 5s \
--publish $PUBPORT:8080 --replicas 2 \
--mount type=bind,source=$(cd ../images; pwd),target=/images,readonly=true \
--mount type=bind,source=$(cd ../processed_images; pwd),target=/app/images \
--mount type=bind,source=$(cd ..; pwd)/config.json,target=/config.json,readonly=true \
--mount type=bind,source=$(cd ~/.aws; pwd),target=/root/.aws,readonly=true \
--name sv-$APPNAME $APPNAME:$timestamp
else
# Service exists
echo "Updating service"
docker service update --image $APPNAME:$timestamp sv-$APPNAME
I haven't looked in a while, but it would be nice if they had some kind of "upstall" (update/install) kinda like upsert (update/insert) in CRUD land.docker service update --image <some-image:version> --with-registry-auth <some-service>
Simplest example would be a job definition like this: https://pastebin.com/KsG0cDzR
This job would have to be preceded by building the new docker image you are going to update to of course.
docker service update --image $image_name:$new_sha $service_name
and Swarm handles the rest. You can even pass in rolling update options, so the container updates are staggered. Put this into a simple bash script that only runs when the current branch is master. (Because your CI is building every branch to give you fast feedback, right?) In the case of CircleCI or TravisCI, that can be done with something like: if [ "${CIRCLE_BRANCH}" == "master" ]; then