Docker installation and deployment super detailed tutorial (version 2021)

1, Preconditions

1.1. kernel

Docker has high requirements for the kernel, so it is generally recommended to run directly on a platform such as Ubuntu. However, as a container standard, docker also supports other platforms such as CentOS, Mac OS X, Windows, etc. Currently, docker supports the following versions of CentOS:

  • CentOS 7(64 bit)
  • CentOS 6.5(64 bit) and later

When running CentOS 6.5 and later, the kernel version > = 2.6.32-431 is required, because these kernels contain some specific modifications for running Docker.

$ uname -r
2.6.32-431.17.1.el6.x86_64

Note: many online tutorials say CentOS 6 5. You must upgrade the kernel to 3.10 to use docker. In fact, it is an [optional] upgrade, but it is best to upgrade.

1.2. Device Mapper

Docker uses AUFS as the storage driver by default, but AUFS is not included in the Linux mainline kernel. Device Mapper can be used as the storage driver in CentOS, which is a new feature introduced in the 2.6.9 kernel version. We need to confirm whether this function is enabled:

$ ls -l /sys/class/misc/device-mapper
lrwxrwxrwx 1 root root 0 May  1 20:55 /sys/class/misc/device-mapper -> ../../devices/virtual/misc/device-mapper

If Device Mapper is not detected, install the Device Mapper package:

$ sudo yum install -y device-mapper

Then reload dm_mod kernel module:

$ sudo modprobe dm_mod

2, Docker installation

2.1. CentOS 7

2.2.1. prepare

  • CentOS7 can access the Internet

  • yum -y install gcc

  • yum -y install gcc-c++

  • Uninstall old version

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

If yum reports that none of these packages are installed, this is normal.

2.2.2. install

  • Install dependent packages

yum install -y yum-utils device-mapper-persistent-data lvm2
  • Set stable image warehouse

yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
 or
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

  • Update yum package index

yum makecache fast

If this is the first time you refresh the package index since adding the Docker image warehouse, you will be prompted to accept the GPG key and the fingerprint of the key will be displayed. Verify that the fingerprint is correct and accept the key if it is correct. Fingerprint should match 060A 61C5 1B55 8A7F 742B 77AA C52F EB6B 621E 9F35.

  • Install docker

1. Install the specified version:

yum list docker-ce.x86_64  --showduplicates | sort -r    #List docker CE versions from high to low

The package name is the package name (docker CE) plus the version string (second column), from the first colon (:) to the first hyphen, separated by a hyphen (-).

For example, specify the version (docker-ce-18.09.9) for installation:

yum install docker-ce-<VERSION_STRING> docker-ce-cli-<VERSION_STRING> containerd.io

# For example:
yum install docker-ce-18.09.9 docker-ce-cli-18.09.9 containerd.io

2. Install the latest version:

yum -y install docker-ce
  • Start docker

Start docker by executing the following command:

systemctl start docker
  • Test docker

2.2.CentOS 6.5

2.2.1. prepare

  • Disable selinux [optional]

Due to the conflict between selinux and LXC, selinux.com needs to be disabled Edit / etc/selinux/config and set two key variables

SELINUX=disabled
SELINUXTYPE=targeted

Note: there are tutorials on the Internet that say it should be set, but I suggest not to set it, because it will affect security

  • Install Fedora EPEL source

yum install http://ftp.riken.jp/Linux/fedora/epel/6/x86_64/epel-release-6-8.noarch.rpm

  • Add hop5 Repo source

cd /etc/yum.repos.d
wget http://www.hop5.in/yum/el6/hop5.repo

2.2.2.yum install

  • Upgrade 3.10 kernel with aufs module [optional]

yum install kernel-ml-aufs kernel-ml-aufs-devel

Modify grub's main configuration file / etc / grub Conf, set default=0, indicating that the content under the first title is the default boot kernel (generally, the newly installed kernel is in the first position):

Restart the system {reboot now, and then execute the following command to check whether it is already a 3.10 kernel:

Check whether the kernel supports aufs:

  • Install docker

Installation dependency:

yum install redhat-lsb        
yum install device-mapper-libs
yum install libcgroup*

If the following error occurs:

The solution is to edit / etc / yum repos. d/epel. Repo, comment out the addresses of the base URL and the mirror list:

To install docker:

yum install docker-io

In case of the following errors:

Error: Cannot retrieve metalink for repository: epel. Please verify its path and try again

The solution is to edit / etc / yum repos. d/epel. Repo, comment out the addresses of the base URL and the mirror list:

If the following error occurs:

The reason is: network problem, download failed. Solution: execute the yum install docker IO command several times until the download and installation are successful (it takes me more than 1 hour here).

Continue the installation. If the following information appears, it indicates that the installation can be normal:

  • Start docker

Start docker by executing the following command:

service docker start

If the above prompt appears, the installation and startup are successful.

If the following prompt appears, the installation fails:

Solution: execute the following command to delete docker io

sudo yum remove docker-io

Manually install docker by executing the following command:

rpm -ivh ./docker-engine-1.7.1-1.el6.x86_64.rpm

Note: download address of installation package: https://yum.dockerproject.org/repo/main/centos/6/Packages/

Execute the following command again to start docker:

service docker start

[optional] set to start when the server starts:

chkconfig docker on

3. Use

  • View docker version
docker version

  • View docker logs
cat /var/log/docker

  • Search image
docker search tomcat

Note: start stands for popularity. OFFICIAL stands for the OFFICIAL version

  • View all current mirrors
docker images
  • Download Image
docker pull centos
  • Run container
docker run centos echo "hello word"
  • Run container hello word
docker run hello-world

After outputting this prompt, hello world will stop running and the container will terminate automatically.

4. Delete

You can delete docker using yum or up2date

  • Query delete docker

1. List the specific name of docker package

$ sudo yum list  installed | grep docker
containerd.io.x86_64                 1.2.4-3.1.el7 
docker-ce.x86_64                     3:18.09.3-3.el7
docker-ce-cli.x86_64                 1:18.09.3-3.el7

2. Delete docker

$ sudo yum -y remove containerd.io.x86_64 \
                     docker-ce.x86_64 \
                     docker-ce-cli.x86_64  
  • Delete docker directly

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

Note: the above command will only delete the docker running environment, and will not delete images, containers, volume files, and user created configuration files.

Clear mirror and container files:

sudo rm -rf /var/lib/docker

Manually find and delete user created profiles.

5. Attention

5.1. Manually mount cgroup

The lower version of Redhat(6.3) may need to mount cgroup manually. The specific operation methods are as follows:

  • It is preferred to disable the cgconfig service corresponding to cggroup
 service cgconfig stop # Shut down service 
 chkconfig cgconfig off # Cancel startup
  • Then mount cgroup, which can be mounted from the command line
mount -t cgroup none /cgroup  #Valid only this time
  • Or modify the configuration file, edit / etc/fstab, and add
none                    /cgroup                 cgroup  defaults        0 0

5.2. Manage docker as a non root user

The Docker daemon is bound to a Unix socket instead of a TCP port. By default, Unix socke is available only to the root user, while other users can only use it by using sudo. The Docker daemon always runs as root.

If you don't want to add sudo to the docker command, create a Unix group named docker and add users to it. When the docker daemon starts, it creates a Unix socket that can be accessed by members of the docker group.

  • 1. Create docker group
sudo groupadd docker
  • 2. Add user to docker group
sudo usermod -aG docker $USER

Please replace $USER with a USER other than root in the system.

  • 3. Log out and log back in to reassess your group membership.

If you are testing on a virtual machine, you may need to restart the virtual machine for the changes to take effect.

  • 4. Verify that the docker command can be run without sudo.
 docker run hello-world

If you initially used sudo's Dokcer CLI command before adding users to the docker group. You may make the following errors to indicate your ~ / The docker directory was created with incorrect permissions due to the sudo command.

WARNING: Error loading config file: /home/user/.docker/config.json -
stat /home/user/.docker/config.json: permission denied

To fix this problem, delete ~ / docker / directory (it will be automatically recreated, but any custom settings will be lost), or use the following command to change its ownership and permissions:

$ sudo chown "$USER":"$USER" /home/"$USER"/.docker -R
$ sudo chmod g+rwx "$HOME/.docker" -R

5.3. Image acceleration

In view of the domestic network problems, it is very slow to pull the Docker image later. We need to configure an accelerator to solve it.

Docker domestic image:

  • Netease accelerator: http://hub-mirror.c.163.com
  • Official China accelerator: https://registry.docker-cn.com
  • Image of USTC: https://docker.mirrors.ustc.edu.cn

You can also use a private image accelerator address, such as Alibaba cloud's image accelerator: log in to Alibaba cloud - > Products - > search "container image service" - > Image Library - > Image accelerator.

The new version of Docker uses / etc / Docker / Daemon JSON (Linux) to configure Daemon.

Please add to the configuration file (if there is no such file, please create one first):

vi /etc/docker/daemon.json 
{ 
  "registry-mirrors": ["https://hub-mirror.c.163.com"] 
} 

After successful configuration, centos6 5 restart:

service docker restart

After successful configuration, CentOS7 restarts:

systemctl daemon-reload     #Restart acceleration profile
systemctl restart docker    #Restart docker background service

Keywords: Java Docker

Added by dotwebbie on Thu, 20 Jan 2022 16:02:00 +0200