How to Install Docker on Linux and Master Common Commands

docker installationdocker commandslinux docker setupdocker mirror accelerationdocker container management
Published·Modified·

Docker is an open-source application container engine that allows developers to package their applications and dependencies into a portable image. These images can be deployed on any popular Linux or Windows machine, enabling virtualization. Containers operate using a sandbox mechanism, ensuring complete isolation from one another.

Prerequisites

Installing Docker on Linux requires a kernel version greater than 3.1. Additionally, OpenVZ virtualization is not supported by Docker.

To check your Linux kernel version:

# Check Linux kernel version
uname -r

To check the virtualization architecture using virt-what:

# Install virt-what on CentOS
yum -y install virt-what
# Install virt-what on Debian or Ubuntu
apt-get install -y virt-what
# Check virtualization architecture
virt-what

Ensure your Linux kernel is greater than 3.1 and that you are not using OpenVZ virtualization before proceeding with the Docker installation.

Installing Docker on CentOS 7

First, uninstall any old versions of Docker:

sudo yum remove docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-engine

Set up the Docker repository:

sudo yum -y install -y yum-utils
sudo yum-config-manager \
    --add-repo \
    https://download.docker.com/linux/centos/docker-ce.repo

Install the latest Docker Engine and container:

sudo yum -y install docker-ce docker-ce-cli containerd.io

Start Docker

# Start Docker
sudo systemctl start docker
# Enable Docker to start on boot
sudo systemctl enable docker

For more details, refer to: https://docs.docker.com/engine/install/centos/

Installing Docker on Debian

Older versions of Docker were known as docker, docker.io, or docker-engine. Before installation, uninstall these old versions:

# Remove old Docker versions
sudo apt-get remove docker docker-engine docker.io containerd runc

Next, update the package list and install the required dependencies:

# Update packages
sudo apt-get update
# Install required dependencies
sudo apt-get install \
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg2 \
    software-properties-common

Continue by adding the official Docker GPG key:

curl -fsSL https://download.docker.com/linux/debian/gpg | sudo apt-key add -

Import the repository source:

sudo add-apt-repository \
   "deb [arch=amd64] https://download.docker.com/linux/debian \
   $(lsb_release -cs) \
   stable"

Install the latest version of Docker Engine-Community and containerd:

# Update packages
sudo apt-get update
# Install Docker
sudo apt-get install docker-ce docker-ce-cli containerd.io

For more details, refer to: https://docs.docker.com/engine/install/debian/

Verification and Testing

If the installation was successful, you can check the current version by running:

root@zhanmei:~# docker -v
Docker version 19.03.7, build 7141c199a2

You can also run a Hello World test to verify functionality:

sudo docker run hello-world

Docker Image Acceleration

Docker images are hosted overseas, and pulling them in China can be very slow. You can use the NetEase Docker mirror source to accelerate downloads. Create a new file /etc/docker/daemon.json and add the following content:

{
 "registry-mirrors": ["http://hub-mirror.c.163.com"]
}

Common Docker Commands

  • View running Docker processes: docker ps
  • Start a container: docker start <id>
  • Stop a specific Docker process: docker stop <id>
  • Search for images: docker search <name>
  • View installed images: docker images
  • Remove an image: docker rm <image id>

Summary

This article briefly summarizes Docker installation and common commands. You may encounter other issues in actual practice, so this guide is for reference only. For more information, please consult the official Docker documentation.

This article references: https://docs.docker.com/install/linux/docker-ce/debian/