Docker is a container technology used to run containers in Linux, Windows systems with little system resources. In this tutorial, we will examine how to install Docker and run containers in deb
or apt
based distros like Ubuntu, Debian, Mint, Kali, etc.
Update Apt Repository Information
The best practice before installing a package from a package repository is updating the repository information and getting the latest changes. We will start updating the repository with apt update
or apt-get update
command like below.
$ sudo apt update
OR
$ sudo apt-get update

Install Docker From Ubuntu Repository
Rpm or Yum based distributions like RedHat, CentOS, Fedora uses docker
as package names without a problem. Because there is no collusion with the name docker
. But deb
or apt
based distributions like Ubuntu, Debian, Mint, Kali have some restriction because docker
package name is used by other application. So we need to use docker.io
as package name while installing the Docker.
$ sudo apt install docker.io
OR
$ sudo apt-get install docker.io
Install Docker From Official Docker Repository
Docker is also provided as Ubuntu packages from the official Docker repository. Docker repository provides the latest version as it is the creator of the Docker packages. First, we will add GPG keys for the official Docker repository to the current system. We will use curl
and apt-key
commands like below.
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add

Now we can add official Docker Ubuntu repository with the add-apt-repository
command like below.
$ sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"

Now everything is ready to install Docker on Ubuntu. Before installing the official Docker we can print some information about it with the apt show
command like below. The package name provided by Docker will be docker-ce
which is different from the Ubuntu provided Docker which is docker.io
. But most of the things are the same.
$ apt show docker-ce

We can see that information like the package name, version, installed-size, etc is provided.
We can install the Docker package with the following command. We will use sudo
command in order to install the package.
$ sudo apt install docker-ce

Start Docker Service
Docker works as a service that is named docker
for all Docker installation. We can start the Docker service in order to use Docker, create containers, etc. We can start the Docker service with the following command. Docker service start will require root
privileges which can be provided with the sudo
command or current user password will be asked.
$ sudo systemctl start docker
OR
$ systemctl start docker

Stop Docker Server
We can stop the Docker server if we do not want to use or to reload it by stopping and starting. We will use the following command.
$ sudo systemctl stop docker
OR
$ systemctl stop docker

Print Docker Service Status
As Docker runs as a service if there is a problem by creating, running, saving docker containers we generally look to the Docker service status. We can print Docker service status with the following command.
$ systemctl status docker

We can see that Docker service is currently active or running from the given time. Information like Process ID, Task count, Memory usage, and last logs about the service is also provided.
Print Docker Version
Some features are provided upward from the given version. So if we want o use a feature with a given version number we need to know the current version of the Docker installation. We can print the Docker version with the --version
option like below.
$ docker --version

Search Docker Images
Generally, the first actions after installing Docker is searching for some images in order to download. We will use the command docker
for most of the operations like search. In this example, we will search the images named or tagged with the term centos
.
$ docker search centos

Download Docker Image
We can download or pull specific image before using it. We will use the pull
command which will download the image but do not start any container. In this example, we will pull the image named centos
.
$ docker pull centos

List Docker Images
Before downloading an image or in order to learn locally existing images we can use docker images
command which will list currently downloaded and existing images.
$ docker images

Create and Run Sample CentOS Container
Creating a container is the same for all distributions. We will use docker
command for this. We do not use docker.io
because it is not a command just a package name. In this example, we will run a bash shell in CentOS
container interactively. We need to be in docker
group or have root privileges in order to run Docker containers. So
$ sudo docker run -t -i centos bash

List Running Docker Containers
We can run multiple Docker containers at the same time. If we want to list currently running Docker containers we can use docker ps
command which will list only running Docker containers.
$ docker ps

Stop Running Docker Container
In the end, we want to stop a container we do not need for now. We can use stop
command of the Docker like below. We also need to provide Docker ID which can be obtained with the Docker running container list command.
$ sudo docker stop 1e4adf4c3c84