1 简介
1.1 什么是Docker
- Docker 是一个开源的应用容器引擎,基于 Go 语言并遵从 Apache2.0 协议开源;
- Docker 可以让开发者打包他们的应用以及依赖包到一个轻量级、可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现虚拟化;
- 容器是完全使用沙箱机制,相互之间不会有任何接口,更重要的是容器性能开销极低;
- Docker 从 17.03 版本之后分为 CE(Community Edition-社区版)和 EE(Enterprise Edition-企业版)。
1.2 友情链接
- Docker官网
- Docker中文社区
- Docker Hub
1.3 docker 架构图
1.4 docker工作原理
Docker是一个Client-Server结构的系统,Docker守护进程运行在主机上, 然后通过Socket连接从客户端访问,守护进程从客户端接受命令并管理运行在主机上的容器。 容器,是一个运行时环境,就是我们前面说到的集装箱。
1.5 docker为什么比vmware块
(1) 更瘦,没有虚拟化硬件
docker有着比虚拟机更少的抽象层。由亍docker不需要Hypervisor实现硬件资源虚拟化,运行在docker容器上的程序直接使用的都是实际物理机的硬件资源。因此在CPU、内存利用率上docker将会在效率上有明显优势。
(2) 使用的是宿主机的内核,不需要重新加载一个操作系统
docker利用的是宿主机的内核,而不需要Guest OS。因此,当新建一个容器时,docker不需要和虚拟机一样重新加载一个操作系统内核。仍而避免引寻、加载操作系统内核返个比较费时费资源的过程,当新建一个虚拟机时,虚拟机软件需要加载Guest OS,返个新建过程是分钟级别的。而docker由于直接利用宿主机的操作系统,则省略了返个过程,因此新建一个docker容器只需要几秒钟。
2 安装
官网推荐仓库安装
2.1 Centos 安装docker
2.1.1 环境需要
Centos7内核需在3.10以上
# 或者 uname -r 只查看内核版本
[root@centos2 ~]# uname -a
Linux centos2 3.10.0-1160.el7.x86_64 #1 SMP Mon Oct 19 16:18:59 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
[root@centos2 ~]#
2.2.2 卸载旧版本
安装前检查是否有旧版本
[root@mozhu ~]# find / -iname docker
/etc/docker
/var/lib/docker
/run/docker
/usr/bin/docker
/usr/libexec/docker
/usr/share/bash-completion/completions/docker
/sys/fs/cgroup/hugetlb/docker
/sys/fs/cgroup/devices/docker
/sys/fs/cgroup/memory/docker
/sys/fs/cgroup/blkio/docker
/sys/fs/cgroup/pids/docker
/sys/fs/cgroup/cpuset/docker
/sys/fs/cgroup/freezer/docker
/sys/fs/cgroup/perf_event/docker
/sys/fs/cgroup/net_cls,net_prio/docker
/sys/fs/cgroup/cpu,cpuacct/docker
/sys/fs/cgroup/systemd/docker
[root@mozhu ~]#
卸载旧版本
sudo yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
2.2.3 安装仓库
# 1 更新yum
[root@centos2 ~]# yum makecache fast
# 2 安装工具包
[root@centos2 ~]# yum install -y yum-utils
# 3 配置仓库 yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo 这个是从国外下载,我们配置阿里的
[root@centos2 ~]# yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
adding repo from: http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
grabbing file http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo to /etc/yum.repos.d/docker-ce.repo
repo saved to /etc/yum.repos.d/docker-ce.repo
[root@centos2 ~]#
配置阿里的镜像仓库 最好有 阿里ESC(按照2.3步骤)
如果新建 /etc/docker/daemon.json,容易出错,导致docker启动报错
2.3.4 安装docker
# 安装docker -ce 社区版 -ee 企业版 需要授权 推荐ce 默认安装最新的
[root@centos2 ~]# yum -y install docker-ce docker-ce-cli containerd.io
或者安装指定版本
# 安装指定版本
# 1 列出可用版本
yum list docker-ce --showduplicates | sort -r
# 2 安装指定版本
# 按其完全限定的软件包名称(即软件包名称(docker ce)加上版本字符串(第2列),从第一个冒号(:)开始,一直到第一个连字符,用连字符(-)分隔)安装特定版本。例如,docker-ce-18.09.1。
yum install docker-ce-<VERSION_STRING> docker-ce-cli-<VERSION_STRING> containerd.io
2.3.5 启动 、停止
# 启动docker
[root@centos2 ~]# systemctl start docker
# 查看状态
[root@centos2 ~]# 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 六 2021-07-17 15:19:52 CST; 2min 55s ago
Docs: https://docs.docker.com
Main PID: 4415 (dockerd)
Tasks: 8
Memory: 53.0M
CGroup: /system.slice/docker.service
└─4415 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
。。。
7月 17 15:19:52 centos2 systemd[1]: Started Docker Application Container Engine.
Hint: Some lines were ellipsized, use -l to show in full.
# 停止
[root@centos2 ~]# systemctl stop docker
Warning: Stopping docker.service, but it can still be activated by:
docker.socket
[root@centos2 ~]# ps -ef |grep docker
root 4572 1313 0 15:23 pts/1 00:00:00 grep --color=auto docker
[root@centos2 ~]#
查看版本信息
[root@centos2 ~]# ps -ef |grep docker
root 4415 1 1 15:19 ? 00:00:00 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
root 4552 1313 0 15:20 pts/1 00:00:00 grep --color=auto docker
# 查看daocker 信息
[root@centos2 ~]# docker version 启动后可以看到 clietn 和server信息
Client: Docker Engine - Community
Version: 20.10.7
API version: 1.41
Go version: go1.13.15
Git commit: f0df350
Built: Wed Jun 2 11:58:10 2021
OS/Arch: linux/amd64
Context: default
Experimental: true
Server: Docker Engine - Community
Engine:
Version: 20.10.7
API version: 1.41 (minimum version 1.12)
Go version: go1.13.15
Git commit: b0f5bc3
Built: Wed Jun 2 11:56:35 2021
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.4.6
GitCommit: d71fcd7d8303cbf684402823e425e9dd2e99285d
runc:
Version: 1.0.0-rc95
GitCommit: b9ee9c6314599f1b4a7f497e1f1f856fe433d3b7
docker-init:
Version: 0.19.0
GitCommit: de40ad0
[root@centos2 ~]#
查看安装信息
[mozhu@momo java]$ sudo docker info
Client:
Context: default
Debug Mode: false
Plugins:
app: Docker App (Docker Inc., v0.9.1-beta3)
buildx: Build with BuildKit (Docker Inc., v0.6.3-docker)
scan: Docker Scan (Docker Inc., v0.9.0)
Server:
Containers: 0
Running: 0
Paused: 0
Stopped: 0
Images: 0
Server Version: 20.10.11
Storage Driver: overlay2
Backing Filesystem: extfs
Supports d_type: true
Native Overlay Diff: true
userxattr: false
Logging Driver: json-file
Cgroup Driver: cgroupfs
Cgroup Version: 1
Plugins:
Volume: local
Network: bridge host ipvlan macvlan null overlay
Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
Swarm: inactive
Runtimes: io.containerd.runc.v2 io.containerd.runtime.v1.linux runc
Default Runtime: runc
Init Binary: docker-init
containerd version: 7b11cfaabd73bb80907dd23182b9347b4245eb5d
runc version: v1.0.2-0-g52b36a2
init version: de40ad0
Security Options:
seccomp
Profile: default
Kernel Version: 3.10.0-957.el7.x86_64
Operating System: CentOS Linux 7 (Core)
OSType: linux
Architecture: x86_64
CPUs: 1
Total Memory: 1.795GiB
Name: momo
ID: QMYN:YE7X:RKUP:UOY7:7SNY:YP6Q:TQTW:SEIH:YVPL:6OMC:J6Z3:HGG6
Docker Root Dir: /var/lib/docker ## docker默认的存储路径
Debug Mode: false
Registry: https://index.docker.io/v1/
Labels:
Experimental: false
Insecure Registries:
127.0.0.0/8
Live Restore Enabled: false
[mozhu@momo java]$
2.3.6 测试
# 测试 hello-world 没找到,会去仓库找,先拉去到本地然后运行
[root@centos2 ~]# docker run hello-world
Unable to find image 'hello-world:latest' locally # 没有找到镜像
latest: Pulling from library/hello-world # 仓库中拉去hello-world 镜像
b8dfde127a29: Pull complete # 拉去完成
Digest: sha256:df5f5184104426b65967e016ff2ac0bfcd44ad7899ca3bbcf8e44e4461491a9e
Status: Downloaded newer image for hello-world:latest
Hello from Docker! # 运行完成,输出 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/
[root@centos2 ~]#
2.2 centos 卸载 docker
# 官网提供卸载:
# 1 卸载 docker社区版,客户端,容器
sudo yum remove docker-ce docker-ce-cli containerd.io
# 2.不会自动删除主机上的映像、容器、卷或自定义配置文件。要删除所有图像、容器和卷,请执行以下操作:
rm -rf /var/lib/docker
rm -rf /var/lib/containerd
2.3 阿里配ECS置加速器
如果是阿里云服务器,可以配置docker加速器
- 产品与服务–弹性计算–选择容器镜像服务
- 镜像工具–镜像加速器–操作文档–centos
- 复制命令,在服务器中执行即可
3 Docker命令
3.1 基本命令
# 查看docker版本
docker version
# 查看docker 信息
docker info
# 帮助命令
docker 命令 --help
docker 官网命令介绍地址 https://docs.docker.com/engine/reference/commandline
3.2 镜像命令
3.2.1 docker images 查看本地主机镜像
[root@mozhu ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
minio/minio latest 433039cbe02e 9 days ago 266MB
3.2.2 docker search 搜索镜像
和官网https://hub.docker.com/搜索镜像相同
# 从dockerhub上搜索镜像 STARS 使用次数 OFFICIAL 官方
[root@centos2 ~]# docker search --limit 5 mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 11135 [OK]
mysql/mysql-server Optimized MySQL Server Docker images. Create… 829 [OK]
centos/mysql-57-centos7 MySQL 5.7 SQL database server 89
mysql/mysql-cluster Experimental MySQL Cluster Docker images. Cr… 88
schickling/mysql-backup-s3 Backup MySQL to S3 (supports periodic backup… 29 [OK]
[root@centos2 ~]#
3.2.3 docker pull 下载镜像
#默认下载 dockerhub 中最新版本
[root@centos2 ~]# docker pull mysql
# 指定版本下载,这个版本必