<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://aznot.com/index.php?action=history&amp;feed=atom&amp;title=Docker%2Fdocker-find-dependants</id>
	<title>Docker/docker-find-dependants - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://aznot.com/index.php?action=history&amp;feed=atom&amp;title=Docker%2Fdocker-find-dependants"/>
	<link rel="alternate" type="text/html" href="https://aznot.com/index.php?title=Docker/docker-find-dependants&amp;action=history"/>
	<updated>2026-05-06T08:04:57Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.41.0</generator>
	<entry>
		<id>https://aznot.com/index.php?title=Docker/docker-find-dependants&amp;diff=5974&amp;oldid=prev</id>
		<title>Kenneth: Created page with &quot;ref: https://raw.githubusercontent.com/RidiculousRichard/bash-dev-scripts/master/docker-find-dependants  ref: https://superuser.com/questions/1187156/with-docker-is-there-a-wa...&quot;</title>
		<link rel="alternate" type="text/html" href="https://aznot.com/index.php?title=Docker/docker-find-dependants&amp;diff=5974&amp;oldid=prev"/>
		<updated>2021-07-01T01:53:46Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;ref: https://raw.githubusercontent.com/RidiculousRichard/bash-dev-scripts/master/docker-find-dependants  ref: https://superuser.com/questions/1187156/with-docker-is-there-a-wa...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;ref: https://raw.githubusercontent.com/RidiculousRichard/bash-dev-scripts/master/docker-find-dependants&lt;br /&gt;
&lt;br /&gt;
ref: https://superuser.com/questions/1187156/with-docker-is-there-a-way-of-determining-what-images-rely-on-another-image&lt;br /&gt;
&lt;br /&gt;
docker-find-dependants:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/bin/bash&lt;br /&gt;
&lt;br /&gt;
# Script to determine what named images and containers are dependent on the specified image (or a containers image) from stdin or in the args&lt;br /&gt;
# An unmatchable input is ignored&lt;br /&gt;
# All of the following will be found:&lt;br /&gt;
#    Any image name / tag / name:tag / id that has a prefix in it that matches the input&lt;br /&gt;
#    Any other image that relies on the images specified&lt;br /&gt;
#    Any containers (running or not) that use any of these images&lt;br /&gt;
&lt;br /&gt;
set -euf -o pipefail&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
specifiedParents=&amp;quot;&amp;quot;&lt;br /&gt;
# Find all the parents&lt;br /&gt;
if [ &amp;quot;$#&amp;quot; -ge &amp;quot;1&amp;quot; ]; then&lt;br /&gt;
	# Parents are in args&lt;br /&gt;
	specifiedParents=&amp;quot;$(printf &amp;quot; %s\n&amp;quot; &amp;quot;$@&amp;quot;)&amp;quot;&lt;br /&gt;
else&lt;br /&gt;
	# Parents arrive via stdin&lt;br /&gt;
	while IFS= read -r inStr; do&lt;br /&gt;
		if [ -n &amp;quot;$inStr&amp;quot; ]; then&lt;br /&gt;
			inStr=&amp;quot;${inStr#&amp;quot;${inStr%%[![:space:]]*}&amp;quot;}&amp;quot;&lt;br /&gt;
			specifiedParents=&amp;quot;$(printf &amp;quot;%s\n %s&amp;quot; &amp;quot;$specifiedParents&amp;quot; &amp;quot;$inStr&amp;quot;)&amp;quot;&lt;br /&gt;
		fi&lt;br /&gt;
	done&lt;br /&gt;
fi&lt;br /&gt;
&lt;br /&gt;
# Check there is some input&lt;br /&gt;
if [ -z &amp;quot;$specifiedParents&amp;quot; ]; then&lt;br /&gt;
	printf &amp;quot;%s&amp;quot; &amp;quot;Nothing specified to search for; images/containers must be specified as either args or on the stdin&amp;quot; &amp;gt;&amp;amp;2 &lt;br /&gt;
	exit 1&lt;br /&gt;
fi&lt;br /&gt;
&lt;br /&gt;
# Collect the container and image info&lt;br /&gt;
containersData=&amp;quot;$(docker ps -a --format &amp;#039;Container {{.ID}} {{.Image}} {{.Names}} {{.Labels}}&amp;#039; --no-trunc)&amp;quot;&lt;br /&gt;
allImagesData=&amp;quot;$(docker images -a --format &amp;#039;Image {{.ID}} {{.Repository}}:{{.Tag}} {{.Repository}} {{.Tag}}&amp;#039; --no-trunc | sed &amp;#039;s/ [^:]*:/ /&amp;#039;)&amp;quot;&lt;br /&gt;
namedImagesData=&amp;quot;$(docker images --format &amp;#039;Image {{.ID}} {{.Repository}}:{{.Tag}}&amp;#039; --no-trunc | sed &amp;#039;s/ [^:]*:/ /&amp;#039;)&amp;quot;&lt;br /&gt;
&lt;br /&gt;
# Check to see if you can find a matching container&lt;br /&gt;
matchedContainerIds=&amp;quot;&amp;quot;&lt;br /&gt;
parentImageIds=&amp;quot;&amp;quot;&lt;br /&gt;
while IFS= read -r aParent; do&lt;br /&gt;
	if [ -z &amp;quot;$aParent&amp;quot; ]; then&lt;br /&gt;
		continue&lt;br /&gt;
	fi&lt;br /&gt;
	&lt;br /&gt;
	# Use space to ensure matching starts at the beginning of a field&lt;br /&gt;
	aContainerId=&amp;quot; $(printf &amp;quot;%s&amp;quot; &amp;quot;$containersData&amp;quot; | grep -F &amp;quot;$aParent&amp;quot; | awk &amp;#039;{print $3}&amp;#039; || true)&amp;quot;&lt;br /&gt;
	if [ &amp;quot;$aContainerId&amp;quot; != &amp;quot; &amp;quot; ]; then&lt;br /&gt;
		# A container matched so use the image ID&lt;br /&gt;
		matchedContainerIds=&amp;quot;$(printf &amp;quot;%s\n%s&amp;quot; &amp;quot;$matchedContainerIds&amp;quot; &amp;quot;$aContainerId&amp;quot;)&amp;quot;&lt;br /&gt;
	fi&lt;br /&gt;
&lt;br /&gt;
	# Also check images&lt;br /&gt;
	while IFS= read -r parentImageId; do&lt;br /&gt;
		if [ -z &amp;quot;$parentImageId&amp;quot; ]; then&lt;br /&gt;
			continue&lt;br /&gt;
		fi&lt;br /&gt;
		# Add the full ID to the parentImageIds, including a space to assist matching later&lt;br /&gt;
		parentImageIds=&amp;quot;$(printf &amp;quot;%s%s\n&amp;quot; &amp;quot;$parentImageIds&amp;quot; &amp;quot; $parentImageId&amp;quot;)&amp;quot;&lt;br /&gt;
	done &amp;lt;&amp;lt;&amp;lt; &amp;quot;$(printf &amp;quot;%s&amp;quot; &amp;quot;$allImagesData&amp;quot; | grep -F &amp;quot;$aParent&amp;quot; | awk &amp;#039;{print $2}&amp;#039;)&amp;quot;&lt;br /&gt;
done &amp;lt;&amp;lt;&amp;lt; &amp;quot;$(printf &amp;quot;%s\n&amp;quot; &amp;quot;$specifiedParents&amp;quot;)&amp;quot;&lt;br /&gt;
&lt;br /&gt;
# Stop if there are no parents or containers&lt;br /&gt;
if [ -z &amp;quot;$parentImageIds&amp;quot; ] &amp;amp;&amp;amp; [ -z &amp;quot;$matchedContainerIds&amp;quot; ]; then&lt;br /&gt;
	exit 0&lt;br /&gt;
fi&lt;br /&gt;
&lt;br /&gt;
# Deduplicate&lt;br /&gt;
parentImageIds=&amp;quot;$(printf &amp;quot;%s&amp;quot; &amp;quot;$parentImageIds&amp;quot; | LC_ALL=C sort -u)&amp;quot;&lt;br /&gt;
&lt;br /&gt;
# Find descendent images&lt;br /&gt;
descendentImages=&amp;quot;&amp;quot;&lt;br /&gt;
while IFS= read -r imageData; do&lt;br /&gt;
	anImageId=&amp;quot;$(printf &amp;quot;%s&amp;quot; &amp;quot;$imageData&amp;quot; | awk &amp;#039;{print $2}&amp;#039;)&amp;quot;&lt;br /&gt;
&lt;br /&gt;
	# Check to see if this ID is a descendent of parentImageIds by imageInfo by moving through it&amp;#039;s lineage&lt;br /&gt;
	areDescendents=&amp;quot;&amp;quot;&lt;br /&gt;
	imageIdsChecked=&amp;quot;&amp;quot;&lt;br /&gt;
	while true; do&lt;br /&gt;
		# Record that anImageId is being checked; including the space at the start to assist matching later&lt;br /&gt;
		imageIdsChecked=&amp;quot;$(printf &amp;quot;%s%s\n&amp;quot; &amp;quot;$imageIdsChecked&amp;quot; &amp;quot; $anImageId&amp;quot;)&amp;quot;&lt;br /&gt;
&lt;br /&gt;
		if printf &amp;quot;%s&amp;quot; &amp;quot;$descendentImages&amp;quot; | grep -q -F &amp;quot;$anImageId&amp;quot;; then&lt;br /&gt;
			# Already determined that anImageId is a descendent of parentImageIds so TheImage is too&lt;br /&gt;
			areDescendents=&amp;quot;true&amp;quot;&lt;br /&gt;
			break;&lt;br /&gt;
		else&lt;br /&gt;
			if printf &amp;quot;%s&amp;quot; &amp;quot;$parentImageIds&amp;quot; | grep -q -F &amp;quot; $anImageId&amp;quot;; then&lt;br /&gt;
				# TheImage is a descendent of parentImageIds&lt;br /&gt;
				areDescendents=&amp;quot;true&amp;quot;&lt;br /&gt;
				break;&lt;br /&gt;
			fi&lt;br /&gt;
&lt;br /&gt;
			# Move onto the parent of anImageId&lt;br /&gt;
			anImageId=&amp;quot;$(docker inspect --format &amp;#039;{{.Parent}}&amp;#039; &amp;quot;$anImageId&amp;quot; | sed &amp;#039;s/.*://&amp;#039;)&amp;quot;&lt;br /&gt;
			if [ -z &amp;quot;$anImageId&amp;quot; ]; then&lt;br /&gt;
				# Reached the end of the line; abandon all hope ye who enter here&lt;br /&gt;
				break;&lt;br /&gt;
			fi&lt;br /&gt;
		fi&lt;br /&gt;
	done&lt;br /&gt;
&lt;br /&gt;
	# Add descendents to the descendentImages&lt;br /&gt;
	if [ -n &amp;quot;$areDescendents&amp;quot; ]; then&lt;br /&gt;
		descendentImages=&amp;quot;$(printf &amp;quot;%s%s\n&amp;quot; &amp;quot;$descendentImages&amp;quot; &amp;quot;$imageIdsChecked&amp;quot;)&amp;quot;&lt;br /&gt;
	fi&lt;br /&gt;
done &amp;lt;&amp;lt;&amp;lt; &amp;quot;$(printf &amp;quot;%s&amp;quot; &amp;quot;$allImagesData&amp;quot;)&amp;quot;&lt;br /&gt;
&lt;br /&gt;
# Identify any named images that are descendents of the parentImageIds&lt;br /&gt;
printf &amp;quot;%s\n&amp;quot; &amp;quot;$namedImagesData&amp;quot; | while IFS= read -r imageData; do&lt;br /&gt;
	thisImageId=&amp;quot;$(printf &amp;quot;%s&amp;quot; &amp;quot;$imageData&amp;quot; | awk &amp;#039;{print $2}&amp;#039;)&amp;quot;&lt;br /&gt;
	if printf &amp;quot;%s&amp;quot; &amp;quot;$descendentImages&amp;quot; | grep -q -F &amp;quot; $thisImageId&amp;quot;; then&lt;br /&gt;
		# We&amp;#039;ve found a descendent&lt;br /&gt;
		printf &amp;quot;%s\n&amp;quot; &amp;quot;$imageData&amp;quot;&lt;br /&gt;
	fi&lt;br /&gt;
done&lt;br /&gt;
&lt;br /&gt;
# Identify containers that rely on images that are descendents of the parentImageIds&lt;br /&gt;
printf &amp;quot;%s\n&amp;quot; &amp;quot;$containersData&amp;quot; || while IFS= read -r containerData; do&lt;br /&gt;
	# Check to see if this container is dependant on a dependant image&lt;br /&gt;
	thisImageId=&amp;quot;$(printf &amp;quot;%s&amp;quot; &amp;quot;$containerData&amp;quot; | awk &amp;#039;{print $3}&amp;#039;)&amp;quot;&lt;br /&gt;
	if printf &amp;quot;%s&amp;quot; &amp;quot;$descendentImages&amp;quot; | grep -q -F &amp;quot; $thisImageId&amp;quot;; then&lt;br /&gt;
		# We&amp;#039;ve found a descendent&lt;br /&gt;
		printf &amp;quot;%s\n&amp;quot; &amp;quot;$containerData&amp;quot;&lt;br /&gt;
		continue&lt;br /&gt;
	fi&lt;br /&gt;
&lt;br /&gt;
	# Check to see if this container itself has been matched&lt;br /&gt;
	thisContainerId=&amp;quot;$(printf &amp;quot;%s&amp;quot; &amp;quot;$containerData&amp;quot; | awk &amp;#039;{print $2}&amp;#039;)&amp;quot;&lt;br /&gt;
	if printf &amp;quot;%s&amp;quot; &amp;quot;$matchedContainerIds&amp;quot; | grep -q -F &amp;quot; $thisContainerId&amp;quot;; then&lt;br /&gt;
		# We&amp;#039;ve matched a container&lt;br /&gt;
		printf &amp;quot;%s\n&amp;quot; &amp;quot;$containerData&amp;quot;&lt;br /&gt;
		continue&lt;br /&gt;
	fi	&lt;br /&gt;
done&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Kenneth</name></author>
	</entry>
</feed>