bash: line 1: dig: command not found in an Ubuntu container means the DNS
tools package is missing. Run apt-get update and
apt-get install -y dnsutils, then dig works.
dnsutils is the name to remember; on Ubuntu 24.04 it pulls in
bind9-dnsutils, which holds the program.
The error
docker exec fix10-lc-r8 bash -c 'dig archive.ubuntu.com 2>&1; echo exit=$?'
bash: line 1: dig: command not found
exit=127
The command went through bash -c, which is why the message carries
line 1:.
Why it happens
The ubuntu:24.04 image does not include the DNS tools, so bash finds no
program called dig and returns 127. The message says nothing about DNS
itself; once the package was installed, the same container resolved the name at once.
After the install, dpkg -s showed how the two package names relate:
dnsutils depends on bind9-dnsutils, and both carry the same
version.
docker exec fix10-lc-r6 bash -c 'dpkg -s dnsutils bind9-dnsutils 2>&1 | grep -E ''^(Package|Version|Status|Depends)'''
Package: dnsutils
Status: install ok installed
Version: 1:9.18.39-0ubuntu0.24.04.9
Depends: bind9-dnsutils
Package: bind9-dnsutils
Status: install ok installed
Version: 1:9.18.39-0ubuntu0.24.04.9
...
The fix
docker exec fix10-lc-r6 bash -c 'dig +short archive.ubuntu.com 2>&1; echo exit=$?'
91.189.90.13
91.189.90.12
185.125.190.83
91.189.92.24
185.125.190.81
91.189.91.83
91.189.92.22
91.189.92.23
91.189.91.82
91.189.90.11
91.189.91.81
185.125.190.82
exit=0
The package had been installed earlier in the same container with
apt-get install -y dnsutils.
+short printed only the addresses, one per line, which suits scripts.
dpkg -S showed the same package also installs nslookup:
docker exec fix10-lc-r6 bash -c 'apt-get install -y dnsutils > /dev/null 2>&1; echo install-exit=$?; dpkg -S bin/nslookup bin/dig 2>&1; nslookup archive.ubuntu.com 2>&1 | head -6'
install-exit=0
bind9-dnsutils: /usr/bin/nslookup
bind9-dnsutils: /usr/bin/dig
...
If you only need to know whether a name resolves and cannot install anything,
getent hosts archive.ubuntu.com listed the same twelve addresses in a fresh
container before any package was installed:
docker exec fix10-lc-r9 bash -c 'dpkg -l | grep -c ^ii; getent hosts archive.ubuntu.com; echo exit=$?'
92
...
exit=0
The address lines are cut here; the nslookup post linked below discusses that check.
Read the exit code before you blame the network. Here 127 meant only that the program
was missing; after the install dig returned 0 and printed answers from the
same container, with nothing else changed. If dig runs but the lookup
itself fails, that is a different problem, and the post on
Temporary failure resolving
covers a broken resolver configuration.
How it was reproduced
The capture was made in a container started with
docker run -d --name fix10-lc-r6 ubuntu:24.04 sleep infinity (Ubuntu 24.04.5
LTS, bash 5.2.21) on Docker Engine 29.9.3, with commands sent through
docker exec ... bash -c. The installed version was
1:9.18.39-0ubuntu0.24.04.9, and the lookups went to the public Ubuntu mirror
name. The container name is the test's own.
Every command was sent from Windows PowerShell 5.1 exactly as shown, with the -c text in single quotes so it reached the container unchanged; a single quote inside that text is written twice, as PowerShell requires.
Frequently asked
- Which package provides dig on Ubuntu?
- dnsutils, which depends on bind9-dnsutils. In the test both were installed at version 1:9.18.39-0ubuntu0.24.04.9 and dig then worked.
- How do I get only the IP addresses from dig?
- Add +short. In the test dig +short archive.ubuntu.com printed twelve addresses, one per line, and returned 0.
- Why is dig not found in my Docker container?
- The ubuntu:24.04 image does not include the DNS tools, so bash finds no dig program and returns 127.
More decoded errors in the Fixes category. The same package fixes bash: nslookup: command not found.