Installing Docker on Linux and Common Docker Commands

Publish: 2020-03-10 | Modify: 2020-05-01

Docker is an open-source application container engine that allows developers to package their applications and dependencies into a portable image, which can then be deployed on any popular Linux or Windows machine, as well as virtualized environments. Containers are completely isolated from each other and do not have any interfaces.

Prerequisites

To install Docker on Linux, the kernel version needs to be greater than 3.1, and OpenVZ virtualization is not supported.

To check the Linux kernel version, use the following command:

# Check Linux kernel version
uname -r

To check the virtualization architecture using virt-what:

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

Please ensure that your Linux kernel version is greater than 3.1 and that you are not using the OpenVZ virtualization architecture. Now let's proceed with the Docker installation.

Install Docker on CentOS 7

First, uninstall the old version 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 information, refer to: https://docs.docker.com/engine/install/centos/

Install Docker on Debian

The old versions of Docker were known as docker, docker.io, or docker-engine. Before installing the new version, uninstall the old versions:

# Download the old versions of Docker
sudo apt-get remove docker docker-engine docker.io containerd runc

Next, update the package and install the necessary dependencies with the following command:

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

Add the Docker official GPG key:

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

Import the dep 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 the package
sudo apt-get update
# Install Docker
sudo apt-get install docker-ce docker-ce-cli containerd.io

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

Verify and Test

If the installation is successful, running the command docker -v will display the current version:

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

You can also run a Hello World! test to check if Docker is functioning properly:

sudo docker run hello-world

Docker Image Acceleration

Docker images are hosted overseas, and the download speed in China is very slow. To accelerate the download speed, you can use the Netease Docker image source. Create a file /etc/docker/daemon.json with 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 <container_id>
  • Stop a specific Docker process: docker stop <container_id>
  • Search for an image: docker search <name>
  • View installed images: docker images
  • Remove a specific image: docker rm <image_id>

Summary

The above instructions provide a simple overview of Docker installation and common commands. During the actual process, you may encounter other issues. This information is provided for reference only. For more detailed instructions, it is recommended to consult the official Docker documentation.

This article is partially referenced from: https://docs.docker.com/install/linux/docker-ce/debian/


Comments