Docker is one of the most popular software for running containers. With Docker, developers can build, deliver, and run their code in containers. The host where Docker is installed and containers are running is called the Docker Engine. In this article, we’ll walk you through how to install Docker Engine (Community Edition) and Docker Compose on Linux (CentOS, Debian, and Ubuntu).
How to Install Docker on Ubuntu and Debian?
On Ubuntu and Debian 11 (Bullseye), you can install docker from the default repositories or from the official Docker repos.
Update the package list:
$ sudo apt update
Install the packages required to add a new HTTPS repository :
$ sudo apt install apt-transport-https ca-certificates curl gnupg lsb-release -y
Add the GPG key of the Docker repository:
$ curl -fsSL https://download.docker.com/linux/debian/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
Add repository:
$ echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/debian \
$(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
Now you can install Docker:
$ sudo apt update
$ sudo apt -y install docker-ce docker-ce-cli containerd.io
Enable and start Docker:
$ sudo systemctl enable docker
$ sudo systemctl start docker
Once installed, check the version of docker and the status of its services:
$ sudo docker version
$ sudo systemctl status docker
You can run a test hello-world container:
$ sudo docker run hello-world
A message should appear:
Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker...
List running containers:
$ docker ps
Installing Docker Engine on CentOS/RHEL Linux
To install Docker on CentOS/RHEL 8, run the following commands:
dnf install wget
Add a repository:
wget -P /etc/yum.repos.d/ https://download.docker.com/linux/centos/docker-ce.repo
Install docker:
dnf install docker-ce docker-ce-cli
Use the following command to install docker on CentOS 7:
yum install docker-ce docker-ce-cli containerd.io
Enable docker:
systemctl enable docker –now
How to Install Docker Compose on Linux?
Docker-compose allows you to deploy multi-container Docker applications.
Check out the latest available version of Docker Compose here https://github.com/docker/compose/releases. In my case it is v2.6.0.
Install it:
COMVER=2.6.0
curl -L "https://github.com/docker/compose/releases/download/v$COMVER/docker-compose-$(uname -s)-$(uname -m)" -o /usr/bin/docker-compose
Allow the file to run:
chmod +x /usr/bin/docker-compose
Check version:
docker-compose --version
Docker Post-Installation Steps in Linux
After installing Docker, create a JSON file with the following config:
vi /etc/docker/daemon.json
{
"data-root": "/opt/docker",
"storage-driver": "overlay2",
"log-driver": "json-file",
"log-opts": {
"max-size": "10m",
"max-file": "7"
}
}
- data-root – docker root directory;
- storage-driver – storage driver (recommended to use overlay2)
- log-driver – log driver to write logs. If you specify json-file, then file storage will be used for logging;
- log-opts – limiting the size of the logs (7 files of 10 MB each).
Common Docker Errors
When starting docker, you may get an error:
/usr/bin/containerd: symbol lookup error: /usr/bin/containerd: undefined symbol: seccomp_api_set
To fix the error, you need to update the libseccomp library:
- Ubuntu/Debian:
apt-get --only-upgrade install libseccomp2
- CentOS:
yum update libseccomp
Another error when the system cannot create a docker network interface:
error initializing network controller list bridge addresses failed no available network
In this case, install the package for managing bridge interfaces:
- Ubuntu/Debian:
apt-get install bridge-utils
- CentOS:
yum install bridge-utils
Now create an interface:
brctl addbr docker0
And assign a static IP address:
ip addr add 192.168.200.1/24 dev docker0
Enable interface:
ip link set dev docker0 up
When running docker as a non-roo user, an error will appear:
Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get "http://%2Fvar%2Frun%2Fdocker.sock/v1.24/containers/json": dial unix /var/run/docker.sock: connect: permission denied
To allow a local user to run docker without sudo, add them to the group:
$ sudo usermod -aG docker youruser