安装docker
Docker的基本组成
镜像(image)
docker镜像就好比一个模板,可以通过这个模板来创建容器服务。tomcat镜像===>run ===>tomcat01容器(提供服务器),通过这个镜像可以创建多个容器(最终服务运行或者项目运行就是在这个容器中)。
容器(container)
Docker利用容器技术,独立运行一个或者一个组应用,通过镜像来创建的。
启动,停止,删除,基本命令
缪按就可以把这个容器理解为就是一个简易的Linux系统,
仓库(repository)
仓库就是来存放镜像的地方!
仓库分为公有仓库和私有仓库!
Docker Hub,Aliyun 。。。都有容器服务器(配置镜像加速!)
安装docker
# 系统内核是3.10以上的
[root@localhost ~]# uname -r
3.10.0-693.el7.x86_64
# 系统版本
[root@localhost ~]# 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"
安装Docker
# 1 卸载旧版本的docker
yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
# 2 需要的安装包
yum install -y yum-utils
# 3 设置镜像的仓库
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 #建议阿里云
# 更新yum软件包索引
yum makecache fast
# 4 安装docker相关的内容 docker-ce 社区 docker-ee 企业
yum install docker-ce docker-ce-cli containerd.io
# 5 启动Docker
systemctl start docker
# 6 使用docker version查看是否安装成功
docker version
[root@localhost ~]# docker version
Client: Docker Engine - Community
Version: 19.03.12
API version: 1.40
Go version: go1.13.10
Git commit: 48a66213fe
Built: Mon Jun 22 15:46:54 2020
OS/Arch: linux/amd64
Experimental: false
Server: Docker Engine - Community
Engine:
Version: 19.03.12
API version: 1.40 (minimum version 1.12)
Go version: go1.13.10
Git commit: 48a66213fe
Built: Mon Jun 22 15:45:28 2020
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.2.13
GitCommit: 7ad184331fa3e55e52b890ea95e65ba581ae3429
runc:
Version: 1.0.0-rc10
GitCommit: dc9208a3303feef5b3839f4323d9beb36df0a9dd
docker-init:
Version: 0.18.0
GitCommit: fec3683
# 7 运行 hello-world
docker run hello-world
[root@localhost ~]# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
0e03bdcc26d7: Pull complete
Digest: sha256:49a1c8800c94df04e9658809b006fd8a686cab8028d33cfba2cc049724254202
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@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@localhost ~]#
# 8 查看一下下载的这个hello-world镜像
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest bf756fb1ae65 7 months ago 13.3kB
了解卸载Docker
# 1 卸载依赖
yum remove docker-ce docker-ce-cli containerd.io
# 2 删除资源
rm -rf /var/lib/docker
# 3 docker的默认资源路径
/var/lib/docker
aliyun镜像加速
-
登录阿里云,找到容器镜像服务
-
找到镜像加速地址
-
- 配置使用
sudo mkdir -p /etc/docker sudo tee /etc/docker/daemon.json <<-'EOF' { "registry-mirrors": ["https://pp4vj5it.mirror.aliyuncs.com"] } EOF sudo systemctl daemon-reload sudo systemctl restart docker
回顾hello-world流程
底层原理
docker是怎么工作的
Docker是一个Client-Server结构的系统,Docker的守护进程运行在主机上,通过Socket从客户端访问!DockerServer接收到Docker-Client指令,就会执行这个命令!
DOcker为什么比虚拟机快
- Docker有着比虚拟机更少的抽象层
- docker利用的是宿主机的内核,vm需要的是GuestOS
所以说,新建一个容器的时候,docker不需要虚拟机一样重新加载一个操作系统内核,避免引导,虚拟机是加载Guest OS ,分钟级别,而Docker是利用宿主机的操作系统,省略了这个复杂过程,秒钟级别
之后学习完毕所有的命令,在看这段理论,就会很清晰