[Solved] “bash: docker: command not found” Error

LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now

When you run into the “bash: docker: command not found” error, it means that you are having trouble using Docker commands in the Bash shell. Usually, this problem occurs when Docker is not installed or cannot be found in the PATH of the system. Installing Docker and setting up the PATH is necessary to fix this issue and guarantee smooth integration.

Reasons for “docker” Command Not Found Error

Some common reasons for “docker” command not found error are:

  • Docker not Installed: If you are unable to use the “docker” command, it could be that your system does not have Docker installed.
  • Incorrect Installation Path: The PATH on your system may not contain Docker.
  • Shell Session Not Refreshed: Restarting the shell session may be necessary if Docker was just installed for the modifications to take effect.
  • Problems with Installation: The “docker” command may not exist because the Docker installation did not succeed.
  • Inadequate permissions: Docker might not have the authorization it needs to function.

4 Methods to Solve “docker” Command Not Found in Bash

Follow the below methods to solve the docker command not found error in Bash:

1. Install Docker

Most of the time, an incorrect installation of Docker on your system causes the “bash: docker: command not found” error. In this instance, installing Docker fixes the issue. However, if your system has docker installed and still showing the error, first run sudo apt autoremove docker. Now, follow the steps below to install Docker:

  1. Update apt-get repository using:
    sudo apt-get update

    executing sudo apt-get update

  2. Install ca-certificates and curl using:
    sudo apt-get install ca-certificates curl

    installing ca-certificates and curl

  3. Now, run the commands below to create a directory for apt keyring, download the GPG key, and adjust permission:
    sudo install -m 0755 -d /etc/apt/keyrings
    sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
    sudo chmod a+r /etc/apt/keyrings/docker.asc

    downloading docker keyring

  4. Then, add the Docker repository to the system’s package sources list using:
    echo \
    "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
    $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
    sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
    sudo apt-get update

    setting up docker repository

  5. Finally, run the command below to install Docker:
    sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

    installing docker in ubuntu

  6. Now you can verify the Docker Engine installation using:
    sudo docker run hello-world

    Running docker hello-worldThus you can resolve the command not found error regarding docker.

Note: If you are using any other distribution of Linux, follow the commands from Install Docker Engine.

2. Set “docker” PATH Variable

A prevalent reason behind the “docker command not found” is Docker is not present in your system PATH environment variables. To set the PATH variable for docker, follow the steps below:

  1. Press CTRL+ALT+T to open the terminal.
  2. Use which command to check the current path of Docker:
    which docker

    getting docker path with which docker command

  3. Open bashrc file using:
    nano ~/.bashrc
  4. Now, replace <path to docker> in export PATH=$PATH:<path to docker> code with the which command’s output and paste it at the end of the file. For example:

    export PATH=$PATH:/usr/bin/docker

    adding docker path variable to bashrc file

  5. Press CTRL+S to save, and CTRL+X to exit.
  6. Then, execute the bashrc file using:
    source ~/.bashrc
  7. Now you can check running a docker command. For example:
    docker –version

    executing bashrc file with source command

3. Start Docker Daemon

You will need to start the Docker daemon manually if it is not running even after installing and adding it to PATH. Your operating system will determine the precise steps, but in general, you can launch the Docker daemon by using the following command:

sudo service docker start

executing sudo service docker startRun docker –version to check if docker is activated now.

4. Create Symbolic Link to Docker

A symbolic link in Linux is a special type of file that serves as a reference or pointer to another file or directory. It acts as a shortcut, allowing users to create a link to a file or directory without copying its content. To create a symbolic link to docker, first get the path of docker using which or whereis command:

whereis docker

getting docker path with whereis docker commandNow, execute ln -s /[path to the docker] /bin/docker. Replace [path to the docker] with the directory found from whereis command. For example:

ln -s /usr/bin/docker  /bin/docker

Conclusion

In conclusion, resolving the “bash docker command not found” issue involves ensuring the installation of Docker and configuring the system’s PATH appropriately. By applying these solutions, developers can seamlessly access Docker commands within the Bash environment and unlock Docker’s powerful containerization capabilities.

People Also Ask

Why my docker command is not found?

Possible reasons for the docker command not being found are: Docker not installed, PATH misconfiguration, terminal not restarted, insufficient permissions, or incorrect installation.

How to run bash in docker command?

Use docker run -it <image_name> bash to run an interactive Bash shell within a Docker container. Replace <image_name> with the name or ID of the Docker image.

How do I enter Docker Bash?

To enter Docker Bash, use docker exec -it <container_name_or_id> bash command. Replace <container_name_or_id> with the name or ID of the running Docker container.

How do I access a docker container Bash?

To access a Docker container’s Bash shell, use the command docker exec -it <container_name> bash. Replace <container_name> with the name or ID of the running Docker container.

How to activate docker in cmd?

To activate Docker in Command Prompt (cmd), ensure Docker Desktop is installed and running. You can directly use CMD commands for Docker such as docker run or docker ps in the command prompt or script once Docker is set up. No separate activation is required.

How to solve “sudo: docker: command not found”?

If you’re getting the “sudo: docker: command not found” error, it means that the docker is not installed on your system, or it’s not in the system’s PATH for the user running the sudo command. To solve this error, install docker in your system. If docker is installed but you still get the error, open ./bashrc file and add the line below:

export PATH=$PATH:[path to docker]

To get the [path to docker], run which docker command.

Related Articles


<< Go Back to [Solved] “bash: command not found” Error | Bash Error Handling and Debugging | Bash Scripting Tutorial

Rate this post
Ashikur Rahman

Hello, I’m Ashikur Rahman, currently working as a Linux Content Developer Executive at SOFTEKO. I have completed my graduation in Computer Science and Engineering from Khulna University of Engineering & Technology (KUET). Also, I’m pursuing my master’s in the same department at Bangladesh University of Engineering & Technology (BUET). I like to learn new technologies, contribute, and share those with others. Here my goal is to provide beneficial and user-friendly articles on Linux distribution for everyone. Read Full Bio

Leave a Comment