I've around two to six projects with docker on my computer, which I use in parallel some times and have to manually stop some containers (mariadb, mongodb, php, nginx, apache) manually every time I want to use another project. Is there a way to stop those containers by its port, so I could simply do docker stop -p 3306
and add it to my Makefile stop
section? And if not, what way should I go to optimize this workflow.
I'm working on a mac with macOS 10.14.x and Docker desktop v18.09.2.
Assuming you can run bash
one way to do it is to write a small script:
#!/usr/bin/env bash
for id in $(docker ps -q)
do
if [[ $(docker port "${id}") == *"${1}"* ]]; then
echo "stopping container ${id}"
docker stop "${id}"
fi
done
docker ps -q
will list the ids of the running containers. If the port mappings contain the passed in argument, it stops the container.
Put the script in the PATH
and you can execute it anywhere:
stopByPort.sh 3306