随笔记录
目录
1. 要在CentOS 7上安装Docker,操作如下:
1.1 更新系统
1.2 安装Docker依赖
1.3 添加Docker官方存储库
1.4 安装Docker引擎
1.5 启动Docker服务
1.6 验证Docker是否成功安装
1.7 设置Docker开机自启动
1.8 权限问题
2. 安装Docker-compose
2.1 准备
2.1.1 系统设置
2.2 在 /usr/local/bin 目录下下载 Docker Compose 二进制文件
2.3 将下载的二进制文件设置为可执行权限
2.4 创建一个符号链接以便于直接使用 docker-compose 命令
2.5 验证安装是否成功
3. docker-compse 常用命令
3.1 启动服务
3.2 列出项目中所有容器
3.3 停止正在运行的容器
3.4 启动、重启 容器
3.5 停止和删除容器
3.7 查看服务容器的输出
3.8 删除所有(停止状态的)服务容器
# 创建dockers 文件夹
[root@localhost magx]# pwd
/home/magx
[root@localhost magx]# mkdir Dockers
[root@localhost magx]# cd /home/magx/Dockers
# 查询yum version
[root@localhost Dockers]# yum --version
3.4.3
Installed: rpm-4.11.3-48.el7_9.x86_64 at 2023-04-21 10:31
Built : CentOS BuildSystem at 2021-11-24 16:33
Committed: Michal Domonkos at 2021-11-01
Installed: yum-3.4.3-168.el7.centos.noarch at 2023-10-10 08:57
Built : CentOS BuildSystem at 2020-10-01 17:03
Committed: CentOS Sources at 2020-09-29
Installed: yum-plugin-fastestmirror-1.1.31-54.el7_8.noarch at 2023-10-10 08:57
Built : CentOS BuildSystem at 2020-05-12 16:27
Committed: Michal Domonkos at 2020-03-12
[root@localhost Dockers]#
[root@localhost Dockers]#
# 升级系统
[root@localhost Dockers]# yum update
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: mirrors.huaweicloud.com
* epel: mirrors.bfsu.edu.cn
* extras: mirrors.huaweicloud.com
* updates: mirrors.huaweicloud.com
No packages marked for update
[root@localhost Dockers]#
#
# sudo yum install -y yum-utils device-mapper-persistent-data lvm2
[root@localhost Dockers]# yum install -y yum-utils device-mapper-persistent-data lvm2
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: mirrors.huaweicloud.com
* epel: mirrors.bfsu.edu.cn
* extras: mirrors.huaweicloud.com
* updates: mirrors.huaweicloud.com
Package yum-utils-1.1.31-54.el7_8.noarch already installed and latest version
Package device-mapper-persistent-data-0.8.5-3.el7_9.2.x86_64 already installed and latest version
Package 7:lvm2-2.02.187-6.el7_9.5.x86_64 already installed and latest version
Nothing to do
[root@localhost Dockers]#
[root@localhost Dockers]#
[root@localhost Dockers]# yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
Loaded plugins: fastestmirror, langpacks
adding repo from: https://download.docker.com/linux/centos/docker-ce.repo
grabbing file https://download.docker.com/linux/centos/docker-ce.repo to /etc/yum.repos.d/docker-ce.repo
repo saved to /etc/yum.repos.d/docker-ce.repo # 保存路径: /etc/yum.repos.d/
[root@localhost Dockers]#
[root@localhost Dockers]#
[root@localhost Dockers]#
[root@localhost Dockers]# yum install docker-ce docker-ce-cli containerd.io
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: mirrors.huaweicloud.com
* epel: mirrors.bfsu.edu.cn
* extras: mirrors.huaweicloud.com
* updates: mirrors.huaweicloud.com
docker-ce-stable | 3.5 kB 00:00:00
(1/2): docker-ce-stable/7/x86_64/primary_db | 118 kB 00:00:00
(2/2): docker-ce-stable/7/x86_64/updateinfo | 55 B 00:00:01
Resolving Dependencies
--> Running transaction check
---> Package containerd.io.x86_64 0:1.6.25-3.1.el7 will be installed
--> Processing Dependency: container-selinux >= 2:2.74 for package: containerd.io-1.6.25-3.1.el7.x86_64
---> Package docker-ce.x86_64 3:24.0.7-1.el7 will be installed
--> Processing Dependency: docker-ce-rootless-extras for package: 3:docker-ce-24.0.7-1.el7.x86_64
---> Package docker-ce-cli.x86_64 1:24.0.7-1.el7 will be installed
--> Processing Dependency: docker-buildx-plugin for package: 1:docker-ce-cli-24.0.7-1.el7.x86_64
--> Processing Dependency: docker-compose-plugin for package: 1:docker-ce-cli-24.0.7-1.el7.x86_64
--> Running transaction check
---> Package container-selinux.noarch 2:2.119.2-1.911c772.el7_8 will be installed
---> Package docker-buildx-plugin.x86_64 0:0.11.2-1.el7 will be installed
---> Package docker-ce-rootless-extras.x86_64 0:24.0.7-1.el7 will be installed
--> Processing Dependency: fuse-overlayfs >= 0.7 for package: docker-ce-rootless-extras-24.0.7-1.el7.x86_64
--> Processing Dependency: slirp4netns >= 0.4 for package: docker-ce-rootless-extras-24.0.7-1.el7.x86_64
---> Package docker-compose-plugin.x86_64 0:2.21.0-1.el7 will be installed
--> Running transaction check
---> Package fuse-overlayfs.x86_64 0:0.7.2-6.el7_8 will be installed
--> Processing Dependency: libfuse3.so.3(FUSE_3.2)(64bit) for package: fuse-overlayfs-0.7.2-6.el7_8.x86_64
--> Processing Dependency: libfuse3.so.3(FUSE_3.0)(64bit) for package: fuse-overlayfs-0.7.2-6.el7_8.x86_64
--> Processing Dependency: libfuse3.so.3()(64bit) for package: fuse-overlayfs-0.7.2-6.el7_8.x86_64
---> Package slirp4netns.x86_64 0:0.4.3-4.el7_8 will be installed
--> Running transaction check
---> Package fuse3-libs.x86_64 0:3.6.1-4.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
=========================================================================================================================================================================
Package Arch Version Repository Size
=========================================================================================================================================================================
Installing:
containerd.io x86_64 1.6.25-3.1.el7 docker-ce-stable 34 M
docker-ce x86_64 3:24.0.7-1.el7 docker-ce-stable 24 M
docker-ce-cli x86_64 1:24.0.7-1.el7 docker-ce-stable 13 M
Installing for dependencies:
container-selinux noarch 2:2.119.2-1.911c772.el7_8 extras 40 k
docker-buildx-plugin x86_64 0.11.2-1.el7 docker-ce-stable 13 M
docker-ce-rootless-extras x86_64 24.0.7-1.el7 docker-ce-stable 9.1 M
docker-compose-plugin x86_64 2.21.0-1.el7 docker-ce-stable 13 M
fuse-overlayfs x86_64 0.7.2-6.el7_8 extras 54 k
fuse3-libs x86_64 3.6.1-4.el7 extras 82 k
slirp4netns x86_64 0.4.3-4.el7_8 extras 81 k
Transaction Summary
=========================================================================================================================================================================
Install 3 Packages (+7 Dependent packages)
Total download size: 107 M
Installed size: 378 M
Is this ok [y/d/N]: y # y
Downloading packages:
(1/10): container-selinux-2.119.2-1.911c772.el7_8.noarch.rpm | 40 kB 00:00:00
warning: /var/cache/yum/x86_64/7/docker-ce-stable/packages/docker-buildx-plugin-0.11.2-1.el7.x86_64.rpm: Header V4 RSA/SHA512 Signature, key ID 621e9f35: NOKEY01:09 ETA
Public key for docker-buildx-plugin-0.11.2-1.el7.x86_64.rpm is not installed
(2/10): docker-buildx-plugin-0.11.2-1.el7.x86_64.rpm | 13 MB 00:00:13
(3/10): docker-ce-24.0.7-1.el7.x86_64.rpm | 24 MB 00:00:27
(4/10): docker-ce-cli-24.0.7-1.el7.x86_64.rpm | 13 MB 00:00:15
(5/10): docker-ce-rootless-extras-24.0.7-1.el7.x86_64.rpm | 9.1 MB 00:00:09
(6/10): fuse3-libs-3.6.1-4.el7.x86_64.rpm | 82 kB 00:00:00
(7/10): fuse-overlayfs-0.7.2-6.el7_8.x86_64.rpm | 54 kB 00:00:00
(8/10): slirp4netns-0.4.3-4.el7_8.x86_64.rpm | 81 kB 00:00:00
(9/10): docker-compose-plugin-2.21.0-1.el7.x86_64.rpm | 13 MB 00:00:13
(10/10): containerd.io-1.6.25-3.1.el7.x86_64.rpm | 34 MB 00:01:39
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total 1.1 MB/s | 107 MB 00:01:39
Retrieving key from https://download.docker.com/linux/centos/gpg
Importing GPG key 0x621E9F35:
Userid : "Docker Release (CE rpm) "
Fingerprint: 060a 61c5 1b55 8a7f 742b 77aa c52f eb6b 621e 9f35
From : https://download.docker.com/linux/centos/gpg
Is this ok [y/N]: y # y
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : 2:container-selinux-2.119.2-1.911c772.el7_8.noarch 1/10
Installing : containerd.io-1.6.25-3.1.el7.x86_64 2/10
Installing : docker-buildx-plugin-0.11.2-1.el7.x86_64 3/10
Installing : fuse3-libs-3.6.1-4.el7.x86_64 4/10
Installing : fuse-overlayfs-0.7.2-6.el7_8.x86_64 5/10
Installing : slirp4netns-0.4.3-4.el7_8.x86_64 6/10
Installing : docker-compose-plugin-2.21.0-1.el7.x86_64 7/10
Installing : 1:docker-ce-cli-24.0.7-1.el7.x86_64 8/10
Installing : docker-ce-rootless-extras-24.0.7-1.el7.x86_64 9/10
Installing : 3:docker-ce-24.0.7-1.el7.x86_64 10/10
Verifying : containerd.io-1.6.25-3.1.el7.x86_64 1/10
Verifying : 3:docker-ce-24.0.7-1.el7.x86_64 2/10
Verifying : docker-ce-rootless-extras-24.0.7-1.el7.x86_64 3/10
Verifying : docker-compose-plugin-2.21.0-1.el7.x86_64 4/10
Verifying : slirp4netns-0.4.3-4.el7_8.x86_64 5/10
Verifying : 2:container-selinux-2.119.2-1.911c772.el7_8.noarch 6/10
Verifying : 1:docker-ce-cli-24.0.7-1.el7.x86_64 7/10
Verifying : fuse3-libs-3.6.1-4.el7.x86_64 8/10
Verifying : docker-buildx-plugin-0.11.2-1.el7.x86_64 9/10
Verifying : fuse-overlayfs-0.7.2-6.el7_8.x86_64 10/10
Installed:
containerd.io.x86_64 0:1.6.25-3.1.el7 docker-ce.x86_64 3:24.0.7-1.el7 docker-ce-cli.x86_64 1:24.0.7-1.el7
Dependency Installed:
container-selinux.noarch 2:2.119.2-1.911c772.el7_8 docker-buildx-plugin.x86_64 0:0.11.2-1.el7 docker-ce-rootless-extras.x86_64 0:24.0.7-1.el7
docker-compose-plugin.x86_64 0:2.21.0-1.el7 fuse-overlayfs.x86_64 0:0.7.2-6.el7_8 fuse3-libs.x86_64 0:3.6.1-4.el7
slirp4netns.x86_64 0:0.4.3-4.el7_8
Complete!
[root@localhost Dockers]#
# 安装完成后,检查docker 文件夹
[root@localhost Dockers]# ll
total 0
-rw-r--r--. 1 root root 0 Dec 4 10:21 =
-rw-r--r--. 1 root root 0 Dec 4 10:21 Finished
-rw-r--r--. 1 root root 0 Dec 4 10:21 Package
-rw-r--r--. 1 root root 0 Dec 4 10:21 Processing
-rw-r--r--. 1 root root 0 Dec 4 10:21 Running
[root@localhost Dockers]#
[root@localhost Dockers]#
# 启动docker
[root@localhost Dockers]#
[root@localhost Dockers]# systemctl start docker
# 查看docker服务 是否运行
[root@localhost Dockers]#
[root@localhost Dockers]# ps -aux|grep docker*
root 104955 5.0 0.1 3500780 63228 ? Ssl 10:23 0:00 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
root 105245 0.0 0.0 112812 992 pts/4 S+ 10:24 0:00 grep --color=auto docker*
[root@localhost Dockers]#
# 停止 docker服务
[root@localhost Dockers]# systemctl stop docker
Warning: Stopping docker.service, but it can still be activated by:
docker.socket
[root@localhost Dockers]#
# 查看docker服务 是否停止运行
[root@localhost Dockers]# ps -aux|grep docker*
root 105646 0.0 0.0 112812 992 pts/4 S+ 10:25 0:00 grep --color=auto docker*
[root@localhost Dockers]#
# sudo docker run hello-world
# docker服务 已运行
[root@localhost Dockers]# ps -aux|grep docker*
root 108747 2.8 0.2 3459032 67708 ? Ssl 10:31 0:00 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
root 109224 0.0 0.0 112812 992 pts/4 S+ 10:31 0:00 grep --color=auto docker*
[root@localhost Dockers]#
# 验证Docker 是否安装成功
# 如果一切顺利,将看到一个测试容器成功运行的消息
[root@localhost Dockers]#
[root@localhost Dockers]# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
719385e32844: Pull complete
Digest: sha256:c79d06dfdfd3d3eb04cafd0dc2bacab0992ebc243e083cabe208bac4dd7759e0
Status: Downloaded newer image for hello-world:latest
Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64)
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/
For more examples and ideas, visit:
https://docs.docker.com/get-started/
[root@localhost Dockers]#
# 设置开机自启动Docker服务
# sudo systemctl enable docker
[root@localhost Dockers]# systemctl enable docker
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
[root@localhost Dockers]#
[root@localhost Dockers]#
现在,你已经成功在CentOS 7上安装了Docker。你可以使用
docker
命令来管理和运行容器了。 请注意,执行Docker命令可能需要使用sudo
来获取足够的权限。如果你想允许普通用户执行Docker命令,你可以将该用户添加到docker
用户组中Note: 设置完成后,请注销并重新登录以使更改生效
# sudo usermod -aG docker your_username
[root@localhost Dockers]# usermod -ag docker magx # -aG 失误错写成 -ag
usermod: -a flag is only allowed with the -G flag
Usage: usermod [options] LOGIN
Options:
-c, --comment COMMENT new value of the GECOS field
-d, --home HOME_DIR new home directory for the user account
-e, --expiredate EXPIRE_DATE set account expiration date to EXPIRE_DATE
-f, --inactive INACTIVE set password inactive after expiration
to INACTIVE
-g, --gid GROUP force use GROUP as new primary group
-G, --groups GROUPS new list of supplementary GROUPS
-a, --append append the user to the supplemental GROUPS
mentioned by the -G option without removing
the user from other groups
-h, --help display this help message and exit
-l, --login NEW_LOGIN new value of the login name
-L, --lock lock the user account
-m, --move-home move contents of the home directory to the
new location (use only with -d)
-o, --non-unique allow using duplicate (non-unique) UID
-p, --password PASSWORD use encrypted password for the new password
-R, --root CHROOT_DIR directory to chroot into
-P, --prefix PREFIX_DIR prefix directory where are located the /etc/* files
-s, --shell SHELL new login shell for the user account
-u, --uid UID new UID for the user account
-U, --unlock unlock the user account
-v, --add-subuids FIRST-LAST add range of subordinate uids
-V, --del-subuids FIRST-LAST remove range of subordinate uids
-w, --add-subgids FIRST-LAST add range of subordinate gids
-W, --del-subgids FIRST-LAST remove range of subordinate gids
-Z, --selinux-user SEUSER new SELinux user mapping for the user account
[root@localhost Dockers]#
[root@localhost Dockers]# usermod -aG docker magx
[root@localhost Dockers]#
Note: 请注销并重新登录以使更改生效
Docker-Compose是基于Docker的编排工具,需要有Docker环境
1、Docker-Compose只能管理当前计算机上的 Docker容器,不能跨计算机管理Docker容器;
2、一个项目可以包括多个容器实例,但Docker-Compose无法实现多容器间负载均衡,若要实现此功能,则需要借助诸如Nginx等工具。
设置允许数据包转发
注意:
Linux默认不允许数据包转发,此时执行docker run时出现如下警告,将导致无法远程访问该docker容器
...省略...
WARNING: IPv4 forwarding is disabled. Networking will not work.
# 设置允许数据包转发
# cat >> /usr/lib/sysctl.d/50-default.conf << EOF
net.ipv4.ip_forward = 1
EOF
# cat /usr/lib/sysctl.d/50-default.conf
# systemctl restart network
1. # 设置允许数据包转发前 查看 /usr/lib/sysctl.d/50-default.conf
[root@localhost Dockers]#
[root@localhost Dockers]# cat /usr/lib/sysctl.d/50-default.conf
# This file is part of systemd.
#
# systemd is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
# See sysctl.d(5) and core(5) for for documentation.
# To override settings in this file, create a local file in /etc
# (e.g. /etc/sysctl.d/90-override.conf), and put any assignments
# there.
# System Request functionality of the kernel (SYNC)
#
# Use kernel.sysrq = 1 to allow all keys.
# See http://fedoraproject.org/wiki/QA/Sysrq for a list of values and keys.
kernel.sysrq = 16
# Append the PID to the core filename
kernel.core_uses_pid = 1
# https://bugzilla.redhat.com/show_bug.cgi?id=1689344
kernel.kptr_restrict = 1
# Source route verification
net.ipv4.conf.default.rp_filter = 1
net.ipv4.conf.all.rp_filter = 1
# Do not accept source routing
net.ipv4.conf.default.accept_source_route = 0
net.ipv4.conf.all.accept_source_route = 0
# Promote secondary addresses when the primary address is removed
net.ipv4.conf.default.promote_secondaries = 1
net.ipv4.conf.all.promote_secondaries = 1
# Enable hard and soft link protection
fs.protected_hardlinks = 1
fs.protected_symlinks = 1
[root@localhost Dockers]#
2. 将net.ipv4.ip_forward=1 追加写入 /usr/lib/sysctl.d/50-default.conf
[root@localhost Dockers]# cat >> /usr/lib/sysctl.d/50-default.conf << EOF
> net.ipv4.ip_forward=1
> EOF
[root@localhost Dockers]#
3.设置允许数据包转发后 查看 /usr/lib/sysctl.d/50-default.conf
# cat /usr/lib/sysctl.d/50-default.conf
[root@localhost Dockers]# cat /usr/lib/sysctl.d/50-default.conf
# This file is part of systemd.
#
# systemd is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
# See sysctl.d(5) and core(5) for for documentation.
# To override settings in this file, create a local file in /etc
# (e.g. /etc/sysctl.d/90-override.conf), and put any assignments
# there.
# System Request functionality of the kernel (SYNC)
#
# Use kernel.sysrq = 1 to allow all keys.
# See http://fedoraproject.org/wiki/QA/Sysrq for a list of values and keys.
kernel.sysrq = 16
# Append the PID to the core filename
kernel.core_uses_pid = 1
# https://bugzilla.redhat.com/show_bug.cgi?id=1689344
kernel.kptr_restrict = 1
# Source route verification
net.ipv4.conf.default.rp_filter = 1
net.ipv4.conf.all.rp_filter = 1
# Do not accept source routing
net.ipv4.conf.default.accept_source_route = 0
net.ipv4.conf.all.accept_source_route = 0
# Promote secondary addresses when the primary address is removed
net.ipv4.conf.default.promote_secondaries = 1
net.ipv4.conf.all.promote_secondaries = 1
# Enable hard and soft link protection
fs.protected_hardlinks = 1
fs.protected_symlinks = 1
net.ipv4.ip_forward=1 #追加写入
[root@localhost Dockers]#
4. 重启网卡
# systemctl restart network
[root@localhost Dockers]# systemctl restart network
[root@localhost Dockers]#
/usr/local/bin
目录下下载 Docker Compose 二进制文件# sudo curl -L "https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
[root@localhost Dockers]# cd /usr/local/bin/
[root@localhost bin]#
[root@localhost bin]# ll
total 0
[root@localhost bin]# pwd
/usr/local/bin
[root@localhost bin]#
[root@localhost bin]# curl -L "https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
0 0 0 0 0 0 0 0 --:--:-- 0:00:01 --:--:-- 0
100 56.9M 100 56.9M 0 0 54375 0 0:18:17 0:18:17 --:--:-- 53536
[root@localhost bin]#
[root@localhost bin]# ll
total 58304
-rw-r--r--. 1 root root 59699273 Dec 4 11:28 docker-compose
[root@localhost bin]#
[root@localhost bin]# chmod +x /usr/local/bin/docker-compose # 添加可执行权限
[root@localhost bin]#
[root@localhost bin]# ll
total 58304
-rwxr-xr-x. 1 root root 59699273 Dec 4 11:28 docker-compose
[root@localhost bin]#
[root@localhost bin]#
docker-compose
命令[root@localhost bin]#
[root@localhost bin]# ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
[root@localhost bin]#
# 验证docker-compose 是否安装成功
[root@localhost bin]# docker-compose --version
Docker Compose version v2.23.3
[root@localhost bin]#
启动服务
# docker-compose up
【选项】
-d 在后台运行服务容器;
-f 指定使用的Compose模板文件,默认为docker-compose.yml,可以多次指定;
列出项目中所有容器
# docker-compose ps
停止正在运行的容器
# docker-compose stop
启动容器
# docker-compose start
重启项目中的服务
# docker-compose restart
停止和删除容器、网络、卷、镜像;
# docker-compose down
[选项]
–rmi type:删除镜像,type必须为all或local:all,删除compose文件中定义的所有镜像;local,删除镜像名为空的镜像;
-v, –volumes:删除已经在compose文件中定义的和匿名的附在容器上的数据卷;
# 查看服务容器的输出
# docker-compose logs
# 删除所有(停止状态的)服务容器:
# docker-compose rm
[选项]
–f:强制直接删除,包括非停止状态的容器;
-v:删除容器所挂载的数据卷;