【docker基础】第二课:安装、配置与基础命令

📚前言

👀回顾,系统学习docker系列的总计划如下:

【docker基础】0、系统学习docker之总计划

【docker基础】第一课、从零开始理解容器技术

🔗相关文档:

windows下安装docker

【docker基础】Ubuntu 安装 Docker 超详细小白教程

本文,是计划的第二周的内容,内容纲要如下:

第2周:环境安装与配置

  1. Docker Desktop 安装
    • Windows 系统安装指南
    • macOS 系统安装指南
    • Linux 系统安装指南
  2. 环境验证 :运行 docker --versiondocker run hello-world
  3. Docker 设置:镜像加速配置、资源分配调整
  4. 基础命令熟悉docker infodocker --help

🌍Docker第二周:安装、配置与基础命令

1. Docker Desktop安装指南

1.1 Windows系统安装指南

系统要求:

  • Windows 10 64位专业版、企业版或教育版(版本1903或更高)
  • Windows 11 64位专业版、企业版或教育版
  • 至少4GB内存
  • 开启Hyper-V和容器功能

安装步骤说明:

  1. 访问Docker官网下载Docker Desktop for Windows安装包
  2. 双击下载的安装包(.exe文件)启动安装向导
  3. 在安装界面中,保持默认选项,点击"OK"
  4. 安装完成后,点击"Close and restart"重启计算机
  5. 重启后,Docker Desktop会自动启动
  6. 首次启动时,需要接受服务条款并登录Docker账号(可选)

1.2 macOS系统安装指南

系统要求:

  • macOS 11(Big Sur)或更高版本
  • 至少4GB内存
  • Apple Silicon或Intel处理器

安装步骤:

  1. 访问Docker官网下载Docker Desktop for Mac安装包
  2. 双击下载的.dmg文件
  3. 将Docker图标拖动到Applications文件夹
  4. 在Applications文件夹中找到Docker并双击启动
  5. 首次启动时,需要输入管理员密码并接受服务条款
  6. 登录Docker账号(可选)

1.3 Linux系统安装指南

📣提示:Ubuntu更详细的安装说明,见文档:

【docker基础】Ubuntu 安装 Docker 超详细小白教程

1.3.1 Ubuntu系统:
  1. 更新软件包索引:

    复制代码
    sudo apt update
  2. 安装依赖包:

    复制代码
    sudo apt install apt-transport-https ca-certificates curl gnupg-agent software-properties-common
  3. 添加Docker官方GPG密钥:

    复制代码
    curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
  4. 添加Docker存储库:

    复制代码
    sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
  5. 安装Docker Engine:

    复制代码
    sudo apt update
    sudo apt install docker-ce docker-ce-cli containerd.io
  6. 验证安装:

    复制代码
    sudo docker run hello-world
1.3.2 CentOS系统:
  1. 安装依赖包:

    复制代码
    sudo yum install -y yum-utils device-mapper-persistent-data lvm2
  2. 添加Docker存储库:

    复制代码
    sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
  3. 安装Docker Engine:

    复制代码
    sudo yum install docker-ce docker-ce-cli containerd.io
  4. 启动Docker服务:

    复制代码
    sudo systemctl start docker
  5. 设置Docker开机自启:

    复制代码
    sudo systemctl enable docker
  6. 验证安装:

    复制代码
    sudo docker run hello-world

2. 环境验证

2.1 查看Docker版本

  1. 打开命令行工具(Windows:CMD或PowerShell;macOS/Linux:终端)

  2. 运行以下命令:

    复制代码
    docker --version
  3. 预期输出示例:

    复制代码
    Docker version 20.10.24, build 297e128

2.2 运行Hello World容器

  1. 在命令行中运行以下命令:

    复制代码
    docker run hello-world
  2. 预期输出示例:

    复制代码
    Hello from Docker!
    This message shows that your installation appears to be working correctly.
  3. 这个命令会下载并运行一个名为"hello-world"的官方Docker镜像,验证Docker是否正常工作。

3. Docker设置

3.1 镜像加速配置

Windows/macOS(通过Docker Desktop):

  1. 打开Docker Desktop

  2. 点击右上角的设置图标(齿轮图标)

  3. 选择"Docker Engine"选项卡

  4. 在配置文件中添加镜像源,例如:

    复制代码
    {
      "registry-mirrors": [
        "https://docker.mirrors.ustc.edu.cn",
        "https://hub-mirror.c.163.com",
        "https://mirror.baidubce.com"
      ]
    }
  5. 点击"Apply & Restart"保存设置并重启Docker

Linux系统:

  1. 创建或编辑Docker配置文件:

    复制代码
    sudo nano /etc/docker/daemon.json
  2. 添加镜像源配置:

    复制代码
    {
      "registry-mirrors": [
        "https://docker.mirrors.ustc.edu.cn",
        "https://hub-mirror.c.163.com",
        "https://mirror.baidubce.com"
      ]
    }
  3. 保存文件并重启Docker服务:

    复制代码
    sudo systemctl daemon-reload
    sudo systemctl restart docker
  4. 验证配置是否生效:

    复制代码
    docker info

    在输出中找到"Registry Mirrors"部分,确认镜像源已添加

3.2 资源分配调整

Windows/macOS(通过Docker Desktop):

  1. 打开Docker Desktop
  2. 点击右上角的设置图标(齿轮图标)
  3. 选择"Resources"选项卡
  4. 调整CPU、内存和磁盘资源分配:
    • CPU:建议分配2-4个核心
    • 内存:建议分配4GB或更多
    • 磁盘:根据需要调整
  5. 点击"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的基本操作流程,这样在后续的学习中会更加得心应手。

相关推荐
@土豆2 小时前
【混合云组网实战】Docker部署内网互通服务,实现本地网段访问公有云VPC私网
运维·docker·容器
merlin-mm3 小时前
volcano 原理分析
容器·kubernetes
Keep Running *3 小时前
Docker_学习笔记
笔记·学习·docker
正经教主3 小时前
【docker基础】第三课:镜像管理与Dockerfile基础
运维·docker·容器
loriloy3 小时前
Docker 部署 Docmost 详细教程
docker·docmost
阿沁QWQ3 小时前
docker使用
docker·容器·perl
杨浦老苏13 小时前
开源的AI编程工作站HolyClaude
人工智能·docker·ai·编辑器·开发·群晖
普通网友19 小时前
《K8s 自动扩缩容:基于 CPU / 内存的 HPA 配置》
docker·容器·kubernetes
学到四19 小时前
kubernetes(k8s)
云原生·容器·kubernetes