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 官方和国内很多云服务商都提供了国内加速器服务,比如:
- 阿里云的加速器:https://help.aliyun.com/document_detail/60750.html
- 网易加速器:http://hub-mirror.c.163.com
- Docker官方中国加速器:https://registry.docker-cn.com
- ustc 的镜像:https://docker.mirrors.ustc.edu.cn
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