Basic Environment#
System Environment:
Operating System: CentOS 7.9
Software Environment:
Docker-26.1.4, Docker Compose-2.27.1, portainer-2.20.3
Configure Aliyun yum source:
cd /etc/yum.repos.d
mv CentOS-Base.repo CentOS-Base.repo.bak
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
Configure firewall policy:
firewall-cmd --add-port=9443/tcp --permanent
sudo firewall-cmd --reload
sudo firewall-cmd --list-all
Install Docker#
(1) Install the yum-utils package (provides the yum-config-manager utility) and set up the repository.
# Install yum-utils package
yum install -y yum-utils
# Set up docker-ce repository
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
(2) Install the latest version of Docker and Docker Compose.
yum install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
(3) Change Docker's data directory to "/data/dockerData" and modify the Docker image source.
# Create Docker daemon configuration file, edit the configuration file /etc/docker/daemon.json, configure Docker data directory, and modify Docker image source
echo '{
"data-root": "/data/dockerData",
"registry-mirrors": [
"https://registry.docker-cn.com",
"http://hub-mirror.c.163.com",
"https://dockerhub.azk8s.cn",
"https://mirror.ccs.tencentyun.com",
"https://registry.cn-hangzhou.aliyuncs.com",
"https://docker.mirrors.ustc.edu.cn",
"https://docker.m.daocloud.io",
"https://noohub.ru",
"https://huecker.io",
"https://dockerhub.timeweb.cloud"
]
}' > /etc/docker/daemon.json
(4) Start the Docker service and set it to start on boot.
# Start Docker service
systemctl start docker
# Set Docker to start on boot
systemctl enable docker
# Check Docker service status
systemctl status docker
Install Portainer#
(1) Pull the Portainer image and create a container.
Method 1: Using the Docker command
docker run -d -p 9443:9443 --name portainer --restart=always -v /var/run/docker.sock:/var/run/docker.sock -v /data/portainer:/data portainer/portainer-ce:2.20.3
Method 2: Using a compose file for installation
vi portainer.yml
--------------------------------------------------
services:
portainer:
image: portainer/portainer-ce:2.20.3
container_name: portainer
restart: always
ports:
- "9443:9443"
volumes:
- /var/run/docker.sock:/var/run/docker.sock
- /data/portainer:/data
user: "0"
networks:
net:
ipv4_address: 172.20.110.10
networks:
net:
driver: bridge
ipam:
config:
- subnet: 172.20.110.0/24
--------------------------------------------------
# Run the installation
docker compose -f portainer.yml up -d
(2) Initialize Portainer and set the admin user's password.
(3) After entering the system, click "Get Started" to view the containers on the local machine, as shown in the figure below.
Multi-Instance Environment Management#
Portainer can manage multiple environments, including Docker Standalone, Docker Swarm, Kubernetes, ACI, etc.
To manage multiple Docker Standalone instances with Portainer, there are two methods: API and client container.
Method 1: Use Docker API to manage Docker Standalone instances#
(1) On other instance environments, modify the Docker service configuration file to allow the Docker daemon to listen for connections via TCP on port 2375 and via UNIX socket for local connections.
Add the -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock parameters after ExecStart.
vi /usr/lib/systemd/system/docker.service
# Modify the content as follows
[Service]
ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock
(2) Reload the configuration to make it effective.
# Reload the configuration
systemctl daemon-reload
# Restart Docker
systemctl restart docker
(3) Set the firewall policy.
# Only allow the Portainer host to access port 2375 on this host
sudo firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.22.23/32" port protocol="tcp" port="2357" accept"
(4) Click on the Environments in the Environment-related menu, click the Add environment button, and add Docker Standalone, as shown in the figure below.
(5) Select API, fill in the name and the IP and port (2375) of the Docker Standalone instance, click Connect, and after a successful connection, click Close, as shown in the figure below.
Method 2: Use agent container to manage Docker Standalone instances#
(1) On other instance environments, deploy the agent container for connection, the specific command is as follows:
docker run -d \
-p 9001:9001 \
--name portainer_agent \
--restart=always \
-v /var/run/docker.sock:/var/run/docker.sock \
-v /var/lib/docker/volumes:/var/lib/docker/volumes \
portainer/agent:2.20.3
(2) Set the firewall policy.
# Only allow the Portainer host to access port 9001 on this host
sudo firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.22.23/9001" port protocol="tcp" port="9001" accept"
After a successful connection, check the Docker Standalone instances connected by the two methods on the Home interface.