Namespacing_container_stacks
Here’s a quick hack I came up with while experimenting with git worktrees and multiple coding agents and dealing with pod naming collisions when bringing up multiple copies of my local dev stack.
In the vars section of Taskfile.yml:
PROJECT_NAME:
sh: |
if [ -f ".git" ]; then
# This is a worktree - use directory name
echo "$(basename $(pwd))"
else
# This is the main repo
echo "main"
fi
This captues the current worktree directory name and stores it in a variable. We’ll use this as a pod name prefix when starting the stack.
The task I have defined to bring up my local dev stack:
dev-up:
desc: Bring up local dev stack
silent: true
deps:
- ghcr-login
cmds:
- podman compose -p {{ .PROJECT_NAME }} up --detach
This uses the PROJECT_NAME variable defined above as the project name podman compose will use to start the set of containers. Now each one will have names like my-first-worktree_postgres_1 avoiding name collisions. Of course this doesn’t do anything about host port mapping conflicts but in cases where your stack only exposes one or two services on the host it helps.