Install Docker on a virtual machine

Install Docker

Docker is divided into two major versions: CE and EE. CE is the community edition (free, support period is 7 months), EE is the enterprise edition, which emphasizes security, is paid for use, and has a support period of 24 months.

Docker CE is divided into three update channels: stable test and nightly.

The official website has installation guides for various environments. Here we mainly introduce the installation of Docker CE on CentOS.

1.CentOS installation Docker

Docker CE supports the 64-bit version of CentOS 7, and requires the kernel version to be no less than 3.10. CentOS 7 meets the minimum kernel requirements, so we install Docker on CentOS 7. See official CentOS installation Docker: click to jump

1.1. Uninstall (optional)

If you have installed an older version of Docker before, you can uninstall it using the following command:

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

1.2. Install docker

First, everyone needs to connect the virtual machine to the Internet and install the yum tool.

yum install -y yum-utils \
device-mapper-persistent-data \
lvm2 --skip-broken

Command analysis:
This is a yum command in CentOS/RHEL system, its meaning is as follows:
yum install: Install the specified software package.
-y: Automatically confirm the installation to avoid the need for manual confirmation.
yum-utils: Install the yum-utils software package to manage the yum repository.
device-mapper-persistent-data: Install the device-mapper-persistent-data software package for LVM persistent storage.
lvm2: Install the lvm2 software package for logical volume management.
–skip-broken: Skip uninstallable packages.
To sum up, the function of this command is to install the yum-utils, device-mapper-persistent-data and lvm2 software packages in the CentOS/RHEL system and skip the software packages that cannot be installed. Among them, yum-utils is used to manage the yum warehouse, device-mapper-persistent-data is used for LVM persistent storage, and lvm2 is used for logical volume management.

Then update the local mirror source:

# Set docker image source
yum-config-manager \
    --add-repo \
    https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
    
sed -i 's/download.docker.com/mirrors.aliyun.com\/docker-ce/g' /etc/yum.repos.d/docker-ce.repo

yum makecache fast

Then enter the command:

yum install -y docker-ce
or yum install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

docker-ce is a community free version. Wait a moment and docker will be installed successfully.

1.3. Start docker

Docker applications need to use various ports and modify the firewall settings one by one. It’s very troublesome, so I suggest you turn off the firewall directly!

Before starting docker, be sure to turn off the firewall! !

# Close
systemctl stop firewalld
# Disable starting the firewall at boot
systemctl disable firewalld

Start docker via command:

systemctl start docker # Start docker service

systemctl stop docker # Stop docker service

systemctl restart docker # Restart docker service

systemctl enable docker # Set up auto-start at boot

docker ps # Execute the docker ps command. If no error is reported, the installation is started successfully.

Then enter the command to view the docker version:

docker -v

1.4. Configure image acceleration

The Internet speed of docker’s official image warehouse is poor. We need to set up a domestic image service:

Refer to Alibaba Cloud’s mirror acceleration documentation: https://cr.console.aliyun.com/cn-hangzhou/instances/mirrors

1.4.1 Activate mirror service

First visit the Alibaba Cloud website: https://www.aliyun.com/

Among the products on the homepage, find Alibaba Cloud’s container image service:

Click to enter the console:


For the first time, you may need to choose to activate immediately and then enter the console.

1.4.2 Configure image acceleration

Find the image accelerator under the image tool:


Scroll down the page to find the configuration documentation:


The specific commands are as follows:

# Create directory
mkdir -p /etc/docker

# Copy the content, be sure to change the mirror acceleration address to your own
tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://xxxx.mirror.aliyuncs.com"]
}
EOF

# Reload configuration
systemctl daemon-reload

# Restart Docker
systemctl restart docker

2.CentOS7 installation DockerCompose

2.1.Download

Under Linux, you need to download it through the command:

# Installation
curl -L https://github.com/docker/compose/releases/download/1.23.1/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose

Provided docker-compose file: click to download

You can also upload it to the /usr/local/bin/ directory.

2.2. Modify file permissions

Modify file permissions:

# Modify permissions
chmod +x /usr/local/bin/docker-compose

2.3.Base auto-completion command:

# Complete command
curl -L https://raw.githubusercontent.com/docker/compose/1.29.1/contrib/completion/bash/docker-compose > /etc/bash_completion.d/docker-compose

If an error occurs here, you need to modify your hosts file:

echo "199.232.68.133 raw.githubusercontent.com" >> /etc/hosts

3.Docker image warehouse

Building a mirror warehouse can be achieved based on the DockerRegistry officially provided by Docker.

Official website address: https://hub.docker.com/_/registry

3.1. Simplified version of the mirror warehouse

Docker’s official Docker Registry is a basic version of the Docker image warehouse, which has complete functions of warehouse management, but does not have a graphical interface.

The construction method is relatively simple, the commands are as follows:

docker run -d \
    --restart=always \
    --name registry \
    -p 5000:5000 \
    -v registry-data:/var/lib/registry \
    registry

The command mounts a data volume registry-data to the /var/lib/registry directory in the container, which is the directory where the private image library stores data.

Visit http://YourIp:5000/v2/_catalog to view the images included in the current private image service

3.2. Version with graphical interface

Use DockerCompose to deploy DockerRegistry with a graphical interface. The command is as follows:

version: '3.0'
services:
  registry:
    image: registry
    volumes:
      - ./registry-data:/var/lib/registry
  ui:
    image: joxit/docker-registry-ui:static
    ports:
      - 8080:80
    environment:
      - REGISTRY_TITLE= Chuanzhi Education Private Warehouse
      - REGISTRY_URL=http://registry:5000
    depends_on:
      -registry

3.3. Configure Docker trust address

Our private server uses the http protocol, which is not trusted by Docker by default, so we need to make a configuration:

# Open the file to be modified
vi /etc/docker/daemon.json
#Add content:
"insecure-registries":["http://192.168.150.101:8080"]
# reload
systemctl daemon-reload
# Restart docker
systemctl restart docker