Docker教程FG002-Docker安装与环境配置(Windows/Linux/Mac)实战
本教程基于Docker官方文档的安装与配置部分,详细解析Docker在Windows、Linux和Mac三大平台的安装方法、环境配置以及常见问题解决。通过理论与实战相结合的方式,帮助读者快速完成Docker环境的搭建,为后续的容器化应用部署奠定基础。
风哥教程参考Docker官方文档Docker安装指南、Docker环境配置手册等相关内容。
目录大纲
Part01-基础概念与理论知识
1.1 Docker安装概述
Docker提供了多种安装方式,适用于不同的操作系统平台:
- Linux平台:通过包管理器(如apt、yum)安装Docker Engine
- Windows平台:安装Docker Desktop for Windows
- Mac平台:安装Docker Desktop for Mac
Docker的安装类型:
- Docker Engine:核心运行引擎,适用于Linux服务器
- Docker Desktop:包含Docker Engine、Docker CLI、Docker Compose等工具的完整套装,适用于开发环境
更多视频教程www.fgedu.net.cn
1.2 各平台安装要求
Linux平台要求:
- 操作系统:Ubuntu 20.04+、Debian 10+、CentOS 7+、RHEL 7+、Oracle Linux 7+
- 内核版本:3.10+
- CPU:至少2核心
- 内存:至少4GB
- 存储:至少10GB
Windows平台要求:
- 操作系统:Windows 10 64位(专业版、企业版或教育版),版本1903+
- 启用Hyper-V或WSL 2
- CPU:支持虚拟化
- 内存:至少4GB
- 存储:至少20GB
Mac平台要求:
- 操作系统:macOS 10.15+(Catalina或更高版本)
- CPU:Intel或Apple Silicon芯片
- 内存:至少4GB
- 存储:至少20GB
学习交流加群风哥微信: itpux-com
Part02-生产环境规划与建议
2.1 系统硬件要求
风哥提示:生产环境中Docker的硬件要求应根据实际应用负载进行规划,以下为基本建议配置。
- CPU:至少4核心,推荐8核心以上
- 内存:至少8GB,推荐16GB以上
- 存储:至少100GB SSD,推荐200GB以上
- 网络:千兆网络,推荐万兆网络
检查Linux系统硬件信息:
$ nproc
8
# 检查内存大小
$ free -h
total used free shared buff/cache available
Mem: 16G 2.3G 12G 128M 1.7G 13G
Swap: 4G 0B 4G
# 检查磁盘空间
$ df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 200G 20G 180G 10% /
学习交流加群风哥QQ113257174
2.2 网络与存储规划
网络规划:
- 为Docker容器分配独立的网络段
- 配置防火墙规则,允许Docker相关端口
- 考虑使用Docker网络插件,如Calico、Flannel等
存储规划:
- 使用SSD存储提高容器IO性能
- 配置数据卷(Volume)进行持久化存储
- 考虑使用存储插件,如Ceph、NFS等
更多学习教程公众号风哥教程itpux_com
Part03-生产环境项目实施方案
3.1 Linux平台安装与配置
以Oracle Linux 9.3为例,安装Docker Engine:
$ sudo dnf update -y
[sudo] password for fgedu:
Last metadata expiration check: 1:00:00 ago on Mon Apr 10 10:00:00 2026.
Dependencies resolved.
================================================================================
Package Architecture Version Repository Size
================================================================================
Upgrading:
kernel x86_64 5.15.0-100.0.0.0.1 ol9_baseos_latest 120 M
kernel-core x86_64 5.15.0-100.0.0.0.1 ol9_baseos_latest 400 M
kernel-modules x86_64 5.15.0-100.0.0.0.1 ol9_baseos_latest 280 M
…
Complete!
# 安装必要的依赖包
$ sudo dnf install -y yum-utils device-mapper-persistent-data lvm2
Last metadata expiration check: 0:10:00 ago on Mon Apr 10 10:10:00 2026.
Dependencies resolved.
================================================================================
Package Architecture Version Repository Size
================================================================================
Installing:
device-mapper-persistent-data x86_64 0.9.0-11.el9 ol9_baseos_latest 44 k
lvm2 x86_64 2.03.11-6.el9 ol9_baseos_latest 1.2 M
yum-utils noarch 4.3.0-5.el9 ol9_baseos_latest 69 k
…
Complete!
# 配置Docker仓库
$ sudo yum-config-manager –add-repo https://download.docker.com/linux/centos/docker-ce.repo
Adding repo from: https://download.docker.com/linux/centos/docker-ce.repo
# 安装Docker CE
$ sudo dnf install -y docker-ce docker-ce-cli containerd.io
Last metadata expiration check: 0:05:00 ago on Mon Apr 10 10:15:00 2026.
Dependencies resolved.
================================================================================
Package Architecture Version Repository Size
================================================================================
Installing:
containerd.io x86_64 1.6.28-3.1.el9 docker-ce-stable 34 M
docker-ce x86_64 24.0.9-1.el9 docker-ce-stable 24 M
docker-ce-cli x86_64 24.0.9-1.el9 docker-ce-stable 14 M
…
Complete!
# 启动Docker服务
$ sudo systemctl start docker
# 设置Docker开机自启
$ sudo systemctl enable docker
Created symlink /etc/systemd/system/multi-user.target.wants/docker.service → /usr/lib/systemd/system/docker.service.
# 验证Docker安装
$ docker –version
Docker version 24.0.9, build 2936816
from Docker视频:www.itpux.com
3.2 Windows平台安装与配置
安装Docker Desktop for Windows:
- 访问Docker官方网站,下载Docker Desktop安装包
- 运行安装程序,按照向导完成安装
- 启用WSL 2功能(如果未启用)
- 启动Docker Desktop并登录Docker Hub账号
验证Windows平台Docker安装:
# 验证Docker版本
PS C:\Users\fgedu> docker –version
Docker version 24.0.9, build 2936816
# 运行Hello World容器
PS C:\Users\fgedu> docker run hello-world
Unable to find image ‘hello-world:latest’ locally
latest: Pulling from library/hello-world
719385e32844: Pull complete
Digest: sha256:8914eb54f464c411294a4815933756cc5786ff9aef9a059f9615279016513106
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/
3.3 Mac平台安装与配置
安装Docker Desktop for Mac:
- 访问Docker官方网站,下载Docker Desktop安装包(.dmg文件)
- 双击.dmg文件,将Docker图标拖到Applications文件夹
- 从Applications文件夹启动Docker Desktop
- 登录Docker Hub账号
验证Mac平台Docker安装:
# 验证Docker版本
$ docker –version
Docker version 24.0.9, build 2936816
# 运行Hello World容器
$ docker run hello-world
Unable to find image ‘hello-world:latest’ locally
latest: Pulling from library/hello-world
719385e32844: Pull complete
Digest: sha256:8914eb54f464c411294a4815933756cc5786ff9aef9a059f9615279016513106
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/
Part04-生产案例与实战讲解
4.1 安装验证与基本操作
验证Docker安装并进行基本操作:
$ docker info
Client:
Context: default
Debug Mode: false
Plugins:
buildx: Docker Buildx (Docker Inc., v0.11.2)
compose: Docker Compose (Docker Inc., v2.20.2)
dev: Docker Dev Environments (Docker Inc., v0.1.0)
extension: Manages Docker extensions (Docker Inc., v0.2.20)
sbom: View the packaged-based Software Bill Of Materials (SBOM) for an image (Anchore Inc., 0.6.0)
scan: Docker Scan (Docker Inc., v0.25.0)
Server:
Containers: 0
Running: 0
Paused: 0
Stopped: 0
Images: 1
Server Version: 24.0.9
Storage Driver: overlay2
Backing Filesystem: xfs
Supports d_type: true
Using metacopy: false
Native Overlay Diff: true
userxattr: false
Logging Driver: json-file
Cgroup Driver: cgroupfs
Cgroup Version: 1
Plugins:
Volume: local
Network: bridge host ipvlan macvlan null overlay
Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
Swarm: inactive
Runtimes: io.containerd.runc.v2 runc
Default Runtime: runc
Init Binary: docker-init
containerd version: 1e1ea6e986c6c86565bc33d52e34b81b3e2bc71f
runc version: v1.1.7-0-g860f061
init version: de40ad0
Security Options:
seccomp
Profile: builtin
Kernel Version: 5.15.0-100.0.0.0.1.el9.x86_64
Operating System: Oracle Linux Server 9.3
OSType: linux
Architecture: x86_64
CPUs: 8
Total Memory: 15.56GiB
Name: fgedu.net.cn
ID: 12345678-1234-1234-1234-1234567890ab
Docker Root Dir: /var/lib/docker
Debug Mode: false
Registry: https://index.docker.io/v1/
Labels:
Experimental: false
Insecure Registries:
127.0.0.0/8
Live Restore Enabled: false
Product License: Community Engine
# 运行Ubuntu容器
$ docker run -it –name fgedu-container ubuntu:22.04 /bin/bash
root@fgedu-container:/# ls -la
total 64
drwxr-xr-x 1 root root 4096 Apr 10 10:25 .
drwxr-xr-x 1 root root 4096 Apr 10 10:25 ..
-rwxr-xr-x 1 root root 0 Apr 10 10:25 .dockerenv
drwxr-xr-x 2 root root 4096 Mar 28 00:30 bin
drwxr-xr-x 2 root root 4096 Mar 28 00:30 boot
drwxr-xr-x 5 root root 360 Apr 10 10:25 dev
drwxr-xr-x 1 root root 4096 Apr 10 10:25 etc
drwxr-xr-x 2 root root 4096 Mar 28 00:30 home
drwxr-xr-x 8 root root 4096 Mar 28 00:30 lib
drwxr-xr-x 2 root root 4096 Mar 28 00:30 media
drwxr-xr-x 2 root root 4096 Mar 28 00:30 mnt
drwxr-xr-x 2 root root 4096 Mar 28 00:30 opt
drwxr-xr-x 2 root root 4096 Mar 28 00:30 proc
drwx—— 2 root root 4096 Mar 28 00:30 root
drwxr-xr-x 2 root root 4096 Mar 28 00:30 run
drwxr-xr-x 2 root root 4096 Mar 28 00:30 sbin
drwxr-xr-x 2 root root 4096 Mar 28 00:30 srv
drwxr-xr-x 2 root root 4096 Mar 28 00:30 sys
drwxrwxrwt 2 root root 4096 Mar 28 00:30 tmp
drwxr-xr-x 11 root root 4096 Mar 28 00:30 usr
drwxr-xr-x 11 root root 4096 Mar 28 00:30 var
root@fgedu-container:/# exit
exit
更多视频教程www.fgedu.net.cn
4.2 环境配置优化
配置Docker镜像加速器,提高镜像拉取速度:
$ sudo vi /etc/docker/daemon.json
# 添加以下内容(使用阿里云镜像加速器)
{
“registry-mirrors”: [“https://xxxxxxxx.mirror.aliyuncs.com”]
}
# 重启Docker服务
$ sudo systemctl restart docker
# 验证配置是否生效
$ docker info | grep Registry
Registry: https://index.docker.io/v1/
Insecure Registries:
127.0.0.0/8
Registry Mirrors:
https://xxxxxxxx.mirror.aliyuncs.com/
配置Docker存储驱动:
$ sudo vi /etc/docker/daemon.json
# 添加存储驱动配置
{
“storage-driver”: “overlay2”,
“registry-mirrors”: [“https://xxxxxxxx.mirror.aliyuncs.com”]
}
# 重启Docker服务
$ sudo systemctl restart docker
# 验证存储驱动
$ docker info | grep Storage
Storage Driver: overlay2
学习交流加群风哥微信: itpux-com
Part05-风哥经验总结与分享
5.1 安装常见问题解决
风哥提示:在安装Docker过程中,可能会遇到各种问题,以下是常见问题及解决方案。
- Linux平台:Docker服务启动失败:
- 检查内核版本是否满足要求:
uname -r - 检查Docker日志:
sudo journalctl -u docker.service - 检查selinux设置:
sudo getenforce,如为Enforcing,可临时设置为Permissive:sudo setenforce 0
- 检查内核版本是否满足要求:
- Windows平台:WSL 2 installation is incomplete:
- 下载并安装WSL 2 Linux内核更新包
- 设置WSL默认版本为2:
wsl --set-default-version 2
- Mac平台:Docker Desktop无法启动:
- 检查系统版本是否满足要求
- 重置Docker Desktop:点击Docker图标 → Troubleshoot → Reset to factory defaults
- 镜像拉取缓慢:
- 配置镜像加速器
- 使用国内镜像源,如阿里云、网易云等
学习交流加群风哥QQ113257174
5.2 最佳实践建议
- 安装建议:
- 使用官方推荐的安装方法,避免使用第三方源
- 定期更新Docker版本,获取安全补丁和新功能
- 在生产环境中,使用稳定版本而非边缘版本
- 配置建议:
- 配置镜像加速器,提高镜像拉取速度
- 调整Docker存储驱动,根据实际情况选择overlay2或btrfs
- 设置合理的容器资源限制,避免资源耗尽
- 安全建议:
- 启用Docker Content Trust,验证镜像完整性
- 使用非root用户运行容器
- 定期扫描镜像漏洞,及时更新基础镜像
- 运维建议:
- 配置Docker日志轮转,避免日志占用过多空间
- 使用Docker Compose管理多容器应用
- 建立容器监控机制,及时发现异常
更多学习教程公众号风哥教程itpux_com
from Docker视频:www.itpux.com
本文由风哥教程整理发布,仅用于学习测试使用,转载注明出处:http://www.fgedu.net.cn/10327.html
