You are currently viewing How to install Docker on a Linux machine

How to install Docker on a Linux machine

Docker is an open-source platform that leverages containerization to enable automated deployment, scalability, and management of applications. It provides an easy way to package an application with all its dependencies into a container (a standardized entity). This article highlights how to install docker on a Linux machine.

Note that there is already a package named “docker” on Kali before you attempt to install Docker; as a result, Docker must be installed under a different name. You won’t get the container version if you install Docker. Docker.io is the name of the version we will be installing. Running docker on the command line will be the proper command because all commands are identical.

Launch your terminal and type in the following commands to set up the Docker apt repository!
# Add Docker's official GPG key:
sudo apt-get update
sudo apt-get install ca-certificates curl gnupg
sudo install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
sudo chmod a+r /etc/apt/keyrings/docker.gpg

# Add the repository to Apt sources:
echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] 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

Install the Docker packages and Docker Compose.

sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin docker-compose

Docker Compose error

When you start interacting with docker-compose, you might encounter some errors. See a snippet below.

File "/usr/lib/python3/dist-packages/compose/cli/docker_client.py", line 41, in get_client
    client = docker_client(
             ^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/compose/cli/docker_client.py", line 170, in docker_client
    client = APIClient(use_ssh_client=not use_paramiko_ssh, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/docker/api/client.py", line 197, in __init__
    self._version = self._retrieve_server_version()
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/docker/api/client.py", line 221, in _retrieve_server_version
    raise DockerException(
docker.errors.DockerException: Error while fetching server API version: HTTPConnection.request() got an unexpected keyword argument 'chunked'

To resolve this, uninstall the standalone Docker Compose and rely on the newer Docker Compose v2 (which is part of Docker CLI):

sudo apt remove docker-compose
docker compose version

To run docker compose commands:

docker compose up
docker compose up -d
docker compose up -d --build
docker compose down

You can now interact with docker as a sudo user at this stage. To use docker without sudo, you will need to add your_username to the docker group using the command below.

sudo usermod -aG docker $USER  #this adds the current to the docker group.
newgrp docker #activate the changes to groups

To login in from the command line, you will need to create an account from the docker website. Navigate to https://hub.docker.com/signup to create an account or https://hub.docker.com/login to log in if you already have an account.

Configure Docker to start on boot with systemd

sudo systemctl enable docker.service
sudo systemctl enable containerd.service

Run the following command from the terminal to login to your docker hub account.

docker login

You will be prompted to key in your username and password.

A common error when trying to login: Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Post “http://%2Fvar%2Frun%2Fdocker.sock/v1.24/auth”: dial unix /var/run/docker.sock: connect: permission denied

Solution: Ensure to log in as the sudo us as below or ensure to add your username in the docker group.

sudo docker login

Common Docker commands that you can use

docker run image >> downloads the image from online if not present on the local repository
docker ps >> lists all running containers
docker ps -a >> lists all containers ; may be running or stopped
docker stop container-name >> stop running container
docker rm container-name >> remove container permanently
docker images >> lists all images
docker rmi image-name >> removes image ; first stop image to remove it
docker pull image-name >> pulls the image to local repository, but doesnt automatically run it.
docker inspect container-name >> gives more infor:: among them env definitions

For more information on setting up docker on other distributions, refer to the official documentation: https://docs.docker.com/get-docker/

Should you face any challenges, reach out to us, by posting a comment, or contacting us for assistance.

For some reason, you may want to install docker. See the procedure on how to uninstall docker.

How to Uninstall docker from a Linux machine

Run the following commands to uninstall Docker

sudo systemctl stop docker   #to stop the docker service
sudo apt-get purge docker-ce docker-ce-cli containerd.io docker-compose #uninstall docker 
sudo rm -rf /var/lib/docker  #Remove Docker volumes, images, and containers (optional but recommended)

If docker was installed using the snap package, the above will not uninstall it. Run the following command

sudo snap remove docker #to uninstall docker install through the snap package

#other commands that may be helpful
docker --version    #see the current docker version on your system
ps aux | grep docker   #see if there are any docker processes running and kill them.

Finally, the following commands be run to remove all docker-related directories:

sudo rm -rf /etc/docker
sudo rm -rf /var/lib/docker
sudo rm -rf /var/run/docker.sock

Make a donation to support us



Web Hosting and email hosting Packages


For web development services, SEO services, Digital marketing strategies, website set up services, web hosting and domain registration; contact Dynamic Technologies.


Related content