Skip to content

Commit

Permalink
[examples] add an example for pre and post run command.
Browse files Browse the repository at this point in the history
  • Loading branch information
shizunge committed Jan 19, 2024
1 parent 1f16f9a commit e205d06
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ docker service create \
shizunge/gantry
```

Or with docker compose, see the [example](examples/docker-compose.yml).
Or with docker compose, see the [example](examples/README.md).

You can also run *Gantry* as a script outside the container `source ./src/entrypoint.sh`. *Gantry* is written to work with `busybox ash` as well as `bash`.

Expand Down
10 changes: 10 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Examples

## [cronjob](./cronjob)

Run *gantry* at the specific time.

## [prune-and-watchtower](./prune-and-watchtower)

Run [`docker system prune`](https://docs.docker.com/engine/reference/commandline/system_prune/) and [*watchtower*](https://github.com/containrrr/watchtower) with *gantry*.

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ services:
- /var/run/docker.sock:/var/run/docker.sock
environment:
- "GANTRY_NODE_NAME={{.Node.Hostname}}"
- "GANTRY_SERVICES_SELF=${STACK}_gantry"
# The gantry service is able to find the name of itself service. Use GANTRY_SERVICES_SELF when you want to set a different value.
# - "GANTRY_SERVICES_SELF=${STACK}_gantry"
- "GANTRY_SLEEP_SECONDS=0"
deploy:
replicas: 0
Expand Down
8 changes: 8 additions & 0 deletions examples/prune-and-watchtower/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# prune and watchtower

This example run `docker system prune` and *watchtower* before updating docker swarm services.

* [`docker system prune`](https://docs.docker.com/engine/reference/commandline/system_prune/) removes all unused containers, networks and images.
* [*watchtower*](https://github.com/containrrr/watchtower) updates standalone docker containers.
* [*gantry*](https://github.com/shizunge/gantry) updates docker swarm services.

53 changes: 53 additions & 0 deletions examples/prune-and-watchtower/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
version: "3.8"

services:
gantry:
image: shizunge/gantry
volumes:
- /var/run/docker.sock:/var/run/docker.sock
environment:
- "GANTRY_NODE_NAME={{.Node.Hostname}}"
- "GANTRY_PRE_RUN_CMD=SERVICE_NAME=gantry-prune;
docker service remove $${SERVICE_NAME} 2>/dev/null;
docker service create --mode global-job --name $${SERVICE_NAME}
--mount type=bind,source=/var/run/docker.sock,destination=/var/run/docker.sock
--entrypoint docker
alpinelinux/docker-cli
system prune -f;
docker service logs $${SERVICE_NAME};
docker service remove $${SERVICE_NAME};
SERVICE_NAME=gantry-container;
docker service remove $${SERVICE_NAME} 2>/dev/null;
docker service create --mode global-job --name $${SERVICE_NAME}
--mount type=bind,source=/var/run/docker.sock,destination=/var/run/docker.sock
ghcr.io/containrrr/watchtower
--cleanup=true
--label-enable
--run-once=true
--stop-timeout=60s
--tlsverify=true;
docker service logs $${SERVICE_NAME};
docker service remove $${SERVICE_NAME};
"
- "GANTRY_POST_RUN_CMD=echo \"This is a post run command.\";"
- "GANTRY_SLEEP_SECONDS=0"
deploy:
replicas: 0
placement:
constraints:
- node.role==manager
restart_policy:
condition: none
labels:
- swarm.cronjob.enable=true
- swarm.cronjob.schedule=45 23 0 * * *
- swarm.cronjob.skip-running=true

cronjob:
image: crazymax/swarm-cronjob:latest
volumes:
- /var/run/docker.sock:/var/run/docker.sock
deploy:
placement:
constraints:
- node.role==manager

0 comments on commit e205d06

Please sign in to comment.