📚前言
👀回顾,系统学习docker系列的总计划如下:
🔗相关文档:
【docker基础】Ubuntu 安装 Docker 超详细小白教程
本文,是计划的第二周的内容,内容纲要如下:
第2周:环境安装与配置
- Docker Desktop 安装 :
- Windows 系统安装指南
- macOS 系统安装指南
- Linux 系统安装指南
- 环境验证 :运行
docker --version和docker run hello-world - Docker 设置:镜像加速配置、资源分配调整
- 基础命令熟悉 :
docker info、docker --help
🌍Docker第二周:安装、配置与基础命令
1. Docker Desktop安装指南
1.1 Windows系统安装指南
系统要求:
- Windows 10 64位专业版、企业版或教育版(版本1903或更高)
- Windows 11 64位专业版、企业版或教育版
- 至少4GB内存
- 开启Hyper-V和容器功能
安装步骤说明:
- 访问Docker官网下载Docker Desktop for Windows安装包
- 双击下载的安装包(.exe文件)启动安装向导
- 在安装界面中,保持默认选项,点击"OK"
- 安装完成后,点击"Close and restart"重启计算机
- 重启后,Docker Desktop会自动启动
- 首次启动时,需要接受服务条款并登录Docker账号(可选)
1.2 macOS系统安装指南
系统要求:
- macOS 11(Big Sur)或更高版本
- 至少4GB内存
- Apple Silicon或Intel处理器
安装步骤:
- 访问Docker官网下载Docker Desktop for Mac安装包
- 双击下载的.dmg文件
- 将Docker图标拖动到Applications文件夹
- 在Applications文件夹中找到Docker并双击启动
- 首次启动时,需要输入管理员密码并接受服务条款
- 登录Docker账号(可选)
1.3 Linux系统安装指南
📣提示:Ubuntu更详细的安装说明,见文档:
【docker基础】Ubuntu 安装 Docker 超详细小白教程
1.3.1 Ubuntu系统:
-
更新软件包索引:
sudo apt update -
安装依赖包:
sudo apt install apt-transport-https ca-certificates curl gnupg-agent software-properties-common -
添加Docker官方GPG密钥:
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - -
添加Docker存储库:
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" -
安装Docker Engine:
sudo apt update sudo apt install docker-ce docker-ce-cli containerd.io -
验证安装:
sudo docker run hello-world
1.3.2 CentOS系统:
-
安装依赖包:
sudo yum install -y yum-utils device-mapper-persistent-data lvm2 -
添加Docker存储库:
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo -
安装Docker Engine:
sudo yum install docker-ce docker-ce-cli containerd.io -
启动Docker服务:
sudo systemctl start docker -
设置Docker开机自启:
sudo systemctl enable docker -
验证安装:
sudo docker run hello-world
2. 环境验证
2.1 查看Docker版本
-
打开命令行工具(Windows:CMD或PowerShell;macOS/Linux:终端)
-
运行以下命令:
docker --version -
预期输出示例:
Docker version 20.10.24, build 297e128
2.2 运行Hello World容器
-
在命令行中运行以下命令:
docker run hello-world -
预期输出示例:
Hello from Docker! This message shows that your installation appears to be working correctly. -
这个命令会下载并运行一个名为"hello-world"的官方Docker镜像,验证Docker是否正常工作。
3. Docker设置
3.1 镜像加速配置
Windows/macOS(通过Docker Desktop):
-
打开Docker Desktop
-
点击右上角的设置图标(齿轮图标)
-
选择"Docker Engine"选项卡
-
在配置文件中添加镜像源,例如:
{ "registry-mirrors": [ "https://docker.mirrors.ustc.edu.cn", "https://hub-mirror.c.163.com", "https://mirror.baidubce.com" ] } -
点击"Apply & Restart"保存设置并重启Docker
Linux系统:
-
创建或编辑Docker配置文件:
sudo nano /etc/docker/daemon.json -
添加镜像源配置:
{ "registry-mirrors": [ "https://docker.mirrors.ustc.edu.cn", "https://hub-mirror.c.163.com", "https://mirror.baidubce.com" ] } -
保存文件并重启Docker服务:
sudo systemctl daemon-reload sudo systemctl restart docker -
验证配置是否生效:
docker info在输出中找到"Registry Mirrors"部分,确认镜像源已添加
3.2 资源分配调整
Windows/macOS(通过Docker Desktop):
- 打开Docker Desktop
- 点击右上角的设置图标(齿轮图标)
- 选择"Resources"选项卡
- 调整CPU、内存和磁盘资源分配:
- CPU:建议分配2-4个核心
- 内存:建议分配4GB或更多
- 磁盘:根据需要调整
- 点击"Apply & Restart"保存设置并重启Docker
Linux系统:
Linux系统上的Docker资源分配通常通过Docker命令参数或Docker Compose文件来控制,例如:
# 运行容器时限制CPU和内存
docker run --cpus=2 --memory=4g nginx
4. 基础命令熟悉
4.1 docker info
功能:查看Docker系统信息,包括镜像和容器数量、存储驱动、网络等详细信息。
使用方法:
docker info
输出示例:
Client:
Context: default
Debug Mode: false
Plugins:
buildx: Docker Buildx (Docker Inc., v0.10.4)
compose: Docker Compose (Docker Inc., v2.17.3)
dev: Docker Dev Environments (Docker Inc., v0.1.0)
extension: Manages Docker extensions (Docker Inc., v0.2.19)
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: 1
Running: 0
Paused: 0
Stopped: 1
Images: 1
Server Version: 20.10.24
Storage Driver: overlay2
4.2 docker --help
功能:获取Docker命令的帮助信息,包括所有可用的子命令和选项。
使用方法:
docker --help
输出示例:
Usage: docker [OPTIONS] COMMAND
A self-sufficient runtime for containers
Options:
--config string Location of client config files (default "C:\\Users\\Username\\.docker")
-c, --context string Name of the context to use to connect to the daemon (overrides DOCKER_HOST env var and default context set with "docker context use")
-D, --debug Enable debug mode
-H, --host list Daemon socket(s) to connect to
-l, --log-level string Set the logging level ("debug"|"info"|"warn"|"error"|"fatal") (default "info")
--tls Use TLS; implied by --tlsverify
--tlscacert string Trust certificates signed only by this CA (default "C:\\Users\\Username\\.docker\\ca.pem")
--tlscert string Path to TLS certificate file (default "C:\\Users\\Username\\.docker\\cert.pem")
--tlskey string Path to TLS key file (default "C:\\Users\\Username\\.docker\\key.pem")
--tlsverify Use TLS and verify the server
-v, --version Print version information and quit
Commands:
attach Attach local standard input, output, and error streams to a running container
build Build an image from a Dockerfile
commit Create a new image from a container's changes
cp Copy files/folders between a container and the local filesystem
create Create a new container
diff Inspect changes to files or directories on a container's filesystem
events Get real time events from the server
exec Run a command in a running container
export Export a container's filesystem as a tar archive
history Show the history of an image
images List images
import Import the contents from a tarball to create a filesystem image
info Display system-wide information
inspect Return low-level information on Docker objects
kill Kill one or more running containers
load Load an image from a tar archive or STDIN
login Log in to a Docker registry
logout Log out from a Docker registry
logs Fetch the logs of a container
pause Pause all processes within one or more containers
port List port mappings or a specific mapping for the container
ps List containers
pull Pull an image or a repository from a registry
push Push an image or a repository to a registry
rename Rename a container
restart Restart one or more containers
rm Remove one or more containers
rmi Remove one or more images
run Run a command in a new container
save Save one or more images to a tar archive (streamed to STDOUT by default)
search Search the Docker Hub for images
start Start one or more stopped containers
stats Display a live stream of container(s) resource usage statistics
stop Stop one or more running containers
tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
top Display the running processes of a container
unpause Unpause all processes within one or more containers
update Update configuration of one or more containers
version Show the Docker version information
wait Block until one or more containers stop, then print their exit codes
Run 'docker COMMAND --help' for more information on a command.
4.3 其他常用基础命令
查看所有容器(包括停止的):
docker ps -a
查看所有镜像:
docker images
拉取镜像:
docker pull ubuntu
删除容器:
docker rm container_name_or_id
删除镜像:
docker rmi image_name_or_id
总结
通过本教程,你已经完成了Docker的安装、环境验证、设置配置和基础命令的学习。这些内容是Docker使用的基础,为后续的容器管理和应用部署打下了坚实的基础。在接下来的学习中,你将学习如何构建自定义镜像、使用Docker Compose管理多容器应用等更高级的内容。
建议你在实际操作中多练习这些命令,熟悉Docker的基本操作流程,这样在后续的学习中会更加得心应手。