What the 'docker: command not found' Error Means
The docker: command not found error in Linux means that the shell (bash, zsh, etc.) cannot find the docker executable file in the directories specified in the PATH environment variable.
The full error text may look like:
bash: docker: command not found
or
zsh: command not found: docker
This error occurs when attempting to run any Docker command (docker --version, docker ps, etc.). Return code 127 (standard for "command not found") confirms that the system did not locate the docker executable in the search paths.
Causes
- Docker is not installed on the system at all.
- Docker is installed, but the path to the executable is not added to the
PATHvariable. This can happen with a manual installation from a binary archive or if the package manager did not configurePATHautomatically. - The terminal session has not been updated after Docker installation. Changes to
PATHonly take effect after restarting the session (or runningsourceon the shell profile file). - Attempting to run Docker via
sudowith a cleared environment (by default,sudomay resetPATH). In this case, Docker might only be accessible by specifying the full path. - Corrupted Docker installation or deletion of the executable file.
Solutions
Method 1: Install Docker
If Docker is not installed, perform an installation appropriate for your distribution.
For Ubuntu/Debian:
sudo apt update
sudo apt install docker.io
For CentOS/RHEL 7:
sudo yum install docker
For CentOS/RHEL 8 / Fedora:
sudo dnf install docker
For Arch Linux:
sudo pacman -S docker
After installation, verify that the docker file appears in standard locations (usually /usr/bin/docker).
Method 2: Add Docker's Path to the PATH Variable
If Docker is installed but the command does not work, you need to add the directory containing the executable to PATH.
- Locate the
dockerexecutable:sudo find / -name docker -type f 2>/dev/null | head -5
The path is typically/usr/bin/dockeror/usr/local/bin/docker. - Add the path to
PATHvia the shell configuration file:- For bash (the default shell in Ubuntu/Debian/CentOS):
(replaceecho 'export PATH=$PATH:/usr/bin' >> ~/.bashrc/usr/binwith your directory if different) - For zsh:
echo 'export PATH=$PATH:/usr/bin' >> ~/.zshrc
- For bash (the default shell in Ubuntu/Debian/CentOS):
- Apply the changes:
source ~/.bashrc # or source ~/.zshrc
Method 3: Restart the Session
Sometimes simply restarting the terminal or logging out and back in is sufficient. This updates PATH to reflect any changes made by the package installer.
Method 4: Check Permissions and Configure sudo
Ensure your user has permission to execute the docker file:
ls -l $(which docker)
If execute permissions (x) are missing, fix them:
sudo chmod +x $(which docker)
To use Docker without sudo, add your user to the docker group:
sudo usermod -aG docker $USER
⚠️ Important: After adding to the
dockergroup, you must log out and back in or restart your session for the changes to take effect.
Method 5: Verify the Installation
After completing the previous steps, check if Docker works:
docker --version
If the command returns a version, the issue is resolved. For a full check, run a test container:
docker run hello-world
On success, you will see Docker's welcome message.
Prevention
- When installing via official repositories (apt, dnf, yum, pacman), Docker is usually added to
PATHautomatically. Restart your terminal after installation. - With manual installations (downloading a binary archive), always manually add the executable's directory to
PATH. - Regularly update Docker through your package manager to avoid compatibility issues.
- Use user groups (
docker) to manage permissions instead of running viasudoevery time.
Frequently Asked Questions
Q: What to do if which docker doesn't find the command, but Docker is clearly installed?
A: Check if the docker file exists in your system (sudo find / -name docker -type f). If the file exists but in a non-standard directory (e.g., /usr/local/bin/docker), add that directory to PATH as described in Method 2.
Q: Why doesn't the Docker command work after installing via apt until a reboot?
A: The docker.io package adds the path via the file /etc/profile.d/docker.sh, which is only loaded when a new session starts. Restart your terminal or run source /etc/profile.d/docker.sh.
Q: Can I use Docker in cron jobs?
A: Yes, but PATH is usually limited in cron jobs. Specify the full path to docker (e.g., /usr/bin/docker) or configure the PATH variable within the cron file itself.
Q: Why does sudo docker work, but docker does not?
A: This is a classic sign that docker is not in your user's PATH, but it is installed globally. When using sudo, a different configuration file may load where PATH includes system directories. Solution: add docker to PATH for your user (Method 2).