Docker基础——初识Docker

Docker架构

Docker 使用客户端-服务器 (C/S) 架构模式,使用远程API来管理和创建Docker容器。

  • Docker 客户端(Client) : Docker 客户端通过命令行或者其他工具使用 Docker SDK (https://docs.docker.com/develop/sdk/) 与 Docker 的守护进程通信。
  • Docker 主机(Host) :一个物理或者虚拟的机器用于执行 Docker 守护进程和容器。

Docker 包括三个基本概念:

  • 镜像(Image):Docker 镜像(Image),就相当于是一个 root 文件系统。比如官方镜像 ubuntu:16.04 就包含了完整的一套 Ubuntu16.04 最小系统的 root 文件系统。
  • 容器(Container):镜像(Image)和容器(Container)的关系,就像是面向对象程序设计中的类和实例一样,镜像是静态的定义,容器是镜像运行时的实体。容器可以被创建、启动、停止、删除、暂停等。
  • 仓库(Repository):仓库可看着一个代码控制中心,用来保存镜像

Docker 安装

卸载老的Docker及依赖

shell 复制代码
sudo yum remove docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-engine

安装一些依赖库

  • yum-utils 提供 yum-config-manager 类库
  • device-mapper-persistent-data 和 lvm2 被devicemapper 存储驱动依赖
shell 复制代码
sudo yum install -y yum-utils \
  device-mapper-persistent-data \
  lvm2

设置稳定版本的库

shell 复制代码
sudo yum-config-manager \
    --add-repo \
    https://download.docker.com/linux/centos/docker-ce.repo

安装Docker CE

bash 复制代码
yum install -y docker-ce

安装完后启动

shell 复制代码
sudo systemctl start docker

查看docker版本

shell 复制代码
[root@test ~]# docker -v
Docker version 24.0.4, build 3713ee1

查看docker状态

shell 复制代码
[root@test ~]# sudo systemctl start docker
[root@test ~]# systemctl status docker
● docker.service - Docker Application Container Engine
   Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled; vendor preset: disabled)
   Active: active (running) since Tue 2023-07-11 07:22:15 UTC; 21s ago
     Docs: https://docs.docker.com
 Main PID: 2222 (dockerd)
    Tasks: 11
   Memory: 29.9M
   CGroup: /system.slice/docker.service
           └─2222 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock

仓库配置

Docker 官方和国内很多云服务商都提供了国内加速器服务,比如:

shell 复制代码
# 配置镜像加速器
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["http://hub-mirror.c.163.com"]
}
EOF

之后重新启动服务

shell 复制代码
$ sudo systemctl daemon-reload
$ sudo systemctl restart docker

镜像查看和拉取

拉取hello world

shell 复制代码
$ docker pull hello-world:latest

[root@test ~]# docker pull hello-world:latest
latest: Pulling from library/hello-world
719385e32844: Pull complete 
Digest: sha256:a13ec89cdf897b3e551bd9f89d499db6ff3a7f44c5b9eb8bca40da20eb4ea1fa
Status: Downloaded newer image for hello-world:latest
docker.io/library/hello-world:latest

看本地仓库是否有这个库

shell 复制代码
[root@test ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
hello-world   latest    9c7a54a9a43c   2 months ago   13.3kB

运行这个镜像的实例,即容器

shell 复制代码
[root@test ~]# docker run hello-world

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/

注意, 如果你在没有镜像的时候,直接docker run hello-world也是可以的;它会先检查本地是否有这个镜像,没有的话会先从指定仓库中拉取

启动docker服务:systemctl start docker

停止docker服务:systemctl stop docker

重启docker服务:systemctl restart docker

查看docker服务状态:systemctl status docker

设置docker开机启动:systemctl enable docker

取消docker开机启动:systemctl disable docker


脚本安装

sh 复制代码
#!/bin/bash
# 移除掉旧的版本
sudo yum remove docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-selinux \
                  docker-engine-selinux \
                  docker-engine

# 删除所有旧的数据
sudo rm -rf /var/lib/docker

#  安装依赖包
sudo yum install -y yum-utils \
  device-mapper-persistent-data \
  lvm2

# 添加源,使用了阿里云镜像
sudo yum-config-manager \
    --add-repo \
    http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

# 配置缓存
sudo yum makecache fast

# 安装最新稳定版本的docker
sudo yum install -y docker-ce

# 配置镜像加速器
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["http://hub-mirror.c.163.com"]
}
EOF

# 启动docker引擎并设置开机启动
sudo systemctl start docker
sudo systemctl enable docker

# 配置当前用户对docker的执行权限
sudo groupadd docker
sudo gpasswd -a ${USER} docker
sudo systemctl restart docker
相关推荐
认真的酒窝1 小时前
自己动手开发编译器(十一)语义分析
java·开发语言
wbs_scy3 小时前
Linux C++ 高并发编程:线程池全链路深度解析,从原理到手撕实现
java·开发语言
JAVA面经实录9173 小时前
Linux 常用命令完整知识体系
java·linux·开发语言·汇编
Full Stack Developme5 小时前
Java LRU 与 LFU 算法及应用
java·开发语言·算法
万联WANFLOW6 小时前
SD-WAN 控制平面高可用怎么做?SDWAN 控制器挂了,全网会发生什么
运维·网络·分布式·架构
程序员老油条7 小时前
WSL2 + Docker Desktop:Windows 下的完美 Java 开发环境
java·windows·docker·wsl2
shushangyun_8 小时前
2026智能采购商城系统选型指南:如何引领企业数字化采购升级
java·大数据·数据库·人工智能·机器学习
她说..8 小时前
Java 默认值设置方式
java·开发语言·后端·springboot
学渣超8 小时前
记一次分布式事务数据不一致的排查之旅:从超时到索引,层层剥茧
java·后端·架构
掉鱼的猫8 小时前
Agent Harness 实战指南:构建生产级 AI Agent 的"马具"框架
java·llm·aigc