Installation tutorial of Docker and Docker Compose under CentOS

Docker is an open source application container engine that allows developers to package their applications and dependencies into a portable container and then publish them to any popular Linux machines can also be virtualized. Containers completely use the sandbox mechanism and will not have any interfaces with each other.

Docker Compose is a tool for defining and running multi-container docker applications. compose manages multiple docker container . can use docker-compose.yml script to Start, stop, and restart applications, and arrange and manage docker containers . but docker compose does not implement container load balancing , and other tools are needed to achieve it.

The following takes the CentOS system as an example to introduce how to install Docker and Docker Compose.

1. Install Docker

(1 Introduction

Docker requiresCentOS that the system’s kernel version is higher than 3.10, we first check the current kernel version through the uname -r command to see if it meets the conditions:

(2) Upgrade software

Execute the following command to update the yum package to the latest version.

yum update -y

(3) Depends on tool installation

Execute the following command to install the required software packages.

yum install -y yum-utils device-mapper-persistent-data lvm2

(4) Set up yum source

Execute the following command to set the yum source.

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

(5) View available docker versions

Execute the following command to view all docker versions in all current warehouses.

yum list docker-ce --showduplicates | sort -r

(6) Install the latest version of docker

Here we execute the following command to install the latest stable version based on the above results 24.0.5-1.el7

yum install docker-ce-24.0.5-1.el7 -y
docker version

(7) Set up docker to start up

Execute the following two commands respectively to start docker and add it to boot.

systemctl start docker
systemctl enable docker

(8) Installation verification

Executedocker version Verify whether the installation is successful (as long as there isclient< a i=4> and service means that docker is installed and started. succeeded).

(9) Modify the Docker Hub image address

$. vi /etc/sysconfig/docker
OPTIONS='--selinux-enabled --log-driver=journald --signature-verification=false'
#修改为:
OPTIONS='--selinux-enabled --log-driver=journald --signature-verification=false --registry-mirror=https://docker.mirrors.ustc.edu.cn'

Other docker configurations:

vi /etc/docker/daemon.json
# 在配置文件中添加以下内容:  
"hosts": ["tcp://0.0.0.0:2375", "unix:///var/run/docker.sock"]
# 例如:
{
  // 开放docker端口,允许远程访问:
  "hosts": ["tcp://0.0.0.0:2375", "unix:///var/run/docker.sock"],
  // 修改默认镜像地址
  "registry-mirrors": ["https://k728i8z5.mirror.aliyuncs.com"],
  // 允许以http方式访问下面镜像库
  "insecure-registries":["k8s-harbor:10001"]
}

Restart docker

systemctl restart docker

Attachment: Upgrade Docker version

(1) If the docker version of our host is relatively low and we want to upgrade to the latest version. First execute the following command to delete the old version.

yum remove docker docker-common docker-selinux docker-engine

(2) After deletion, follow step 5 above and then follow the specified version again. After the main installation, it must also be added to auto-start at boot.

2. Install Docker Compose

Installation method one (use)

docker official website address:Overview | Docker Documentation

1. Check the local docker version

docker version

2. Docker-compose version selection

Select the corresponding docker-compose version according to the docker version.

docker-compose官网地址:Compose file version 3 reference | Docker Documentation

3. Installation

Official website installation address:Overview | Docker Documentation

# github: https://github.com/docker/compose/releases/tag/v2.20.2 
# 国内下载地址:https://gitee.com/smilezgy/compose/releases/tag/v2.20.2
curl -SL \
https://github.com/docker/compose/releases/download/v2.20.2/docker-compose-linux-x86_64 \
-o /usr/local/bin/docker-compose

# 或者手动下载, 上传到服务器后执行如下指令(use)
# 在 docker-compose-linux-x86_64 文件同一目录下执行
cp docker-compose-linux-x86_64 /usr/local/bin/docker-compose

4. Add executable permissions

chmod +x /usr/local/bin/docker-compose

5. Test

[root@bogon bin]# docker-compose --version
Docker Compose version v2.20.2

If you need to delete it, execute the following command

rm -rf /usr/local/bin/docker-compose

6. Docker Compose running project

To run Docker Compose, you need to have a docker-compose.yml file in the project directory. Complete the following steps:

  1. Open a terminal or command prompt.
  2. Navigate to the directory containing the docker-compose.yml file.
  3. Run the following command to start the container defined in the compose file:
docker-compose up
By default, this command will start all services specified in the compose file and display their logs in the terminal.
To run the container in detached mode (in the background), you can add -d flag:
# 此命令会启动容器并将控制返回给终端。
docker-compose up -d
Please note that if this is the first time you run docker-compose up , which will pull any necessary Docker images from Docker Hub before starting the container.

Installation method two

(1) Execute the following command to install pip3:

yum -y install python3-pip
pip3 install --upgrade pip -i https://pypi.tuna.tsinghua.edu.cn/simple

(2) Execute the following command to install docker-compose:

pip3 install docker-compose -i https://pypi.tuna.tsinghua.edu.cn/simple

(3) After installation, execute the following command to check the version:

docker-compose version

(4) If the console displays the following, the installation is successful:

Guess you like

Origin blog.csdn.net/justlpf/article/details/131973134