目录
1.什么是Docker?
2.Docker是干嘛的?
3.Docker基本组成
4.Docker安装
4.1.环境准备
4.2.卸载Docker
4.3.需要的安装包
4.4.设置镜像仓库
4.5.更新yum索引
4.6.安装docker (docker-ce 社区版免费 docker-ee 企业版)
4.7.启动docker
4.8.验证docker是否启动成功
4.9.测试
4.10.查看下载的hello-world镜像
4.11.卸载docker
4.11.1.卸载 Docker 引擎、CLI 和容器包
4.11.2.主机上的映像、容器、卷或自定义配置文件不会自动删除。删除所有映像、容器和卷
1.什么是Docker?
2.Docker是干嘛的?
3.Docker基本组成
镜像(image):
容器(container):
仓库(registry):
4.Docker安装
4.1.环境准备
服务器、CentOS7(系统内核在3.0以上)
官方文档:在 CentOS | 上安装 Docker 引擎Docker 文档
4.2.卸载Docker
yum remove docker
docker-client
docker-client-latest
docker-common
docker-latest
docker-latest-logrotate
docker-logrotate
docker-engine
4.3.需要的安装包
yum install -y yum-utils
4.4.设置镜像仓库
yum-config-manager
--add-repo
https://download.docker.com/linux/centos/docker-ce.repo #(默认国外的、建议不用)
yum-config-manager
--add-repo
https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo #建议使用
4.5.更新yum索引
yum makecache fast
4.6.安装docker (docker-ce 社区版免费 docker-ee 企业版)
yum install docker-ce docker-ce-cli containerd.io
4.7.启动docker
systemctl start docker
4.8.验证docker是否启动成功
docker version
#结果如下
Client: Docker Engine - Community
Version: 20.10.11
API version: 1.41
Go version: go1.16.9
Git commit: dea9396
Built: Thu Nov 18 00:38:53 2021
OS/Arch: linux/amd64
Context: default
Experimental: true
Server: Docker Engine - Community
Engine:
Version: 20.10.11
API version: 1.41 (minimum version 1.12)
Go version: go1.16.9
Git commit: 847da18
Built: Thu Nov 18 00:37:17 2021
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.4.12
GitCommit: 7b11cfaabd73bb80907dd23182b9347b4245eb5d
runc:
Version: 1.0.2
GitCommit: v1.0.2-0-g52b36a2
docker-init:
Version: 0.19.0
GitCommit: de40ad0
4.9.测试
docker run hello-world
#结果如下
Unable to find image 'hello-world:latest' locally #-----------本地没有镜像
latest: Pulling from library/hello-world
2db29710123e: Pull complete #------------拉取镜像
Digest: sha256:cc15c5b292d8525effc0f89cb299f1804f3a725c8d05e158653a563f15e4f685
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/
4.10.查看下载的hello-world镜像
docker images
4.11.卸载docker
4.11.1.卸载 Docker 引擎、CLI 和容器包
yum remove docker-ce docker-ce-cli containerd.io
4.11.2.主机上的映像、容器、卷或自定义配置文件不会自动删除。删除所有映像、容器和卷
yum install -y yum-utils
4.4.设置镜像仓库
yum-config-manager
--add-repo
https://download.docker.com/linux/centos/docker-ce.repo #(默认国外的、建议不用)
yum-config-manager
--add-repo
https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo #建议使用
4.5.更新yum索引
yum makecache fast
4.6.安装docker (docker-ce 社区版免费 docker-ee 企业版)
yum install docker-ce docker-ce-cli containerd.io
4.7.启动docker
systemctl start docker
4.8.验证docker是否启动成功
docker version
#结果如下
Client: Docker Engine - Community
Version: 20.10.11
API version: 1.41
Go version: go1.16.9
Git commit: dea9396
Built: Thu Nov 18 00:38:53 2021
OS/Arch: linux/amd64
Context: default
Experimental: true
Server: Docker Engine - Community
Engine:
Version: 20.10.11
API version: 1.41 (minimum version 1.12)
Go version: go1.16.9
Git commit: 847da18
Built: Thu Nov 18 00:37:17 2021
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.4.12
GitCommit: 7b11cfaabd73bb80907dd23182b9347b4245eb5d
runc:
Version: 1.0.2
GitCommit: v1.0.2-0-g52b36a2
docker-init:
Version: 0.19.0
GitCommit: de40ad0
4.9.测试
docker run hello-world
#结果如下
Unable to find image 'hello-world:latest' locally #-----------本地没有镜像
latest: Pulling from library/hello-world
2db29710123e: Pull complete #------------拉取镜像
Digest: sha256:cc15c5b292d8525effc0f89cb299f1804f3a725c8d05e158653a563f15e4f685
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/
4.10.查看下载的hello-world镜像
docker images
4.11.卸载docker
4.11.1.卸载 Docker 引擎、CLI 和容器包
yum remove docker-ce docker-ce-cli containerd.io
4.11.2.主机上的映像、容器、卷或自定义配置文件不会自动删除。删除所有映像、容器和卷
yum makecache fast
4.6.安装docker (docker-ce 社区版免费 docker-ee 企业版)
yum install docker-ce docker-ce-cli containerd.io
4.7.启动docker
systemctl start docker
4.8.验证docker是否启动成功
docker version
#结果如下
Client: Docker Engine - Community
Version: 20.10.11
API version: 1.41
Go version: go1.16.9
Git commit: dea9396
Built: Thu Nov 18 00:38:53 2021
OS/Arch: linux/amd64
Context: default
Experimental: true
Server: Docker Engine - Community
Engine:
Version: 20.10.11
API version: 1.41 (minimum version 1.12)
Go version: go1.16.9
Git commit: 847da18
Built: Thu Nov 18 00:37:17 2021
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.4.12
GitCommit: 7b11cfaabd73bb80907dd23182b9347b4245eb5d
runc:
Version: 1.0.2
GitCommit: v1.0.2-0-g52b36a2
docker-init:
Version: 0.19.0
GitCommit: de40ad0
4.9.测试
docker run hello-world
#结果如下
Unable to find image 'hello-world:latest' locally #-----------本地没有镜像
latest: Pulling from library/hello-world
2db29710123e: Pull complete #------------拉取镜像
Digest: sha256:cc15c5b292d8525effc0f89cb299f1804f3a725c8d05e158653a563f15e4f685
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/
4.10.查看下载的hello-world镜像
docker images
4.11.卸载docker
4.11.1.卸载 Docker 引擎、CLI 和容器包
yum remove docker-ce docker-ce-cli containerd.io
4.11.2.主机上的映像、容器、卷或自定义配置文件不会自动删除。删除所有映像、容器和卷
systemctl start docker
4.8.验证docker是否启动成功
docker version
#结果如下
Client: Docker Engine - Community
Version: 20.10.11
API version: 1.41
Go version: go1.16.9
Git commit: dea9396
Built: Thu Nov 18 00:38:53 2021
OS/Arch: linux/amd64
Context: default
Experimental: true
Server: Docker Engine - Community
Engine:
Version: 20.10.11
API version: 1.41 (minimum version 1.12)
Go version: go1.16.9
Git commit: 847da18
Built: Thu Nov 18 00:37:17 2021
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.4.12
GitCommit: 7b11cfaabd73bb80907dd23182b9347b4245eb5d
runc:
Version: 1.0.2
GitCommit: v1.0.2-0-g52b36a2
docker-init:
Version: 0.19.0
GitCommit: de40ad0
4.9.测试
docker run hello-world
#结果如下
Unable to find image 'hello-world:latest' locally #-----------本地没有镜像
latest: Pulling from library/hello-world
2db29710123e: Pull complete #------------拉取镜像
Digest: sha256:cc15c5b292d8525effc0f89cb299f1804f3a725c8d05e158653a563f15e4f685
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/
4.10.查看下载的hello-world镜像
docker images
4.11.卸载docker
4.11.1.卸载 Docker 引擎、CLI 和容器包
yum remove docker-ce docker-ce-cli containerd.io
4.11.2.主机上的映像、容器、卷或自定义配置文件不会自动删除。删除所有映像、容器和卷
docker run hello-world
#结果如下
Unable to find image 'hello-world:latest' locally #-----------本地没有镜像
latest: Pulling from library/hello-world
2db29710123e: Pull complete #------------拉取镜像
Digest: sha256:cc15c5b292d8525effc0f89cb299f1804f3a725c8d05e158653a563f15e4f685
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/
4.10.查看下载的hello-world镜像
docker images
4.11.卸载docker
4.11.1.卸载 Docker 引擎、CLI 和容器包
yum remove docker-ce docker-ce-cli containerd.io
4.11.2.主机上的映像、容器、卷或自定义配置文件不会自动删除。删除所有映像、容器和卷
4.11.1.卸载 Docker 引擎、CLI 和容器包
yum remove docker-ce docker-ce-cli containerd.io
4.11.2.主机上的映像、容器、卷或自定义配置文件不会自动删除。删除所有映像、容器和卷
/var/lib/docker为默认工作路径
sudo rm -rf /var/lib/docker sudo rm -rf /var/lib/containerd5.Docker底层原理
5.1.docker是如何工作的?
5.2.docker为什么比VM轻?
6.Docker常用命令docker version #显示docker版本信息
docker info #显示docker的系统信息,包括镜像和容器数量
docker hlep #docker帮忙
官网API地址:docker save | Docker documentation
6.1.镜像命令docker images #查看所有本地的主机上的镜像
docker search [name] #搜索
docker pull [name][:tag] #下载镜像 name:镜像名 tag:版本 ** 核心:联合文件系统
docker rmi -f [容器id] #删除镜像
6.2.容器命令 6.2.1.*有了镜像才能创建容器docker pull centos
6.2.2.新建容器并启动docker run [可选参数] image
#参数说明
--name="Name" 容器名字 tomcat01,tomcat02 用来区分容器
-d 后台方式运行
-it 使用交互方式运行,进入容器查看内容
-p 指定容器的端口 -p 8080:8080
| -p ip:主机端口:容器端口
| -p 主机端口:容器端口
| -p 容器端口
| 容器端口
-P 随机指定端口
6.2.3.启动并进入容器docker run -it centos /bin/bash6.2.4.退出容器
exit #容器停止并退出 #使用快捷键:Ctrl+P+Q #容器不停止并退出6.2.5.列出所有的运行的容器
docker ps 命令
-a #所有运行、运行过的容器
-n=? #显示最近创建容器
-q #只显示容器的编号
6.2.6.删除docker rm [容器id] #删除指定容器,不能删除运行中容器;强制删除 docker rm -f [容器id]
docker rm -f ${docker ps -aq} #删除所有的容器
6.2.7.启动和停止容器的操作docker start [容器id] #启动容器
docker restart [容器id] #重启容器
docker stop [容器id] #停止单签正在运行的容器
docker kill [容器id] #强制停止容器
6.3.常用其他命令 6.3.1.后台启动镜像docker run -d [镜像名]
#问题docker ps 发现centos停止了
#常见的坑,docker容器使用后台运行,就必须要有一个前台程序,docker发现没有应用,就会自动停止
#nginx,容器启动后,发现自己没有提供服务,就会立刻停止,就没有程序了
6.3.2.查看日志docker logs [容器id] #显示日志
-tf #显示所有日志
--tail [数量] #要显示的条数
6.3.3.查看容器中的进程信息docker top [容器id]
6.3.4.查看镜像元数据docker inspect [容器id]
6.3.5.**进入当前正在进行的容器方式一:docker exec -it [容器id] [bashShell] #进入容器开启新的终端
方式二:docker attach it [容器id] [bashShell] #进入容器正在运行的终端
6.3.6.拷贝docker cp [容器id]:[容器内路径] [主机路径] #将容器中的文件拷贝到主机上



