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
相关推荐
Coder_Boy_4 分钟前
基于SpringAI的在线考试系统-考试管理功能布局+交互优化方案
java·数据库·人工智能·spring boot·交互·ddd·tdd
Tao____11 分钟前
可以本地部署的物联网平台
java·开发语言·物联网·mqtt·低代码
码界奇点11 分钟前
基于DDD与CQRS的Java企业级应用框架设计与实现
java·开发语言·c++·毕业设计·源代码管理
柏林以东_12 分钟前
线程安全的数据集合
java·开发语言·安全
进阶的猿猴14 分钟前
java中实现markdown转为pdf
java·pdf·markdown
sunnyday042615 分钟前
Nginx与Spring Cloud Gateway QPS统计全攻略
java·spring boot·后端·nginx
Coder_Boy_17 分钟前
基于SpringAI的在线考试系统-试卷管理模块完整优化方案
前端·人工智能·spring boot·架构·领域驱动
海南java第二人19 分钟前
Spring Boot全局异常处理终极指南:打造优雅的API错误响应体系
java·spring boot·后端
南朝雨20 分钟前
Spring Boot Admin日志监控坑点:远程配置的logging.file.name为何生效又失效?
java·spring boot·spring cloud·微服务·logback
sanggou22 分钟前
Spring Cloud Gateway 转发 SSE 的那些坑
java