How to Install Docker and Docker Compose on a Linux Server

What is Docker

Docker is a tool designed to make it easier to develop, deploy, and run applications using containers. The software that hosts containers is called Docker Engine. It first appeared in 2013 and is developed by Docker, Inc.

Containers are isolated from each other and package their own software, libraries, and configuration files; they can communicate with each other through precisely defined channels. All containers are managed by the operating system kernel and are therefore lighter than virtual machines. Containers are created from images that specify their exact content.

How to install Docker

First, we need to connect to our server.

We execute the first command and see if there are updates for our OS.
We used Ubuntu 20.04 to install Docker, so please use the same OS, as procedure is not same for CentOS or other RHEL based operating sytems.

apt update

Everything is fine and there are no updates available in our case. If you have them, answer the question with “Y” and press Enter.

Then we need to install the required packages before installing Docker itself.

apt install \
    ca-certificates \
    curl \
    gnupg \
    lsb-release

We accept with the letter “Y” and wait for them to be installed.

After, we add the GPG key for Docker. We press Enter and there should be no answer, just a new empty line.

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg

Next, we add the stable Docker repositories. There will also be no answer – this means that everything is fine.

echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \
  $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

Now we re-look for new updates and install Docker.

apt update
#Then run the command below
apt install docker-ce docker-ce-cli containerd.io

The installation has started. We wait now.

After the install process is done, we are gonna to install Docker Compose itself.

apt install docker-compose

We accept and wait for Docker Compose to be installed.

Is that all or?
Of course not, we are now installing Portnainer to make it easier and more convenient for us to manage the container or containers if there will be more of them on your server in the future. Portnainer itself will be available at the IP address: https://IP:9443.

We execute the command and wait for the installation to be done.

docker run -d -p 8000:8000 -p 9443:9443 --name portainer \
    --restart=always \
    -v /var/run/docker.sock:/var/run/docker.sock \
    -v portainer_data:/data \
    portainer/portainer-ce:latest

After installation, go to the web interface using the link https://IP:9443 and create a user.

Thats all! On this, we have completed the installation of three important things and all of them will be very useful to us in the future use of the server, when adding new Docker containers.