使用Docker技术可以帮助企业快速水平扩展服务,从而到达弹性部署业务的能力。
在云服务概念兴起之后,Docker的使用场景和范围进一步发展,如今在微服务架构越来越流行的情况下,微服务+Docker的完美组合,更加方便微服务架构运维部署落地。
Docker 属于 Linux 容器的一种封装,提供简单易用的容器使用接口。它是目前最流行的 Linux 容器解决方案。
Docker 将应用程序与该程序的依赖,打包在一个文件里面。运行这个文件,就会生成一个虚拟容器。程序在这个虚拟容器里运行,就好像在真实的物理机上运行一样。
简而言之, 用户可以方便地创建和使用容器,把自己的应用放入容器。容器还可以进行版本管理、复制、分享、修改,就像管理普通的代码一样。
Docker 在容器的基础上,进行了进一步的封装,从文件系统、网络互联到进程隔离等等,极大的简化了容器的创建和维护。使得 Docker 技术比虚拟机技术更为轻便、快捷
Docker 跟传统的虚拟化方式相比具有众多的优势。
Docker是CS架构,主要有两个概念:
了解了Docker的组成,再来了解一下Docker的三个主要概念:
镜像就相当于打包好的版本,镜像启动之后运行在容器中,仓库就是装存储镜像的地方。
建议在linux环境下安装Docker, 使用Centos7+yum来安装Docker
内核3.10及以上
[root@VM-0-7-centos ~]# uname -r
3.10.0-1160.11.1.el7.x86_64
[root@VM-0-7-centos ~]# cat /etc/os-release
NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="7"
PRETTY_NAME="CentOS Linux 7 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:7"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"
CENTOS_MANTISBT_PROJECT="CentOS-7"
CENTOS_MANTISBT_PROJECT_VERSION="7"
REDHAT_SUPPORT_PRODUCT="centos"
REDHAT_SUPPORT_PRODUCT_VERSION="7"
[root@VM-0-7-centos ~]#
[root@VM-0-7-centos ~]# yum remove docker \
> docker-client \
> docker-client-latest \
> docker-common \
> docker-latest \
> docker-latest-logrotate \
> docker-logrotate \
> docker-engine
Loaded plugins: fastestmirror, langpacks
No Match for argument: docker
No Match for argument: docker-client
No Match for argument: docker-client-latest
No Match for argument: docker-common
No Match for argument: docker-latest
No Match for argument: docker-latest-logrotate
No Match for argument: docker-logrotate
No Match for argument: docker-engine
No Packages marked for removal
[root@VM-0-7-centos ~]#
[root@VM-0-7-centos ~]# yum install -y yum-utils
Loaded plugins: fastestmirror, langpacks
Determining fastest mirrors
elk | 2.9 kB 00:00:00
epel | 4.7 kB 00:00:00
extras | 2.9 kB 00:00:00
os | 3.6 kB 00:00:00
updates | 2.9 kB 00:00:00
(1/5): extras/7/x86_64/primary_db | 243 kB 00:00:00
(2/5): epel/7/x86_64/updateinfo | 1.0 MB 00:00:00
(3/5): elk/primary_db | 590 kB 00:00:00
(4/5): epel/7/x86_64/primary_db | 7.0 MB 00:00:00
(5/5): updates/7/x86_64/primary_db | 11 MB 00:00:01
Package yum-utils-1.1.31-54.el7_8.noarch already installed and latest version
Nothing to do
[root@VM-0-7-centos ~]#
yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo # 默认是国外的
yum-config-manager \
--add-repo \
http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo # 阿里云镜像仓库(推荐)
那必须整国内的呀
[root@VM-0-7-centos ~]# yum-config-manager \
> --add-repo \
> http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
Loaded plugins: fastestmirror, langpacks
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@VM-0-7-centos ~]#
[root@VM-0-7-centos ~]# yum makecache fast
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
docker-ce-stable | 3.5 kB 00:00:00
elk | 2.9 kB 00:00:00
epel | 4.7 kB 00:00:00
extras | 2.9 kB 00:00:00
os | 3.6 kB 00:00:00
updates | 2.9 kB 00:00:00
(1/2): docker-ce-stable/7/x86_64/primary_db | 64 kB 00:00:00
(2/2): docker-ce-stable/7/x86_64/updateinfo | 55 B 00:00:00
Metadata Cache Created
[root@VM-0-7-centos ~]#
[root@VM-0-7-centos ~]# yum install docker-ce docker-ce-cli containerd.io
一路 y
直到看到
Installed:
containerd.io.x86_64 0:1.4.10-3.1.el7 docker-ce.x86_64 3:20.10.8-3.el7 docker-ce-cli.x86_64 1:20.10.8-3.el7
Dependency Installed:
audit-libs-python.x86_64 0:2.8.5-4.el7 checkpolicy.x86_64 0:2.5-8.el7
container-selinux.noarch 2:2.119.2-1.911c772.el7_8 docker-ce-rootless-extras.x86_64 0:20.10.8-3.el7
docker-scan-plugin.x86_64 0:0.8.0-3.el7 fuse-overlayfs.x86_64 0:0.7.2-6.el7_8
fuse3-libs.x86_64 0:3.6.1-4.el7 libcgroup.x86_64 0:0.41-21.el7
libsemanage-python.x86_64 0:2.5-14.el7 policycoreutils-python.x86_64 0:2.5-34.el7
python-IPy.noarch 0:0.75-6.el7 setools-libs.x86_64 0:3.3.8-4.el7
slirp4netns.x86_64 0:0.4.3-4.el7_8
Complete!
[root@VM-0-7-centos ~]# systemctl start docker
[root@VM-0-7-centos ~]#
[root@VM-0-7-centos ~]# docker version
Client: Docker Engine - Community
Version: 20.10.8
API version: 1.41
Go version: go1.16.6
Git commit: 3967b7d
Built: Fri Jul 30 19:55:49 2021
OS/Arch: linux/amd64
Context: default
Experimental: true
Server: Docker Engine - Community
Engine:
Version: 20.10.8
API version: 1.41 (minimum version 1.12)
Go version: go1.16.6
Git commit: 75249d8
Built: Fri Jul 30 19:54:13 2021
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.4.10
GitCommit: 8848fdb7c4ae3815afcc990a8a99d663dda1b590
runc:
Version: 1.0.2
GitCommit: v1.0.2-0-g52b36a2
docker-init:
Version: 0.19.0
GitCommit: de40ad0
[root@VM-0-7-centos ~]#
[root@VM-0-7-centos ~]# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
2db29710123e: Pull complete
Digest: sha256:9ade9cc2e26189a19c2e8854b9c8f1e14829b51c55a630ee675a5a9540ef6ccf
Status: Downloaded newer image for hello-world:latest
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@VM-0-7-centos ~]#
[root@VM-0-7-centos ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest feb5d9fea6a5 10 days ago 13.3kB
[root@VM-0-7-centos ~]#
[root@VM-0-7-centos ~]#yum remove docker-ce docker-ce-cli containerd.io
[root@VM-0-7-centos ~]#rm -rf /var/lib/docker
[root@VM-0-7-centos ~]#rm -rf /var/lib/containerd
登录阿里云,找到容器镜像服务,找到镜像加速器
[root@VM-0-7-centos ~]# mkdir -p /etc/docker
[root@VM-0-7-centos ~]# tee /etc/docker/daemon.json <<-'EOF'
> {
> "registry-mirrors": ["https://0uf1zwdk.mirror.aliyuncs.com"]
> }
> EOF
{
"registry-mirrors": ["https://0uf1zwdk.mirror.aliyuncs.com"]
}
[root@VM-0-7-centos ~]#
[root@VM-0-7-centos ~]# cat /etc/docker/daemon.json
{
"registry-mirrors": ["https://0uf1zwdk.mirror.aliyuncs.com"]
}
[root@VM-0-7-centos ~]#
[root@VM-0-7-centos ~]#
[root@VM-0-7-centos ~]# systemctl daemon-reload
[root@VM-0-7-centos ~]# systemctl restart docker
[root@VM-0-7-centos ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest feb5d9fea6a5 10 days ago 13.3kB
[root@VM-0-7-centos ~]#