How To Install Docker on Debian 9

Docker is a free and open-source containerization software that helps to deploy, run applications in a container. The containers are similar to a virtual machine but consume fewer resources, easy to manage and can run anywhere regardless of the operating environment it is running in.
Docker uses cgroups and namespace to allow the independent containers to run within a single Linux instance.
This guide will help you install Docker on Debian 9.
Docker Editions
Docker is now available in two editions, namely.
- Community Edition (CE)
- Enterprise Edition (EE)
Here, we will install Docker Comunity Edition (CE) from the Docker repository.
Prerequisites
Uninstall older versions of Docker called docker or docker-engine along with associated dependencies. If your system does not have a Docker package, skip the below step.
sudo apt-get -y remove docker docker-engine docker.io
Contents such as volumes, images, and networks under the /var/lib/docker/ directory are preserved.
Setup Docker Repository
Install the below packages to have apt-get get the support of the https method.
sudo apt-get update sudo apt-get install -y apt-transport-https ca-certificates wget software-properties-common
Add the GPG key for the Docker repository on your system.
wget https://download.docker.com/linux/debian/gpg sudo apt-key add gpg
Add the official Docker repository to the system by running below command in the terminal.
echo "deb [arch=amd64] https://download.docker.com/linux/debian $(lsb_release -cs) stable" | sudo tee -a /etc/apt/sources.list.d/docker.list
Update the apt database.
sudo apt-get update
Make sure you are installing Docker from the official repository, not from the default Debian repository.
sudo apt-cache policy docker-ce
You should see the output like below with the Docker repository details.
docker-ce: Installed: (none) Candidate: 5:19.03.5~3-0~debian-stretch Version table: 5:19.03.5~3-0~debian-stretch 500 500 https://download.docker.com/linux/debian stretch/stable amd64 Packages 5:19.03.4~3-0~debian-stretch 500 500 https://download.docker.com/linux/debian stretch/stable amd64 Packages 5:19.03.3~3-0~debian-stretch 500 500 https://download.docker.com/linux/debian stretch/stable amd64 Packages
Install Docker
Install Docker using the apt-get command.
sudo apt-get -y install docker-ce
Control Docker service
To start Docker, run:
sudo systemctl start docker
To stop Docker service, run:
sudo systemctl stop docker
To restart Docker service, run:
sudo systemctl restart docker
To check the status of Docker service, run:
sudo systemctl status docker
To enable Docker service to autostart on system boot, run:
sudo systemctl enable docker
Verify Docker Installation
To test the Docker installation, we will run a hello-world container.
sudo docker run hello-world
The below output confirms that we have correctly installed Docker on Debian OS.
Unable to find image 'hello-world:latest' locally latest: Pulling from library/hello-world 1b930d010525: Pull complete Digest: sha256:4fe721ccc2e8dc7362278a29dc660d833570ec2682f4e4194f4ee23e415e1064 Status: Downloaded newer image for hello-world:latest Hello from Docker! This message shows that your installation appears to be working correctly. To generate this message, Docker took the following steps: 1. The Docker client contacted the Docker daemon. 2. The Docker daemon pulled the "hello-world" image from the Docker Hub. (amd64) 3. The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading. 4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal. To try something more ambitious, you can run an Ubuntu container with: $ docker run -it ubuntu bash Share images, automate workflows, and more with a free Docker ID: https://hub.docker.com/ For more examples and ideas, visit: https://docs.docker.com/get-started/
Allow Non-root user to run Docker
By default, to run Docker commands, the user should have root privileges or equivalent privileges via sudo. Sometimes we may need to allow non-root users to run Docker containers, so follow the below steps to allow them to run containers.
Create a group docker, if it does not exist.
sudo groupadd docker
Add your user to the docker group, replace raj with your username.
sudo useradd -m raj
Add a user to the docker group.
sudo usermod -aG docker raj
Log out and log back in. You should now be able to run Docker commands without prefixing sudo.
docker run hello-world
Interested Topics
Docker Basic Topics
1: Top Important Docker Commands – Working with Docker Containers
2: Working with Docker Images – Building Docker Images
3: How to Build Docker Images with DockerFile
Docker Advanced Topics
1: How to Setup Docker Private Registry on CentOS 7
2: How to Install and Configure Docker Swarm on CentOS 7
Conclusion
That’s All.